04-27-2009 08:26 AM
Hello Guys,
I'm using NIDAQmx 8.5 version and trying to test the functionality of a DAC and ADC instrument. A win32 dll by name "SampleDAQmxDLL" has been created and used to wrap the NIDAQmx apis in different functions of the dll. The functions in the dll will be invoked from a separate win32 console application "DAQmxAPITesting" that uses the dll "SampleDAQmxDLL".
The dll has been implemented in such a way that the DAQmx task creations will be done in the "DLL_PROCESS_ATTACH" and clearing of tasks will be done in the "DLL_PROCESS_DETACH" of the DllMain function. The following is the code snippet of the dll,
TaskHandle hTask = NULL; // This is a global variable declared in the dll.
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
createTask();
break;
}
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
{
clearTask();
break;
}
}
return TRUE;
}
PVOID createTask()
{
char error[2048] = {'\0'};
int status = 0;
status = DAQmxCreateTask("", &hTask);
return((void *)DAQmxGetErrorString(status, error, sizeof(error)));
}
PVOID clearTask()
{
char error[2048] = {'\0'};
int status = 0;
status = DAQmxStopTask(hTask);
if(!status)
status = DAQmxClearTask(hTask);
return((void *)DAQmxGetErrorString(status, error, sizeof(error)));
}
The application "DAQmxAPITesting" statically links the dll and when I debug the application by stepping, a break point is set at "DLL_PROCESS_ATTACH" and the execution stops at the breakpoint. Next, when I execute the creatTask() function, I see that the "DAQmxCreateTask" api hanging. The same is the behavior for the "clearTask" while the application is exit.
When I have the task creation and task clear apis in any other function than the "DLL_PROCESS_ATTACH" and "DLL_PROCESS_DETACH", the apis work fine.
My actual application design requires the above design of creating and clearing tasks at DLL entry and exit points. Can anyone help me on this?
Thanks and Regards,
Banukumar M
04-27-2009 03:53 PM
Hi Banukumar,
It sounds like you're running into a deadlock with the Windows DLL loader lock. Many DAQmx API entrypoints have the potential to load additional DLLs (if they have not been loaded already), so you should restructure your code to avoid calling into DAQmx from DllMain(). (Note that this also affects constructors and destructors for global/static objects if you are using C++.) The MSDN article on DllMain() has more information on the restrictions you must follow when implementing DllMain().
Brad
04-28-2009 05:45 AM
Hi Brad,
Thanks for the quick reply.
As you mentioned, we may be running into a deadlock with the Windows DLL loader lock but the same concept worked fine when traditional apis were used. Let me give you some more details on what I'm actually trying to achieve at the DLL_PROCESS_ATTACH,
1. I have my dll which uses the traditional apis in the DLL_PROCESS_ATTACH. The traditional apis in the dllmain is used to write and read some high/low values from the digital port. I have been able to successfully write or read any digital values using the traditional apis like DIG_Out_Prt, DIG_In_Prt from DLL_PROCESS_ATTACH.
2. Now, I want to migrate from Traditional to NIDAQmx driver platform and hence converting my dll to use DAQmx apis instead of traditional apis. To achieve the digital write/read functionality as in Step-1 using the DAQmx apis, we need to create the task handles first and then use the corresponding task handle to perform a write or read operations using DAQmxWriteDigitalLines and DAQmxReadDigitalLines.
3. As a first step, I just introduced the "DAQmxCreateTask" api alone in the DLL_PROCESS_ATTACH to check whether the handles are created successfully. The DAQmxCreateTask api hangs when my dll is loaded which in turn executes the DAQmxCreateTask from the DLL_PROCESS_ATTACH.
The above way of performing the digital operations is required in my application design. So, please let me know if there is a way to perform the digital write/read from the DLL_PROCESS_ATTACH of a win32 dll. If so, please send me some code snippet for the same.
Thanks and Regards,
Banukumar
04-28-2009 10:36 AM
Unfortunately, as Brad stated earlier, DllMain() has severe limitations on what you can do in it. You will need to restructure your code to perform your DAQ task outside of DllMain().