LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in calling LabVIEW DLL with C#

Solved!
Go to solution

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.

Download All
0 Kudos
Message 1 of 13
(7,636 Views)

Dear NI team,

 

Can you guys help?

I'm stucked in this issue for days and still can't get this solved.

😞

0 Kudos
Message 2 of 13
(7,607 Views)
Solution
Accepted by topic author brock85

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.

 

Rolf Kalbermatter
My Blog
Message 3 of 13
(7,582 Views)

Hi 

0 Kudos
Message 4 of 13
(7,564 Views)

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

Download All
Message 5 of 13
(7,496 Views)

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.

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 13
(7,482 Views)
Thanks,
I have sane issue now.
0 Kudos
Message 7 of 13
(7,377 Views)
Hi MieElec,
use you dll is working, but it not working use my dll, can you tell me which version are you use?
Br
Boone
0 Kudos
Message 8 of 13
(7,360 Views)
is it 8.2?
0 Kudos
Message 9 of 13
(7,335 Views)

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

 

0 Kudos
Message 10 of 13
(7,325 Views)