Online Development Notebook > Index > ODN > USBm DLL Programming > MSVC++ Example

MSVC++ Example using USBm.dll


This example uses MSVC++ to interface to the U4xx. The very minimum application would be to open the USB device and transmit a single command. This example shows the code that it takes to use USBm.dll to open the U401⁄U421 device and initialize the ports.

NOTE: For Version 42 or earlier of the DLL, dynamic DLL loading functions correctly and should be the method used. A planned update to the DLL will allow for static linking in VC++.

VC++ Dynamic Loading

#include "stdafx.h"
#include <iostream>
#include <windows.h>

using namespace std;

HINSTANCE hDll = 0;

typedef int (__stdcall *USBm_FindDevices_type) ();
typedef int (__stdcall *USBm_DeviceVID_type)   (unsigned char device);
typedef int (__stdcall *USBm_DevicePID_type)   (unsigned char device);
typedef int (__stdcall *USBm_DeviceDID_type)   (unsigned char device);
typedef int (__stdcall *USBm_DirectionA_type)  (unsigned char device, int);
typedef int (__stdcall *USBm_WriteA_type)      (unsigned char device, int);

USBm_FindDevices_type  USBm_FindDevices;
USBm_DeviceVID_type    USBm_DeviceVID;
USBm_DevicePID_type    USBm_DevicePID;
USBm_DeviceDID_type    USBm_DeviceDID;
USBm_WDirection A_type USBm_DirectionA;
USBm_WriteA_type       USBm_WriteA;

This is the type definition setup for dynamic DLL loading.

The main function makes use of the "LoadLibrary()" call to gain access to the DLL.

int main()
{

    int result;

    hDll = LoadLibrary("USBm.dll");

    USBm_FindDevices = (USBm_FindDevices_type)GetProcAddress(hDll, "USBm_FindDevices");
    USBm_DeviceVID = (USBm_DeviceVID_type)GetProcAddress(hDll, "USBm_DeviceVID");
    USBm_DevicePID = (USBm_DevicePID_type)GetProcAddress(hDll, "USBm_DevicePID");
    USBm_DeviceDID = (USBm_DeviceDID_type)GetProcAddress(hDll, "USBm_DeviceDID");
    USBm_DirectionA = (USBm_DirectionA_type)GetProcAddress(hDll, "USBm_DirectionA");
    USBm_WriteA = (USBm_WriteA_type)GetProcAddress(hDll, "USBm_WriteA");

    result = USBm_FindDevices();

    cout << "USBm_DeviceVID(0) = " << USBm_DeviceVID(0) << endl;
    cout << "USBm_DevicePID(0) = " << USBm_DevicePID(0) << endl;
    cout << "USBm_DeviceDID(0) = " << USBm_DeviceDID(0) << endl;

    USBm_DirectionA(0, 0xFF, 0xFF);

    USBm_WriteA(0, 0x55);

    FreeLibrary(hDll);

    return 0;

}


Hardware:  U401 USB Interface    U421 USB Interface
Programming:  USBm DLL Programming    Download Files
Application Notes:  U4x1 Application Notes    Misc Applications and Information    FAQ