Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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
0
dhodo
Asked:
dhodo
  • 3
  • 2
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now