USBmicro

Your source for USB electrical device interface products.

  • Home
  • Services
    • Design
    • Contact USBmicro
  • Where to buy…
You are here: Home / Documentation / Programming Overview / MSVC++ Example

MSVC++ Example

This example uses MSVC++ to interface to the U4x1. 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.

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;

}

Documentation

Open all | Close all