Avatar of mark4262
mark4262 asked on

Using LabView.dll in c# , cannot get string value from dll

I am trying to use a dll in my c# code that was generated by labview ...
The method returns version information for the dll
I get back values for everything but the string (addlInfo)
I know on the labview side that the string is 18 characters long.
When I make the call, addlInfoLen changes from 20 to 18, but I get an empty string for addlInfo???
PS - Also when, i try to relase the pointer, I get an "Invalid pointer" error.

From LabVIew:
void __cdecl esrInfo(float *verNo, long *buildYear, long *buildMonth, long *buildDay, char addlInfo[], long *addlInfoLen);

From C#
Use dllimport
 [DllImport("D:\\Vault\\Projects\\Covaris\\Documents\\sonolabeseries\\sonolabeseries.dll",
       EntryPoint = "esrInfo",
       ExactSpelling = true,
       CharSet = CharSet.Ansi,
       CallingConvention = CallingConvention.Cdecl)
       ]

        public static extern /*unsafe*/ void esrInfo(
             out float verNo,
             out Int32 buildYear,
             out Int32 buildMonth,
             out Int32 buildDay,
             out IntPtr addlInfo,
             out Int32 addLength);

Call it
  public string GetVersion()
        {
            //IntPtr ptr = new IntPtr();
            System.Single verNo = 1.0F;
            Int32 buildYear = 0;
            Int32 buildMonth = 0;
            Int32 buildDay = 0;
            IntPtr addlInfo = Marshal.AllocHGlobal(20);
            Int32 addLength = 20;
           
            IntPtr newPointer = IntPtr.Zero;

            addlInfo = IntPtr.Zero;
            //string temp = "";
            try
            {
                esrInfo(out verNo,
                     out buildYear,
                     out buildMonth,
                     out buildDay,
                     out addlInfo,
                     out addLength);

                Marshal.ReAllocHGlobal(addlInfo, newPointer);
               StringBuilder addInfo = new StringBuilder( Marshal.PtrToStringAnsi(newPointer));

               Marshal.FreeHGlobal(newPointer);

            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message.ToString());
            }
            //addnInfo = ptr.ToString();

            //temp = addnInfo.ToString();
            if (verNo > 0.0) return string.Format("Version: {0} Build(YYYYMMDD):{1}{2}{3}",verNo.ToString(), buildYear.ToString(), buildMonth.ToString(),buildDay.ToString());
            return "0.0";

        }
     


C#

Avatar of undefined
Last Comment
mark4262

8/22/2022 - Mon
Babycorn-Starfish

Hi,

are you sure IntPtr is the correct corresponding type to be passed here?
ASKER
mark4262

No, I don't ... but I know on the other side, that the dll is expecting a pointer to an array. I have to pass that pointer and the size of the array.

I have gotten some very nasty errors, trying char[] , string, stringbuilder ... the IntPtr is the only thing that will execute without error. Since the size of the array is coming back correctly, modified to the correct length, I think I am using the correct approach.

ASKER CERTIFIED SOLUTION
Babycorn-Starfish

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
mark4262

Here is what the code looked like after.
I also had to add an actual value to the string before making the call. In this case I added 20 spaces. If
you don't add a value to it, you will still get nothing back.

public string GetVersion()
        {
            //IntPtr ptr = new IntPtr();
            System.Single verNo = 1.0F;
            Int32 buildYear = 0;
            Int32 buildMonth = 0;
            Int32 buildDay = 0;
            //IntPtr addlInfo = Marshal.AllocHGlobal(20);
            //Int32 addLength = 20;
            StringBuilder addlInfo = new StringBuilder(20);
            addlInfo.Append("                    ");
            Int32 addLength = addlInfo.Length;
            //addlInfo = IntPtr.Zero;
           
            try
            {
                esrInfo(out verNo,
                     out buildYear,
                     out buildMonth,
                     out buildDay,
                     addlInfo,
                     out addLength);

              //StringBuilder addInfo = new StringBuilder( Marshal.PtrToStringAnsi(addlInfo));


              Debug.Print(addlInfo.ToString());

             //Marshal.FreeHGlobal(addlInfo);

            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message.ToString());
            }
            //addnInfo = ptr.ToString();

            //temp = addnInfo.ToString();
            if (verNo > 0.0) return string.Format("Version: {0} Build(YYYYMMDD):{1}{2}{3}",verNo.ToString(), buildYear.ToString(), buildMonth.ToString(),buildDay.ToString());
            return "0.0";

        }
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck