11-19-2013 04:41 AM
Dear All,
I compiled a few DLLs in LabVIEW and called these DLLs using C#.
I'm having difficulty in passing in strings and arrays as input and output of the DLL.
Whenever i try to show the string i input in a dialog box(This part is written in LabVIEW DLL), a LabVIEW message dialog would appear and the program just stucked there.
Whenever i try to input a string or an array to the DLLs, Microsoft Visual Studio would display "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I attach the vi's and also the C# .sln file.
Can you guys please advise where did i go wrong?
Thank you very much.
Solved! Go to Solution.
11-20-2013 01:00 AM
Dear NI team,
Can you guys help?
I'm stucked in this issue for days and still can't get this solved.
😞
11-20-2013 03:41 PM - edited 11-20-2013 03:42 PM
You need to preallocate output arrays and strings!
private void button1_Click(object sender, EventArgs e)
{
string output;
int[] value;
StringOutput("A", "B", out output, 3, out value, 4);
}
}
Your output and value variable need to be preallocated to provide storage for 3 resp. 4 elements. And since you want to receive a NULL terminated C string you need to increase this to 4 characters.
Also string in C# is an object not a C string pointer. You need to declare the StringOutput() function as follows:
[DllImport("SharedLib.dll",CallingConvention=CallingConvention.StdCall, Charset=CharSet.Ansi]
internal static extern int StringOutput(StringBuilder A, StringBuilder
B, out StringBuilder
StringOutput, int sizeString, out int[] output, int sizeArray);
Then call it similar to this:
StringBuilder output = new StringBuilder(4);
int[4] array = new int[4];
StringOutput("A", "B", output, output.Capacity, value, value.Length);
Please note, I'm not a pro in C# and usually don't do much in it. Also this code has not been tested nor compiled at all, so likely contains some errors. It does however point you in the direction of where to look further.
What you need to understand is that the C DLL that you have created in LabVIEW has a so called unmanaged interface. This means C# is not able to know how to manage memory for the parameters at all and you have to do everything yourself, hence the requirement to explicitedly initialize the output and array variables with a preallocated memory block.
11-20-2013 08:46 PM
Hi rolfk,
Thank you very much. It is working now.
In fact i found an even better way of doing this.
Since i'm the one who compile and LabVIEW DLL and call it out in c#, I can actually select the "build .NET assemblies" in the project files when copiling the DLL.
In this case, the DLL i'm compiling can be added easily to my c# project as a reference or another namespace.
Another cool feature i found in LabVIEW!!
12-02-2013 06:57 AM
I need to implement the function: void setCallback (pfnExtIOCallback funcptr);
/ / Void extIOCallback (int cnt, int status, float IQoffs, IQdata short []) do not know how to do this with Call Library Function Node.
Adjunct library and the DLL file. H.
Someone can help me?
Best regards
Manel
12-02-2013 02:28 PM
First, you should not hijack this thread for your highly unrelated problem.
Second, Callback functions are not something you can do natively with the Call Library Node. The most easiest solution is to write an external shared library in C that wraps your API function and provides the necessary callback function. This callback function then translates the even into a user event or occurrence which are the LabVIEW equivalents for callback functions. Look for PostLVUserEvent() here on the NI site or over on www.lavag.org to find several discussions about how to use user events to receive the callback events.
01-25-2014 06:24 PM
01-26-2014 10:00 AM
01-27-2014 05:01 AM
01-27-2014 06:50 AM
Hi Boone301 !
The dll library I use, and the header file is in my previous message.
I tried with labview version 11, 12, 13.
Just need you, like I can create a callback.
Thank you