LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

nicaiu.dll function pointer crashes

Hi,

I'm trying to dynamically load the NIDAQmx library, but yy program is crashing when the dll function gets called.  There doesn't seem to be an error when calling LoadLibrary("nicaiu.dll") or when getting the function pointer from GetProcAddress().  The code dies when the function actually gets called at nirc = (dllFunction)(adr);.

The error is "MFC Application has encountered a problem and needs to close.  We are sorry for the inconvenience."

I have another Labview generated dll that is loaded the same way and works so I'm not sure what I'm doing wrong.  I am new to dynamically loading function pointers, so any advice is appreciated.

I'm running Visual C++, with NIDAQmx version 8.5.0f3.

..
    // try loading the NIDAQmx dll
    NIDAQmxLib = LoadLibrary("nicaiu.dll");
    if (!NIDAQmxLib) {
        CLogFile log(LOGFILE);
        log.WriteLogDT("Error loading nicaiu.dll");
        return TAPE_ERROR;
    }

    log.WriteLogDT("nicaiu.dll loaded");

    // get the library function
    typedef int (*DLLFUNCTION)(const char deviceName[]);
    DLLFUNCTION dllFunction=NULL;
    dllFunction = (DLLFUNCTION)GetProcAddress(NIDAQmxLib, "DAQmxResetDevice");
    if (dllFunction == NULL) {
        log.WriteLogDT("Error loading DAQmxResetDevice() from NIDAQmxLib");
        return TAPE_ERROR;
    }

    strcpy(adr, GetAddress());

    sprintf(buff, "adr=%s", adr);
    log.WriteLog(buff);

    nirc = (dllFunction)(adr);                // seems to crash here!
    //long nirc = DAQmxResetDevice(adr);
    if (nirc != 0) {
        sprintf(buff, "DAQmxResetDevice() address=%s nirc=%d", adr, nirc);
        log.WriteLogDT(buff);
        DAQmxGetErrorString(nirc, buff, LOCAL_LINE_LEN);
        log.WriteLogDT(buff);
        rc = TAPE_ERROR;
    } else rc = TAPE_SUCCESS;

    FreeLibrary(NIDAQmxLib);
...

Thanks!

Nia
..
0 Kudos
Message 1 of 3
(3,577 Views)
Hello Nia,

I have a few quick questions for you:
1) did you try calling any other functions and do they work?
2) what device name are you passing in the string parameter into the DAQmxResetDevice function?
3) could you link your code against the NI-DAQmx static library, thus eliminating the need to load the library and entry points yourself?
4) is the crash address point within the NICAIU.DLL? If so, what's the crash address and library address? If not, which module does the address fit in?

Best Regards
LDP
0 Kudos
Message 2 of 3
(3,572 Views)
Hi LDP,
 
Thanks so much for your comments.  I looked around some more and if I add "_stdcall" to the function pointer definition, it works! 
 
     typedef int (_stdcall *DLLFUNCTION)(const char deviceName[]);
 
Admittedly I don't understand why, but the program doesn't crash with the calls to both DAQmxGetDevProductType() and DAQmxResetDevice().  So I'm going to go through and see if I can use function pointers for the rest of the DAQmx functions and hopefully it will work...I was also having trouble converting my NIDSA code, so hopefully this will solve that problem, too....
 
But to answer your questions --
1) I also tried calling DAQmxGetDevProductType and the same type of behavior occurs.
2) I'm passing "Dev1".  This matches the name configured in Measurement & Automation.
3) If I link statically to the library it works.  But I'm trying to support 2 different devices - NI4551 and NI4461 -- that use different libraries (NIDSA/traditional NIDAQ and NIDAQmx) in the same application.  Only 1 device type would be used on a single PC and the device type would be in a configuration file.  I would like not to require the NIDSA, traditional NIDAQ, and NIDAQmx to be installed when not all are used.
4) The debug error was :
Debug Error!
Program c:\eng\twang
Module:
File :i386/chkesp.c
Line: 42
The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention
 
 
Thanks,
Nia


Message Edited by NF1 on 06-09-2008 06:18 PM
0 Kudos
Message 3 of 3
(3,559 Views)