We help IT Professionals succeed at work.
Get Started

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

mark4262
mark4262 asked
on
2,060 Views
Last Modified: 2008-01-09
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";

        }
     


Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE