Click to See Complete Forum and Search --> : Calling DLL from VB


October 26th, 1999, 10:35 AM
Hi, everyone !
I am a beginner to Visual Basic programming. Please help me.

What I want to do is to call a function in a dll created by C++.

C++ source code :

extern "C" __declspec(dllexport) long DllTest2(int *pMode);
__declspec(dllexport) long DllTest(int *pMode)
{
*pMode = 221;
return 725;
}

My Basic Code :
Private Declare Function DllTest Lib "dtest.dll" (ByRef mode as Long) As Long

Dim mode As Long

Private Sub Command1_Click()
res = DllTest(Mode)
End Sub


It's very simple code, but I always got runtime error #49, telling me that I violated calling rule.
(Help tells me that something is wrong in argument type).

Please help me !
What's wrong with my code ?


Thanks in advance.
J. Lee

BrewGuru99
October 26th, 1999, 01:34 PM
I hardly know C++, so this may be way way off, but...

It looks like you are declaring the variable "*pMode" as an integer, and in your vb code your declaring it as a long.


private Declare Function DllTest Lib "dtest.dll" (byref mode as Integer) as Long




Brewguru99

Jaeyeon Lee
October 26th, 1999, 08:02 PM
Thank you for your answer.

Yes, it was my mistake to declare mode as long.
I modified it, but the situation is the same.

Any other suggestions ?

Thanks in advance,
J. Lee

Lothar Haensler
October 27th, 1999, 10:28 AM
IMHO you need to specify __stdcall in your C declaration of the function.
long vs. integer is not your problem:
a C int is equivalent to a VB long!

vbfingers
October 28th, 1999, 11:03 PM
Hey Jayson Lee:

Yes, as stated in the next email msg, there some rules you must follow:

E.g.: From a "C" DLL to VB:

char to char
short to int But be very careful, or the DLL File will 'burn' you.
int to long
long to long < from my experience.

I made a simple, yet straight forward VC++ DLL File(32-bit) without any MFC stuff and there is no name mangling. I have made a VB front end program that calls my newly made DLL file and it works really well!. I interested, drop me an email msg and we can work something out.

VBFINGERS.
RSVP...

Jaeyeon Lee
October 29th, 1999, 01:24 AM
Thank you for the answer.

I solved the problem by refering MSDN site of MS.
You are right that I should have used __stdcall prefix for the C++ function.
But in that case, the compiled name of the C++ function is changed to strange one like _DllTest@12.
So VB could not find the entry point for the function DllTest, because the name DllTest does not
exist in the dll.
To solve this problem, I needed to use Alias at the declaration of function in VB as follows.

Declare Function DllTest lib "Dtest.dll" Alias "_DllTest@12" (......) As Long

Otherwise, I could add the alias to the DEF file of the C++ project as follows.

Exports
DllTest = _DllTest@12

Of course, in this case, no alias is needed in VB part.

Thanks again.

Lothar Haensler
October 29th, 1999, 01:29 AM
No, sir, you don't need an Alias.
Just create a DEF file for your C DLL and define a "clean" name for your function in the EXPORTS section.