CString to BSTR question dealing with nul characters




I'm creating a C++ DLL for a VB application. I'm using VC7 for creating
the DLL application. I'm trying to return a string from this function,
here is a simple example

extern "C" DLL_API int __stdcall GetString(BSTR* myStr)
{
int rtnCode = PASS; // PASS is defined as 1 somewhere else

CString temp = "Hello";

*str = temp.AllocSysString();

return rtnCode;
}

>>From my test VB 6 app I declare
Private Declare Function GetString GetStr "My.dll" (ByRef myStr As
String,) As Integer

Dim tempStr as String
Dim rtnCode as Integer

rtnCode = GetString(tempStr)


I call the this funciton from my VB 6 application and passes
successfully. BUT I'm getting extra nul characters.

I converted tempStr into a Byte array to check out the individual
bytes. Dim b() as Byte
b = tempStr

I got the following
b(0) = 72
b(1) = 0
b(2) = 0
b(3) = 0
b(4) = 101
b(5) = 0
b(6) = 0
b(7) = 0
b(8) = 108
b(9) = 0
b(10) = 0
b(11) = 0
b(12) = 108
b(13) = 0
b(14) = 0
b(15) = 0
b(16) = 111
b(17) = 0
b(18) = 0
b(19) = 0

While if I declare
Dim anotherStr as String
anotherStr = "Hello"
b = anotherStr

The byte array for this is
b(0) = 72
b(1) = 0
b(2) = 101
b(3) = 0
b(4) = 108
b(5) = 0
b(6) = 108
b(7) = 0
b(8) = 111
b(9) = 0

So am kinda of confused the best way to convert a CString to a BSTR.
Thanks for any help or information.

.



Relevant Pages

  • Re: Explicit Linking of DLLs in VB.net (attn Michel or any others)
    ... loads the dll and finds a form in the dll. ... Dim extForm As Form = extAssembly.CreateInstance("test.entry", ... Wel implement propertys, methods, events in your interface and start ... Public Shared Function LoadMeByName(ByVal vstrAssemblyName As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Auto Write Name and Merge across
    ... Dim Sheetname01 As String ... Dim WeekName01 As String ...
    (microsoft.public.excel.misc)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)

Loading