LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Function call from a DLL

Hello,

is it possible to call a function of my .exe file from a DLL?
For example in my application I have a function 'MainFunc' and in my DLL I have the function 'DLLFunc'. So is it possible that I can call 'MainFunc' from within 'DLLFunc'?

Thanks!
hamsti
0 Kudos
Message 1 of 5
(3,896 Views)
Hi hamsti,

You can do this by passing a function pointer to the function in your DLL. By passing a pointer to a function in your main program to the DLL you will be able to use the function in the DLL. This is similar to how callback functions work.

The attached example demonstrates this.

Scott Y
NI
0 Kudos
Message 2 of 5
(3,878 Views)
Scott, many thanks for the example. I´ve understand the workwise of such function pointers.
But now I have one more (stupid) question: Is there any possibility to realize you sample without passing a function pointer to the DLL e.g. the DLL only knows the name of the function and this is sufficient to call the function in main.
I´m relatively new in working with pointers ;o)

Thanks,
hamsti
0 Kudos
Message 3 of 5
(3,869 Views)
Hi Hamsti,

If the 'MainFunc' function is to be called from different places (I assume it is called in the .exe file) then it would suggest to me that it should be a library file (static or dynamic).

This would, simply, require the creation of a new .dll (As you have previously done).
The the 'MainFunc' function can then be extracted from the .exe's .c file and added top the new .dll file.
Adding a reference to the new .dll file in both 'MainFunc' and your current .dll would allow both to just "know" the name of the function.

If 'MainFunc' is relatively small then this may seem like overkill. However, I am sure that, as the project grows, you will come across other common code "Utilities" that could also be added to your new "utility.dll".
0 Kudos
Message 4 of 5
(3,829 Views)
First of all, great sample code.  I have no problems running it.
 
I'm also trying to implement access to a function through a pointer passed to a DLL.  The main difference I have is that the function call occurs in my Interrupt Service Routine.  A portion of the ISR runs in the DLL, then it jumps out to the function in the DLL user program.  After I return to the DLL, it crashes upon exit from the DLL ISR function.  I've noticed that the stack only has two functions in it and possibly no link to main; therefore, causing the crash. 
 
Implementation of the ISR is through a lower level function from a licensed driver (PCI) development library.  I can't rule out that I'm not implementing the function correctly in the program (Labwindows/CVI).  Particularly so because of the comment in cvidll.h from the sample provided:
 
"Note that we explicitly specify a calling convention for our functions.  This is necessary to ensure that caller and callee agree on stack responsibilities.  You should always sepcify a calling convention explicitly when prototyping DLL functions."
 
Since reading the comment, I've added '__stdcall' before all the functions included in my *dll.h.  I don't know that I really know which calling convention to use.  My main concern is that I attempting to implement this through a DLL for the first time.  I have used older versions of this PCI driver library with Visual C++ and LabWindows/CVI version 6.x and had no problems. 
 
Any advise would be greatly appreciated.
 
 
0 Kudos
Message 5 of 5
(3,719 Views)