Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Exporting VC char * to dll and call it back from VB

2 views
Skip to first unread message

Taka

unread,
Apr 22, 2004, 11:52:01 PM4/22/04
to
I am converting 3 c functions into dll...
as follow.

char* _stdcall Dummy3(char *w1, char *w2)
{
return "w1";
}
char _stdcall Dummy2()
{
return 'a';
}
int _stdcall Dummy()
{
return 1234;
}

and 1 def file as follow.
LIBRARY MYDLL
EXPORTS
Dummy @1
Dummy2 @2
Dummy3 @3

now i am using VB
Declare Function Dum2 Lib "Testing.dll" Alias "Dummy2" () As String
Declare Function Dum3 Lib "Testing.dll" Alias "Dummy3" (ByRef w1 As
String, ByRef w2 As String) As Integer
Declare Function Dum1 Lib "Testing.dll" Alias "Dummy" () As Integer

Besides Dum1. All the other have failed and turn the VB down
immediate.
I have tried many method and done many research on web. Still cant
found solution.
What is happening?
Thanks for help

David Lowndes

unread,
Apr 23, 2004, 2:52:25 AM4/23/04
to
One of the problems you have is that VB's integer is 16-bit rather
than 32-bit (C++), so where you have int, in VB it needs to be Long.

Here's some old examples of passing strings to/from VB/C++:

I did some tests on this a while back. Here's how I've passed strings
between VB & a VC DLL:

VB DEFINITIONS:

Declare Function fnVBDLLTest Lib "VBDLLTest.dll" (ByVal s1 As String,
ByVal s2 As String) As Long

Declare Function fnReturnString Lib "VBDLLTest.dll" (ByVal x As
String) As String

Declare Function fnModifyString Lib "VBDLLTest.dll" (ByRef x As
String) As Long


And the CPP tests:

#define VBDLLTEST_API __declspec(dllexport)

extern "C"
{
// Pass strings from VB to the DLL
VBDLLTEST_API int __stdcall fnVBDLLTest( LPCSTR s1, LPCSTR s2 )
{
MessageBox( NULL, s1, s2, MB_OK );
return 42;
}

//Now, an example of returning a string from a C DLL routine
VBDLLTEST_API BSTR __stdcall fnReturnString( LPCSTR s1 )
{
BSTR bstr;
bstr = SysAllocStringByteLen( "Hello there dear world",
sizeof("Hello there dear world")-1 );

return bstr;
}

// And an example that takes a string and modifies it
VBDLLTEST_API int __stdcall fnModifyString( BSTR * pStr )
{
MessageBox( NULL, (LPCSTR) *pStr,
"String passed from VB that I'm gonna modify",
MB_OK );
SysFreeString( *pStr );

static const char szLongString[] = "A Long String";

*pStr = SysAllocStringByteLen( szLongString,
sizeof( szLongString )-1 );

return 42;
}

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

0 new messages