Discussion:
Pointer to a function (C .dll files)
(too old to reply)
Ken Kearney
2006-01-27 12:12:54 UTC
Permalink
Hi,
 
We have a .c dll file that we are trying to use in Labview to talk to a USB transceiver. What happens is that when we try to configure the device we call c functions using the "call library function node" utility in Labview i.e. void open_channel(char channel_number) etc.
 
At the start of the code, we need to send the .dll this function:

void AssignResponseFunction(RESPONSE_FUNC pfResponse, UCHAR *pucResponseBuffer); where RESPONSE_FUNC is a pointer to the function that will be called whenever a response / event message is received from the module.
When the USB device has a message that it wants to make available to us, it uses the pointer pfResponse to call a function in memory that will process the data that has been stored in pucResponseBuffer and make it available. Is this possible in Labview and how would I even go about doing such a thing?
 
Ken
Jason S
2006-01-27 15:43:47 UTC
Permalink
It is possible to do what you are asking, but it's not the easiest
thing in the world to do, and it can't be done entirely in LabVIEW. The
function that receives the message must be written in C (or some other
language) and compiled in a DLL. (I suppose as of 7.0 one could
actually make a DLL from LabVIEW code for these purposes, but I've
never tried it.) Then, you'll need to ensure that the DLL is loaded
into memory and get the function pointer. Using calls to WinAPI
functions (assuming you're on Windows, that is):


- Load the library containing the callback function in LabVIEW with a DLL call to <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp" target="_blank">LoadLibraryA</a>


- Get the function pointer with <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp" target="_blank">GetProcAddressA</a> and pass it to your library call


- When the program is complete free the library with <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/freelibrary.asp" target="_blank">FreeLibrary</a>



Now, regarding the implementation of the callback itself, you have two choices:


- Completely handle the event inside the callback function


- Use an occurrence or LabVIEW event to notify LabVIEW code that the event has occurred and action should be taken.



The first is generally easier, but not always practical. For instance,
suppose that you just wanted to log the data to a file. You could pass
the file handle through the pucResponseBuffer parameter (I am assuming
that this is just a user parameter that is going to get passed to your
callback) and then use LabVIEW File Manager calls to perform the write.



If, however, you need to have further action taken by your VI in
response to the message, you will need to go with the second option.
For this, I refer you to the <a href="http://sine.ni.com/apps/we/niepd_web_display.display_epd4?p_guid=B45EACE3E7AC56A4E034080020E74861" target="_blank">Windows Message Queue</a>
set of VIs and, more specifically, the DLL code. In this example, a
callback function is registered with the call to SetWindowsHookEx .(The
call is made from within the DLL code rather than from the VI, but that
difference is not important.) The callback function stores the data and
fires a LabVIEW occurrence, notifying the VI to grab the data from the
the DLL storage.



One thing to note about this code: it is no longer necessary to
manually find the Occur function, as is done in the DllMain function as
it is now listed in extcode.h.

Best Regards
Jason

Continue reading on narkive:
Loading...