LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing a Shared Variable to a DLL by Reference

Hello all,
   I am writting a LabVIEW interface for DLL that communicates with a xray spectrometer and am having issues figuring out how to get the data out. I don't have access to the DLL itself, but have the c++ header file and I have been able in import all the functions I need.
 
If you look at the attached image of how I have setup the interface, you will see that I have a shared variable named X123 Data Buffer that is set to READ mode and is connected to the Get Dpp Data vi, unfortunately, I need a pointer to this variable to be passed to the DLL, so that the DLL can write to the variable. So that I can then read the X123 variable and plot the data.
 
1) How do I pass this Shared Variable to GetDppData.vi (which will give it to the DLL)?
 
2) Should I be doing this another way? I am new to LabVIEW, as this is my first project, so I don't know if I am going about this correctly.
 
Thanks,
Kenny
0 Kudos
Message 1 of 8
(3,656 Views)
Forgot to attact the screenshot of my VI.
0 Kudos
Message 2 of 8
(3,653 Views)
Also, the prototype for the DLL Function is:
 
long GetDppData(unsigned long objptr, long *DataBuffer);
 
and I know that it is an array of 2049 of these 32bit longs.
0 Kudos
Message 3 of 8
(3,654 Views)
Hi Kenny_Neal,

From the screen shot it appears that you are simply passing data from a shared variable to a subvi. What exactly is inside the Get Dpp Data subvi? Also, when you run the code as is, what is the error code you receive? Please let me know. Thanks!

Best regards,

Steven
0 Kudos
Message 4 of 8
(3,631 Views)
The subVI is simply an interface function to the DLL. See the attached image.
 
I am not in the lab write now where I can run the VI and show you the exact error, but it is something to the effect of "The labVIEW memory space has been corrupted by a external function call, please restart labVIEW."  I assume this is due to my having the shared memory variable being in the read mode and I am passing it to a DLL that expects to be able to write to it as a c++ pointer to an array of 32bit integers.
0 Kudos
Message 5 of 8
(3,619 Views)

Specifically, here is the error message I am recieving:

Error 1097 occurred at Call Library Function Node in DppApi.lvlib:Get Dpp Data.vi->DppApi.lvlib:DppApi.vi

Possible reason(s):

LabVIEW:  An exception occurred within the external code called by a Call Library Function Node. This might have corrupted LabVIEW's memory. Save any work to a new location and restart LabVIEW.

Thanks!

Kenny Neal

0 Kudos
Message 6 of 8
(3,611 Views)
I am by no means an expert on .dll calls, so I can't say for sure whether the dll call is expecting a reference.  But to answer the question reflected by your message subject, I will say the answer is no, you won't be able to pass the shared variable by reference.
 
The reason is that references are pointers to the variable in a particular memory location as opposed to a copy of that variable.  In the case of shared variables, that memory location may not even be on the same computer that is running the code.  It may be part of a shared variable library on another machine.  NI uses special protocols in the background to read and write to that shared variable engine, thus to the programmer, it seems to behave like an ordinary variable.
 
My guess is that you will have to establish a variable on the same machine that is running the .dll call in whatever manner allows it to pass as a reference.  You will need to put some code before and after the call.  That code will retrieve the shared variable value and put it into the local variable to be referenced before the dll call, and then put in code in that copies that variable value back to the shared variable after the dll call.
 
0 Kudos
Message 7 of 8
(3,603 Views)
Thanks for your help Raven's Fan,
   I understand what you are saying about the shared variables... I really don't need a shared variable at all, what I would really prefer to have is a local variable that I can pass in by reference. I know exactly how I would do this is in c/c++ (see code below), but don't really understand how to get it to work in LabVIEW.
 
 
Here's how I would make the call in c++:
 
// Create a local variable which will be populated with data after DLL call
long databuffer[8192];
 
// Make the DLL call, which will populate databuffer
long numChannels = getDPP(myDPPObject, databuffer);
 
// Display each element of the data
for (int i=0;i<8192;i++) { cout << "dataBuffer[" << i << "] = " << databuffer[i] << endl;}
 
// here is the protoptye for the DLL call:
// long GetDppData(unsigned long objptr, long *DataBuffer);
 
 
Thanks for any help!
Kenny Neal
0 Kudos
Message 8 of 8
(3,588 Views)