
Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0
There are two probkems, one on the VB side and one on the C side. Actually,
you can fix them both on the C side, but for convention's sake you should
make changes in both places.
First, the C function should be made _stdcall. This can happen in a couple
of ways, but the simplest is just to include the _stdcall keyword in the
function definition / prototype.
Second, the VB side should use ByVal xx As String, not ByRef. If you use
ByRef, your C DLL will appear to get char **p, not char *p, since a ByVal
string in VB is already at heart a pointer (it's a BSTR, which loosely
translates to LPSTR).
--
Jim Mack
MicroDexterity, Inc
http://www.microdexterity.com
Quote:
>I'm not sure if this is the appropriate place to post my problem. Someone
>please let me know if there is a more appropriate place.
>I need some help on calling DLL functions that I've written in Visual C++
>5.0, from Visual Basic. It seems some weird things are happening with the
>stack on calling the DLL function. I am able to call my DLL from a 'C'
>program, but it doesn't work from Visual Basic. I've debugged to the point
>where I get entry to the DLL function (I display message boxes from the
>DLL), but the passed data is corrupted. Also, on return from the DLL, I
get
>a "Bad DLL calling convention" message from VB.
>Here is my DLL code:
>#include <windows.h>
>#include <stdio.h>
>__declspec (dllexport) short xftest (char *p);
>BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID fImpLoad )
>{
> return (TRUE);
>}
>__declspec (dllexport) short xftest (char *p)
>{
> sprintf (p, "Test data" );
> return (0);
>}
>Visual Basic Code:
>Public strbuf As String * 2000
>Public Declare Function xftest Lib "SWC40FT" (ByRef x As String) As Integer
>Private Sub Command1_Click()
> Dim x As Integer
> strbuf = "Before"
> x = xftest (strbuf)
>End Sub
>Is there a compatibility problem with the use of __declspec (new DLL
>function declaration method) and VB5? Is there a problem of calling the
DLL
>function from a Private event handler? Are my VB5 declarations incorrect?
>Any insight into my problem would be greatly appreciated.
>Regards,
>Steven Szabo
>Division Softworks Inc.
>601 W 16th Ave. Vancouver BC V5Z 1S5
>(604) 872-3677