|
-
Nov 17th, 2001, 04:32 PM
#1
Thread Starter
New Member
Howto return a String from c++ to vb
Hi,
I've tryed to return a string from my own made dll to visual basic but i'm unsuccesful doing this.
Does anybody know how to do this?
This is my code(this does'nt work!)
void DBSOCK_API test(LPSTR returnStr){
char buffer[]="1234 testing";
strcpy(returnStr,buffer);
}
and in visual basic:
Declare Sub test Lib "dbSock" (ByVal st As String)
Private Sub Command1_Click()
Dim myString
test (myString)
MsgBox myString
End Sub
The message box displayed nothing
Does anybody know what I'm doing wrong
note that I'm using Microsoft eMbedded Visual Tools.
-
Nov 17th, 2001, 05:10 PM
#2
Thread Starter
New Member
Anybody alive here?
maybe i'll try to order some pizza. That always helps
-
Nov 17th, 2001, 05:59 PM
#3
I dont actually know the answer, but I can give you some info to perhaps help you out.
VB strings are Unicode, which means that each character uses two bytes. The C++ equivalent is a BSTR.
You can typecast to to an LPSTR from a BSTR to get standard 1 byte characters.
Z.
-
Nov 17th, 2001, 06:17 PM
#4
Monday Morning Lunatic
Actually, BSTR is nominally for OLE. For normal C++, you want LPWSTR, or in real terms, wchar_t* (as opposed to char*).
www.parksie.uklinux.net/StringDLL.zip (I think it's still there).
VB will pass an ANSI string to a DLL (or a pointer to one if you're filling it from the DLL), unless you specifically convert it to Unicode using StrConv(..., vbUnicode, ...).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 17th, 2001, 06:35 PM
#5
::is enlightened:: Thanks parksie (again =).
Z.
-
Nov 17th, 2001, 06:51 PM
#6
Thread Starter
New Member
Some good advice, thank you
I've got some something working. If you send a pointer to an array of unsigned shorts it works. This function does the trick:
<code>
void charToShort (unsigned short *outStr,char *inStr,int strLen)
{
int counter = 0;
for(counter=0;counter < strLen;counter++)
{
outStr[counter] = inStr[counter];
}
//terminate the string with a 0
outStr[counter]=0;
}
unsigned short DBSOCK_API *test (void){
unsigned short tmpBuffer[255];
charToShort(tmpBuffer,"1234",4);
return tmpBuffer;
}
</code>
Last edited by Chris_CJM; Nov 17th, 2001 at 06:54 PM.
-
Nov 17th, 2001, 06:55 PM
#7
Monday Morning Lunatic
If you want to convert char* to wchar_t*, then use the MultiByteToWideChar function from the Windows API, or wcsmbcs (it's something like that, I can't remember) from the C standard library.
Don't just try and convert the number values.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|