Link to home
Start Free TrialLog in
Avatar of CumulusGranitus
CumulusGranitusFlag for Germany

asked on

How to pass chars from unmanaged DLL to VB.NET

I have to read chars in VB.NET from an unmanaged DLL and convert them to a string. How do i pass the chars to VB.NET?
The only example i have is in C++:

API declaration in C++:
   extern unsigned char __stdcall  ReadXX(unsigned char *sn);

access example of the function from C++:
   unsigned char sn[50];
   memset(sn,0,sizeof(sn));
   ReadXX(sn);
Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

StringBuilder should do the trick.
Avatar of CumulusGranitus

ASKER

May be (but how?). Meanwhile i have this solution:

<DllImport("Lib123.dll")> Public Sub ReadXX(ByVal ptr As IntPtr)
End Sub

            Dim ptr As IntPtr
            Dim n As Integer = 50
            ptr = Marshal.AllocHGlobal(n)
            ZeroMemory(ptr, CType(n, IntPtr))

            ReadXX(ptr)

            Marshal.FreeHGlobal(ptr)

It works very well.
Hi

Try:

<DllImport("Lib123.dll")> Public Sub ReadXX(ByVal str As StringBuilder)

Usage:

Dim str as New StringBuilder();
ReadXX(str)
Console.WriteLine(str.ToString)
Thanks, i will try this good looking solution on Monday.
ASKER CERTIFIED SOLUTION
Avatar of louisfr
louisfr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
both solutions are working.
Thanks for the hint.