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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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. HansenCEOCommented:
StringBuilder should do the trick.
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.
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)
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

CumulusGranitusAuthor Commented:
Thanks, i will try this good looking solution on Monday.
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

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.
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.