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);
CumulusGranitusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David L. HansenProgrammer AnalystCommented:
StringBuilder should do the trick.
0
CumulusGranitusAuthor Commented:
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.
0
iHadiCommented:
Hi

Try:

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

Usage:

Dim str as New StringBuilder();
ReadXX(str)
Console.WriteLine(str.ToString)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

CumulusGranitusAuthor Commented:
Thanks, i will try this good looking solution on Monday.
0
louisfrCommented:
You'll need to set the Capacity property of the StringBuilder.
Dim str as New StringBuilder(50);
ReadXX(str)
Console.WriteLine(str.ToString)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CumulusGranitusAuthor Commented:
both solutions are working.
Thanks for the hint.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.