Valhalla Legends Forums Archive | Visual Basic Programming | C++ DLL Returning a String to VB

AuthorMessageTime
gameschild
Created the following code in C++:

char* __stdcall getArrayFolderPath(int pos)
{
return Folder[pos].Folder_Path;
}

And in Visual Basic declared the DLL as:

Public Declare Function getArrayFolderPath Lib "DirXCore.dll" (ByVal pos As Long) As String

However the string returned is empty. If i change "As String" to "As Byte" i get a number but it two digits, maybe a single char?

Anyone know how i can make the C++ DLL return the string to VB?
November 30, 2003, 7:53 PM
K
you will need to pass an empty string as a parameter and copy the c string into it. Something like:

[code]
int __stdcall getArrayFolderPath(char* lpszBuffer, int nBuffLen, int pos)
{
int len = strlen(Folder[pos].Folder_Path);
if (nBuffLen < len)
return -1; // Error: not enough space in buffer

strcpy(lpszBuffer, Folder[pos].Folder_Path);

return len;
}
[/code]

Although you might run into trouble since VB uses BSTRs.

[code]
Public Declare Sub getArrayFolderPath Lib "DirXCore.dll" (ByVal Buffer As String, ByVal BufferLength As Long, ByVal pos As Long) As Long

// I don't really know VB.
Dim myStr As String
Dim strLen as Long

myStr = Space(255)
strLen = getArrayFolderPath(myStr, 255, pos)
If strLen == -1 Then
// More than 255 characters in string
[/code]
November 30, 2003, 8:02 PM
gameschild
Dim myStr As String
Dim strLen As Long
myStr = Space(255)
strLen = getArrayFolderPath(myStr, 255, pos)
List1.AddItem Left(myStr, strLen)
If strLen = -1 Then
msgbox "More than 255 Chars"

And it works brilliantly, thanks a lot!
November 30, 2003, 8:12 PM
UserLoser.
[quote author=K link=board=31;threadid=3973;start=0#msg32704 date=1070222540]
// I don't really know VB.
[/quote]

You really show it :P VB uses ' or REM for comments

November 30, 2003, 8:21 PM
K
[quote author=UserLoser. link=board=31;threadid=3973;start=0#msg32710 date=1070223706]
[quote author=K link=board=31;threadid=3973;start=0#msg32704 date=1070222540]
// I don't really know VB.
[/quote]
You really show it :P VB uses ' or REM for comments
[/quote]

I know. I like // better. Same with the == in the If statement. ;D
November 30, 2003, 9:36 PM
hismajesty
[quote author=K link=board=31;threadid=3973;start=0#msg32734 date=1070228217]
[quote author=UserLoser. link=board=31;threadid=3973;start=0#msg32710 date=1070223706]
[quote author=K link=board=31;threadid=3973;start=0#msg32704 date=1070222540]
// I don't really know VB.
[/quote]
You really show it :P VB uses ' or REM for comments
[/quote]

I know. I like // better. Same with the == in the If statement. ;D
[/quote]

My friend does ' // for his VB comments.
November 30, 2003, 11:06 PM
Skywing
Have you tried using SysAllocString or SysAllocStringByteLen to allocate the string returned? Perhaps one of those might work.
December 1, 2003, 5:41 AM

Search