06-22-2007 06:06 AM
06-22-2007 07:43 PM
06-25-2007 08:32 AM
Hello Jonathan,
thanks a lot for your response. With the driver for my device, I got a LabVIEW example. Thanks to this example I know what parameters should be passed to the dll functions. Thanks to your post, I now know how to call these functions, however When executing the "Write" function, I got the error "CVI has detected an inconsistancy of the run-time stack. This might have been caused by calling convention mismatch in the last function call." This is on the line " error = (UsbdllWrite) (4, "*IDN?\r\n", 7);" of the code below. Note that the GetProcAddress functions all result in valid addresses. And that the execution of the UsbdllInit function is successful.
Any idea what is causing the error?
--------------------------------------------------------
#include <windows.h>
#include <userint.h>
typedef long (__cdecl *INITPROC)(void);
typedef long (__cdecl *WRITEPROC)(long nDeviceID, unsigned char *strCommand, long command_length);
typedef long (__cdecl *READPROC)(long nDeviceID, unsigned char *strResponse, long buffer_size, long *response_length);
INITPROC UsbdllInit;
WRITEPROC UsbdllWrite;
READPROC UsbdllRead;
HINSTANCE hinstUsbdll;
int main (int argc, char *argv[]) {
long error;
char pathname [MAX_PATHNAME_LEN];
unsigned char response[256];
long response_length;
// Initialize the CVI Run Time Engine.
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
hinstUsbdll = 0;
// Get windows system directory.
if (GetSystemDirectory (pathname, MAX_PATHNAME_LEN) == FALSE)
strcpy (pathname, "C:\\WINOWS\\system32");
// Create path of the dll.
strcat (pathname, "\\usbdll.dll");
// Load the library module.
hinstUsbdll = LoadLibrary (TEXT(pathname));
// Display error message if loading the module failed.
if (!hinstUsbdll){
MessagePopup ("Error", "Loading usbdll.dll failed!");
return 0;
}
// Get usbdll.dll function addresses.
UsbdllInit = (INITPROC) GetProcAddress(hinstUsbdll, "newp_usb_init_system");
UsbdllWrite = (WRITEPROC) GetProcAddress(hinstUsbdll, "newp_usb_send_ascii");
UsbdllRead = (READPROC) GetProcAddress(hinstUsbdll, "newp_usb_get_ascii");
// Execute newp_usb_init_system function.
error = (UsbdllInit) ();
if (error){
MessagePopup ("Error", "Error while initializing.");
goto Error;
}
// Execute newp_usb_send_ascii function.
error = (UsbdllWrite) (4, "*IDN?\r\n", 7);
if (error){
MessagePopup ("Error", "Error while writing.");
goto Error;
}
// Execute newp_usb_get_ascii function.
error = (UsbdllRead) (4, response, 256, &response_length);
if (error){
MessagePopup ("Error", "Error while writing.");
goto Error;
}
// Free Library if it was loaded successfully.
Error:
if (hinstUsbdll) FreeLibrary (hinstUsbdll);
return 0;
}
--------------------------------------------------------
If you need some more information, please let me know...
06-25-2007 09:13 AM
Where did you get the information:
typedef long (__cdecl *INITPROC)(void);
typedef long (__cdecl *WRITEPROC)(long nDeviceID, unsigned char *strCommand, long command_length);
typedef long (__cdecl *READPROC)(long nDeviceID, unsigned char *strResponse, long buffer_size, long *response_length);
from? It is normal practice to use __stdcall for general-purpose dll functions. Note that it IS possible for the wrong calling convention to appear to work - it all depends on how many parameters are passed and what other stack usage is made after the incorrect call is made.
Try the functions using __stdcall instead of __cdecl.
JR
06-25-2007 09:20 AM
Hello JR,
I got it from this example code when searching for the LoadLibrary function : http://msdn2.microsoft.com/en-us/library/ms686944.aspx
Anyway, your suggestion solves the problem. Thanks a lot ![]()