Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0 
Author Message
 Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0

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);

Quote:
}

__declspec (dllexport) short xftest (char *p)
{
    sprintf (p, "Test data" );
    return (0);

Quote:
}

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



Sun, 29 Apr 2001 03:00:00 GMT  
 Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0
Hi, Steven!
Try to use next code and send me about your progress:

#include <windows.h>
#include <stdio.h>

extern "C"
void WINAPI
__declspec (dllexport) short xftest (char *p);

BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID fImpLoad )
{
    return (TRUE);

Quote:
}

extern "C"
void WINAPI
__declspec (dllexport) short xftest (char *p)
{
    sprintf (p, "Test data" );
    return (0);

Quote:
}

and

Public strbuf As String * 2000
' error! Public Declare Function xftest Lib "SWC40FT" (ByRef x As String) As
Integer
Public Declare Function xftest Lib "SWC40FT" (ByVal x As String) As Integer

Private Sub Command1_Click()
    Dim x As Integer

    strbuf = "Before"
    x = xftest (strbuf)
End Sub

Andrew Kotsar'
IT specialist
Moscow
Sodexho Russia



Mon, 30 Apr 2001 03:00:00 GMT  
 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



Mon, 30 Apr 2001 03:00:00 GMT  
 Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0

Quote:

> 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's an example loosely taken from one of my DLLs and is called from
VB among others:

---(H file)
#ifdef __cplusplus
extern "C" {
#endif

DWORD __stdcall myFunctionName( float *pfValue )

#ifdef __cplusplus

Quote:
}

#endif

---(C++ file)
DWORD __stdcall myFunctionName( float *pfValue )
{
   // Some code here...
   return 0;

Quote:
}

---

It is exported by having an entry in the .def file:

---
EXPORTS
   myFunctionName
---

And declared in VB like:

Declare Function myFunctionName Lib "DLLNAME" (fValue As Single) As Long

I haven't tried it in VB5, but this at least works well in VB4.

Hope this helps!

-+-Ben-+-



Tue, 08 May 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. visual basic 4.0 call to borland c++ 5.0 dll

2. VB 5.0 calling C++ 5.0 DLL that calls winsock.dll

3. Looking for differences between Visual Basic 5.0 enterprise and Visual Basic 5.0 Profesional

4. How do you call a Visual C++ (DLL) function from Visual Basic

5. Visual Basic 5.0 vs. C++ 5.0

6. Can I use Visual Foxpro 5.0 and Visual Basic 5.0

7. [Fwd: Visual Basic 5.0 to Visual C++ 4.0]

8. VB 5.0 calling C++ 5.0 DLLs

9. VB 5.0 calling C++ 5.0 DLLs

10. VB 5.0 calling C++ 5.0 DLLs

11. Visual Baic 5.0 VS Microsoft C++ 5.0

12. How to write Visual C++ DLL's and call them from Visual Basic

 

 
Powered by phpBB® Forum Software