LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

calling visual basic dll from c

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:


 

 

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.

(continued in next post)
0 Kudos
Message 1 of 8
(4,537 Views)

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. 

Message Edited by pblase on 04-28-2010 11:17 AM
0 Kudos
Message 2 of 8
(4,532 Views)

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

0 Kudos
Message 3 of 8
(4,524 Views)
Ok, so Visual Basic (VB6) appears to construct only ActiveX DLL's; which don't work with the usual calling procedure. Sigh. Looking at the various NI articles, there are all of the proper functions (DLLCanUnloadNow, etc) to indicate an ActiveX dll. Fine; however, when I try to follow the procedure to open it as an ActiveX server in LabView, it doesn't show any available functions (methods).
0 Kudos
Message 4 of 8
(4,516 Views)

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?

Richard S -- National Instruments -- (former) Applications Engineer -- Data Acquisition with TestStand
0 Kudos
Message 5 of 8
(4,491 Views)

Yep, same result.

 

0 Kudos
Message 6 of 8
(4,469 Views)

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. 

Richard S -- National Instruments -- (former) Applications Engineer -- Data Acquisition with TestStand
0 Kudos
Message 7 of 8
(4,442 Views)

Hi Paul,

Have you succeeded with your efforts to tie in MCP_iSensors.dll in the end?

Best regards,

Ahkub

0 Kudos
Message 8 of 8
(4,240 Views)