|
-
Jul 25th, 2001, 02:18 PM
#1
Strings & DLLs
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
-
Jul 25th, 2001, 02:25 PM
#2
Public Declare Function MyFunc Lib "dllname.dll" As String
MsgBox MyFunc
-
Jul 25th, 2001, 02:38 PM
#3
Monday Morning Lunatic
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
-
Jul 25th, 2001, 03:07 PM
#4
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
-
Jul 25th, 2001, 04:52 PM
#5
Monday Morning Lunatic
Code:
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.
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
-
Jul 25th, 2001, 06:29 PM
#6
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
-
Jul 26th, 2001, 02:23 AM
#7
PowerPoster
-
Jul 26th, 2001, 09:53 PM
#8
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
|