Click to See Complete Forum and Search --> : Strings & DLLs
Destined Soul
Jul 25th, 2001, 02:18 PM
Hi everyone.
I'm somewhat new to the area of DLLs, so I hope this doesn't seem too trivial.
I made a test DLL in C++ that outputs a character pointer (char*). I'm wondering if anyone knows how I could access this string in visual basic?
in C++, I have something like:
extern "C" __declspec (dllexport) __stdcall char* myFunc()
{
char* myPointer = new char[32];
myPointer = "Hello World";
return myPointer;
}
I know how to pass VB strings INTO a C++ dll, but I don't know how to retrieve them. Does anyone know how to do this?
Thanx in advance,
Destined
denniswrenn
Jul 25th, 2001, 02:25 PM
Public Declare Function MyFunc Lib "dllname.dll" As String
MsgBox MyFunc
parksie
Jul 25th, 2001, 02:38 PM
That's a huge memory leak.
http://www.parksie.net/StringDLL.zip
Destined Soul
Jul 25th, 2001, 03:07 PM
True, parksie, it is a pretty big memory leak. It's been a long time since I've used C++, so my skills are quite rusty.
However, looking over your program, you seem to just modify the character pointer passed to the function - it doesn't return anything.
My C++ code is:
extern "C" __declspec (dllexport) __stdcall char* ReturnHello()
{
char* NewString = "hello world";
return NewString;
}
and my VB Code is:
Private Declare Function ReturnHello Lib "c:\test\DLL2\blah1.dll" Alias "ReturnHello@0" () As String
Private Sub Command1_Click()
Dim myStr As String
myStr = ReturnHello
MsgBox myStr
End Sub
The program crashes when I run it.
I'm using the Bloodshed C++ program/compiler & VB 6.0 Professional. I'm not sure if the C++ compiler is the issue, though.
Destined
parksie
Jul 25th, 2001, 04:52 PM
char* NewString = "hello world";
return NewString;You can't guarantee the data will still exist (although it probably will ;)). My method is the same one as used in GetWindowText. The client provides a buffer and a length which the data is copied into, which is exactly the same effect as returning the data by value, if it weren't for VB and C++ having totally different string semantics.
Destined Soul
Jul 25th, 2001, 06:29 PM
Ah.. okay. Now I see what you mean. Everytime VB tries to access this memory that has already been unallocated it crashes. It's too bad VB doesn't have a better implementation of pointers. :(
However, I think your earlier trick will work. Thanx parksie.
Destined
Chris
Jul 26th, 2001, 02:23 AM
I post this question before, yet haven't found an answer for it. Hope can get somethings from here... :)
My question is, how to write a CALLBACK function? So straight forward :D :p
regards,
jim mcnamara
Jul 26th, 2001, 09:53 PM
For code sample on static callback try:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample98/html/vcsmpcallback.asp
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.