Passing strings between c++ and vb.net

I have a c++ dll and have a function that returns a string.  I want to be able to use this function in VB.NET  In VB6 the BSTR type in c++ was the same as a vb string, but now that vb.net uses string objects, I'm not sure how to do this.  Probably not a hard question, but I really need to figure this out today so 500 points.
Thanks,
David
LVL 1
dhodoAsked:
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.

pbocanegraCommented:
Ctype(variable,string)
0
dhodoAuthor Commented:
Ok.  Could you elaborate a little more?  I have a function in c++ that returns a BSTR.  I declare it in VB.net with:

Declare Auto Function GetSegwayPosition Lib "H:\Documents\Visual Studio Projects\SegwayRMP\SegwayControl\Debug\SegwayControl.dll" () As String

then I try to executre the follow code:

Dim test As String
test = CType(GetSegwayPosition(), String)
MsgBox(test)

The c++ function should return "Hello".  The message box displays two squares and an "o", so it looks like it kind of works, but I still have some type issues.


0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

dhodoAuthor Commented:
The first and last articles don't really have anything to do with what I'm trying to do I don't think.  The BSTR type is not the same as the data type used by VB.net for strings, correct?  I tried this code :

   Declare Ansi Function MyFunc Lib "MyDll" (ByVal MyString As String) As Integer   ' Ansi is the key
;)
   ...
        Dim MyString as String
        MyString = Space(20)      ' Allocate space needed
        MyFunc(MyString)
    ...
----------------
    ' C code
int __declspec(dllexport) MyFunc(char *MyChar)
{
    ...
}

that I found on the middle article, but can't get it to work either.  The string is not modified in the c++ code.

Any other suggestions?

0
Bob LearnedCommented:
Try using the System.Runtime.InteropServices.MarshalAs attribute:

   <MarshalAs(UnmanagedType.BStr)>

The code would be:

   Private Declare Ansi Function MyFunc Lib "MyDll" (ByVal <MarshalAs(UnmanagedType.BStr)>MyString As String) As Integer

Bob
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
dhodoAuthor Commented:
Could not get this to work:

Private Declare Ansi Function MyFunc Lib "MyDll" (ByVal <MarshalAs(UnmanagedType.BStr)>MyString As String) As Integer

but using MarshalAs did work.  Thanks a lot.  Here is my working code for anyone else that's interested.

c++ code:
BSTR GetString()
{
      return SysAllocString(L"Hello");
}

vb.net code:
Declare Ansi Function GetString Lib "SegControlDLLWrapper.dll" () As <MarshalAs(UnmanagedType.BStr)> String

msgbox(GetString()) - displays "Hello"

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.