LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Pointer to a function (C .dll files)

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
0 Kudos
Message 1 of 5
(4,076 Views)

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):

  1. Load the library containing the callback function in LabVIEW with a DLL call to LoadLibraryA
  2. Get the function pointer with GetProcAddressA and pass it to your library call
  3. When the program is complete free the library with FreeLibrary

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

  1. Completely handle the event inside the callback function
  2. 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 Windows Message Queue 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

Message 2 of 5
(4,051 Views)
hi,
 
first of all, thanx for the advice.
 
i tried to write the dll, that passes the procaddress to the funtion that needs it. i made a dll with the appbuilder that matches the prototype of the callbackfunctio (expet that the returnvalue isnt bool but an uint).
 
the next thing i have done is to write a dll in visual c++. as i am no programmer at all, i tried to get this done with some books and the internet, but i fail.
 
i wrote following code:
 

#include

"stdafx.h"

#include

<windows.h>

HINSTANCE h = 0;

const

char* lib ="MeineDLL.dll";

const

char* proc="DLL Funktion";

extern

"C" __declspec(dllexport) int myDLLload(LPCWSTR lib)

{

if(h == 0)

{

LPCWSTR lib = libinput;

h = LoadLibrary(lib);

return 0; // ok

}

return 1; // error

}

 

 

// übergib addr in LabView der anderen DLL-Funktion

extern "C" __declspec(dllexport) int myDLLgetprocaddr(LPCSTR proc, int* addr)

{

if(h != 0)

{

*addr = (

int)GetProcAddress(h, proc);

return *addr != 0; // ok

}

return 1; // error

}

extern "C" __declspec(dllexport) void myDLLFree()

{

FreeLibrary(h);

}

the problem is, that the myDLLload function returns 0 all the time, no mather how i define "lib". and i think that is the reason why the myDLLgetprocaddress funktion fails to deliver me the address...

i know that you surely know much more about visual c++ than i do, so i would be very thankfull for any advice how i can solve this problem.

 

thank you a lot

martin

 

0 Kudos
Message 3 of 5
(3,696 Views)

Hi Martin,

There is a Microsoft example on how to implement the LoadLibrary function at this link. I would try to modified your code, specifically the datatype of your lib variable, to match their implementation and see if this solves your issue.

Cheers,

Jonah
Applications Engineer
National Instruments

Jonah Paul
Marketing Manager, NI Software
0 Kudos
Message 4 of 5
(3,671 Views)
duplicate post :
http://forums.ni.com/ni/board/message?board.id=170&thread.id=347882


0 Kudos
Message 5 of 5
(3,652 Views)