Online Development Notebook > Index > Programming Overview > MSVC++ Example

MSVC++ Example using USBm.dll

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;


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);


    return 0;


Hardware:  U401 USB Interface    U421 USB Interface   U451 USB Interface

Programming:  USBm DLL Programming    Download Files

Application Notes:  U4x1 Application Notes    Misc Applications and Information    FAQ

While every effort has been made to make sure that the information posted on this site is correct, the author can not be held liable for any damages whatsoever for losses as a result of the application of this information. Use this information at your own risk.

USBmicro can design your custom and semi-custom USB product. Email about USB design can be directed to " Robert " at

Copyright © USBmicro, L.L.C., 2002-2010

Previous  |  Next ]     [ Up  |  First  |  Last ]     (Article 24 of 126)

This page is created with TreePad