LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW exception on 'shutdown' - unloading/cleanup of wrapper DLL

Solved!
Go to solution

Ok, I want to check that my thinking on this is correct:

 

We've created a wrapper DLL in VC++ that exposes some functions from a third party API/DLL. This works fine - there's an initialise, get data and close VI that use the CLFN . There is one small problem with the wrapper though - if the 'close' VI isn't called (which calls the API's close function) such as if we have to abort the application during development/debugging then LabVIEW crashes with an exception.

 

I suspect that it's because the 'close' function from the API never gets called so it never cleans up when execution stops. Is that right?

 

Is the solution to modify the wrapper DLL DllMain() to have the following and handle the DLL_PROCESS_DETACH by calling the API's close function? Is there anything more that I need to be aware of?

 

BOOL WINAPI DllMain(  
         HINSTANCEhinstDLL,  // handle to DLL module
         DWORD fdwReason,     // reason for calling function
         LPVOID lpReserved )  // reserved
{
    // Perform actions based on the reason for calling.
    switch( fdwReason )
    {
    case DLL_PROCESS_ATTACH:
        // Initialize once for each new process.
        // Return FALSE to fail DLL load.            
        break;

    case DLL_THREAD_ATTACH:        
        // Do thread-specific initialization.
        break;        
   
    case DLL_THREAD_DETACH:
        // Do thread-specific cleanup.            
        break;
   
    case DLL_PROCESS_DETACH:        
        // Perform any necessary cleanup.
        break;    
    }
        return TRUE;
}

 

The original DllMain looked like this:

BOOL APIENTRY DllMain( HANDLE hModule,
                        DWORD  ul_reason_for_call,
                        LPVOID lpReserved )
{
    return TRUE;
}

(Taken from here: http://www.ni.com/white-paper/3056/en/)

 


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 1 of 2
(2,686 Views)
Solution
Accepted by topic author Sam_Sharp

Stupidly, because I was having problems with Visual Studio I couldn't try it at the time of posting but changing the DllMain function worked and we no longer get the LabVIEW exception on exit.


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 2 of 2
(2,668 Views)