04-28-2010 11:09 AM
Working in LabWindows/CVI 8.1, Windows XP 2002 SP3.
I received a dll to allow me to interface to a USB hardware device. The dll is written in Visual Basic and I received the following interface document:
(continued in next post)
MCP_iSensors.Dll
This DLL is a unmanaged WIN32 library, not a COM(component object model.)
Functions and Subroutines exported by this Library are listed below.
Function DllMain(hInst As Long, fdwReason As Long, lpvReserved As Long) As Boolean
The standard win32 DLL entry point function.
Function UsbFx2Find () As Byte
Returns a value of 1 if the iSensors USB device is found.
Sub UsbFx2Read (Addr As Byte, HiByte As Byte, LowByte As Byte, UsbStatus As Byte)
Used to read two bytes from an iSensors register.
Addr is the base address of the register and should not include the R/W bit.
The returned data bytes require decoding determined by the properties of the register.
UsbStatus returns 1 if the communications channel is functional.
UsbStatus would return 0 if the iSensors USB board is uplugged from the usb port.
If UsbStatus returns zero, the function UsbFx2Find is required to re-establish a usb link to the iSensor USB board.
Sub UsbFx2Write (Addr As Byte, HiByte As Byte, LoByte As Byte, UsbStatus As Byte)
Used to write two bytes of data to an iSensor register.
Addr is the base address of the register and should not include the R/W bit.
04-28-2010 11:16 AM - edited 04-28-2010 11:17 AM
I put together a test routine to call this (starting with just the first function):
typedef unsigned char (__stdcall *UsbFx2Find_t) (void);
typedef void (__stdcall *UsbFx2Read_t) (unsigned char *Addr, unsigned char *HiByte, unsigned char *LoByte, unsigned char *UsbStatus);
typedef void (__stdcall *UsbFx2Write_t) (unsigned char *Addr, unsigned char *HiByte, unsigned char *LoByte, unsigned char *UsbStatus);
//==============================================================================
// Static global variables
//==============================================================================
// Static functions
//==============================================================================
// Global variables
//==============================================================================
// Global functions
int main (int argc, char *argv[])
{
HINSTANCE hinstLib;
UsbFx2Find_t UsbFx2Find_p;
UsbFx2Read_t UsbFx2Read_p;
UsbFx2Write_t UsbFx2Write_p;
unsigned char a;
unsigned char Addr, HiByte, LoByte, USBStatus;
if (InitCVIRTE (0, argv, 0) == 0) {
return -1; /* out of memory */
}
//Load the library
hinstLib = LoadLibrary ("C:\\WINDOWS\\system32\\MCP_iSensors.dll");
if (hinstLib != NULL) {
UsbFx2Find_p = (UsbFx2Find_t) GetProcAddress(hinstLib, "UsbFx2Find");
UsbFx2Read_p = (UsbFx2Read_t) GetProcAddress(hinstLib, "UsbFx2Read");
UsbFx2Write_p = (UsbFx2Write_t) GetProcAddress(hinstLib, "UsbFx2Write");
if (UsbFx2Find_p != NULL) {
a = (UsbFx2Find_p)();
}
/*
if (UsbFx2Read_p != NULL) {
(UsbFx2Read_p)(&Addr, &HiByte, &LoByte, &USBStatus);
}
if (UsbFx2Write_p != NULL) {
(UsbFx2Write_p)(&Addr, &HiByte, &LoByte, &USBStatus);
}
*/
}
//done
FreeLibrary(hinstLib);
return 0;
}
It operates fine until tries to call the function ("a = (UsbFx2Find_p)();") at which point it produces a "General Protection Fault".
LoadLibrary() returns a valid handle, and I checked with a dll viewer, and GetProcAddress is getting the proper address for the function call.
Any help would be appreciated.
04-28-2010 11:24 AM
I also tried to create an import library using the utility in LabWindows/CVI (Options/Generate DLL Import Library). The utility produced the .lib file, but when I tried to run a test program I get the message that "This application has failed to start because MCP_iSensors.EXE was not found." Huh? The application is "GyroTest2.exe", the dll is MCP_iSensors.dll and MCP_iSensors.lib. There is no MCP_iSensors.EXE mentioned anywhere!
I created the import library by making a new project containing just the .h include file, output is a .dll. I never actually compile anything since I only want to use the Generate DLL Import Library function.
Thanks
Paul
04-28-2010 01:11 PM
04-29-2010 04:34 PM
I'm not exactly sure why you're seeing that behavior. I was looking around and found this post on calling a VB6 dll in C http://stackoverflow.com/questions/866644/calling-a-dll-made-in-vb6-from-c . Looking over the example code that was provided it looks like they did not put parenthesis around their function. So:
a = (UsbFx2Find_p)();
would become
a = UsbFx2Find_p();
Have you tried this?
04-30-2010 08:35 AM
Yep, same result.
05-03-2010 09:38 AM
pblase,
You should be able to register the DLL with the operating system using regsvr32 and then call the DLL using the ActiveX controller wizard included in CVI.
07-25-2010 04:04 PM
Hi Paul,
Have you succeeded with your efforts to tie in MCP_iSensors.dll in the end?
Best regards,
Ahkub