Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem calling VC DLL from VB

Status
Not open for further replies.

Clairvoyant1332

Programmer
May 21, 2003
147
US
As an excercise, I've written a simple DLL in Visual C to be called from Visual Basic, but I'm having a little trouble getting the datatypes to match up. I'm using VB6 and VC6. The error I get from VB is "Bad DLL calling convention"

This code will work under VB/VC.NET, but I'd rather not use .NET in this case as I'd like to avoid having to deploy the .NET framework for the app that will use this functionality.

Thanks,
Dennis

vctest.cpp:

#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

int vcadd(int a, int b)
{
return a+b;
}

vctest.def:

LIBRARY vctest
EXPORTS
vcadd

vbtest.vb:

Declare Function vcadd Lib "../../vctest/debug/vctest.dll" (ByVal a As Integer, ByVal b As Integer) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s1 As Integer
Dim s2 As Integer
Dim s3 As Integer

s1 = 5
s2 = 8
s3 = vcadd(s1, s2)
MsgBox("s3=" & s3)
End Sub
 
VB integers are 2 bytes, whereas the C int's are 4 bytes. Try using long integers in the Declare from within VB.

Greetings,
Rick
 
I tried declaring all of the VB variables as Long, and used Long for the types in the Declare statement, but no dice.
 
Use __declspec(dllexport), WINAPI, or something similar to that; the function declaration in th C dll is not conform dll conventions, I think.....

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top