• Welcome to Valhalla Legends Archive.
 

C++ DLL Returning a String to VB

Started by gameschild, November 30, 2003, 01:53 PM

Previous topic - Next topic

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?

K

#1
you will need to pass an empty string as a parameter and copy the c string into it. Something like:


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


Although you might run into trouble since VB uses BSTRs.


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

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!

UserLoser.

Quote from: K on November 30, 2003, 02:02 PM
// I don't really know VB.

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


K

Quote from: UserLoser. on November 30, 2003, 02:21 PM
Quote from: K on November 30, 2003, 02:02 PM
// I don't really know VB.
You really show it :P  VB uses ' or REM for comments

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

hismajesty

Quote from: K on November 30, 2003, 03:36 PM
Quote from: UserLoser. on November 30, 2003, 02:21 PM
Quote from: K on November 30, 2003, 02:02 PM
// I don't really know VB.
You really show it :P  VB uses ' or REM for comments

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

My friend does ' // for his VB comments.

Skywing

Have you tried using SysAllocString or SysAllocStringByteLen to allocate the string returned?  Perhaps one of those might work.