02-11-2015 03:39 AM
A customer using LabWindows/CVI 2013 to call a VB dll, it can call the function in the dll successfully, but leads the LabWindows/CVI Crashing when leaving the CVI.
The CVI will crash after finish the last line of code. If I remove the 6th line "#define Test", the code runs ok since it will not call the function in dll. If I recover the 6th line, the code will get Error as below
The code is shown below.
#include <stdio.h>
#include <windows.h>
#include <ansi_c.h>
#include <cvirte.h>
#define TEST
typedef struct {
unsigned int size; //0+4
unsigned short vid; //4+2
unsigned short pid; //6+2
unsigned int friendlyNameSize; //8+4
unsigned char friendlyName[512]; //12+512
unsigned int serialNumberSize; //524+4
unsigned char serialNumber[512]; //528+512
unsigned int locked; //1040+4
unsigned int usbPrintPathSize; //1044+4
unsigned char usbPrintPath[256]; //1048+256
unsigned int usbStackPathSize; //1304+4
unsigned char usbStackPath[256]; //1308+256
unsigned int usbStackAddress; //1564+4 = 1568
}ZUsbPrinterInformation;
typedef unsigned int (* DLLFunction)(const unsigned int includeLocked,
unsigned int *deviceCount,
signed int *changedSinceLastEnumeration);
int main()
{
HINSTANCE ZusbLib = NULL;
const unsigned int index=1;
int result;
DLLFunction DLLFAddress;
unsigned int *Count;
signed int *Changed;
ZusbLib=LoadLibrary("ZUSB.DLL");
if (ZusbLib == NULL)
{
printf(" ** Failed to invoke Dll ** \n");
getchar();
return -1;
}
printf(" ** Success to invoke Dll ZusbLibx=%x ** \n",ZusbLib);
DLLFAddress = (DLLFunction)GetProcAddress(ZusbLib, "ZUsbEnumerateUSBPrinters");
if (DLLFAddress == NULL)
{
FreeLibrary(ZusbLib);
printf(" ** ZUsbEnumerateUSBPrinters is not existed ** \n");
getchar();
return -1;
}
printf(" ** Success to excute ZUsbEnumerateUSBPrinters ** \n");
#ifdef TEST
result=(DLLFAddress) (0,&Count,&Changed);
printf(" USB countu=%u\n",Count);
printf(" Changed count=%d\n",Changed);
#endif
if(ZusbLib)
{
FreeLibrary(ZusbLib);
ZusbLib=NULL;
printf("Free ZusbLib");
}
printf("End%d\n",index);
getchar();
}
02-26-2015 06:11 AM - edited 02-26-2015 06:12 AM
Hi
You've declared two pointers to integers:
unsigned int *Count; signed int *Changed;
when you should have simply declared the two integers:
unsigned int Count; signed int Changed;
They will be converted to pointers by the "address of" operator "&" when you actually call the function:
result= DLLFAddress (0, &Count, &Changed);
With this correction your code does not crash anymore.