|
-
Nov 25th, 2002, 11:10 PM
#1
Thread Starter
Fanatic Member
returning a string c++ to vb
I'm pretty sure this has been asked before, but I couldn't find it when I searched for it. How do I return a string the same way any other windows api function does it (such as GetSystemDirectory)?
I need to pass a buffer to the dll and have the dll fill the buffer. How can I do this in vc++?
(As you can probably tell, I'm just learning c++, but I have a strong knowledge of vb)
-
Nov 26th, 2002, 12:25 AM
#2
Good Ol' Platypus
Well, have your function with it asking for a count and a char[]. Then, you can simply loop through and add your data from 0 to count - 2. Then, at count - 1, simply add the \0 at the end (string terminator). In any case, return the number of characters in the string. It would be called like this:
VB Code:
Private Declare Function MyFunct Lib "MyLib.dll" (ByVal Count As Long, ByRef Str As String) As Long
Function GetFunct() As String
Dim Q As String
Dim Ret As Long
Q = Space(255)
Ret = MyFunct(255, Q)
GetFunct = Left(Q, Ret)
End Function
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Nov 26th, 2002, 12:32 AM
#3
Thread Starter
Fanatic Member
Actually, I meant the other side of the Declare. In c++, how would I create the function.
in MyLib.cpp...
int __stdcall Myfunct(int Count, ??? Str)
{
//...
return sizeof(Str);
}
what type should I use in place of the ??? in the above example
-
Nov 26th, 2002, 02:57 AM
#4
Thread Starter
Fanatic Member
i modified a previous post that returned the string as the result of the function (i.e. 'return <string>;') to try to get this to work right. here is the source code of both the dll and the vb. this now causes "Bad Dll calling convention".
i have a .def file for the dll that exports 'test'
c++
Code:
#include <windows.h>
#include <stdio.h>
extern "C" __declspec(dllexport) int test(int nCount, BSTR lpTesting)
{
char *tmp = new char[nCount];
memset(tmp, ' ', nCount);
sprintf(tmp, "testing testing testing");
lpTesting = SysAllocStringByteLen(tmp, strlen(tmp));
return sizeof(lpTesting);
}
vb
Code:
Option Explicit
Declare Function test Lib "f:\vs\VC98\MyProjects\ID3\Debug\ID3.dll" (ByVal nCount As Long, ByVal lpReturn As String) As String
Sub t()
Dim a As String
a = Space(255)
a = Left(a, test(255, a))
Debug.Print a
End Sub
Sub main()
t
End Sub
-
Nov 26th, 2002, 07:45 AM
#5
Good Ol' Platypus
Well- you're no longer using STDCALL. VB can only use STDCALL functions.
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Nov 26th, 2002, 12:08 PM
#6
This has indeed been asked very often.
You need a def file. You can't use sizeof(lpTesting) to determine the length of the string.
This function will simply allocate memory, the let the pointer stray. There are at least 3 threads that explain this well. Search this forum for "BSTR".
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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
|