PDA

Click to See Complete Forum and Search --> : DLL Calling Problem?


Vlatko
Sep 29th, 2000, 02:59 PM
I have a problem when i call the dll from VB.The message box appears but after i click ok i get the error message "bad dll calling convention".How can i solve this.
The Dll code

#include "stdafx.h"
void car(HWND h)
{
MessageBox(h,"OK","KO",MB_OK);
}

Def File

LIBRARY "brisi.DLL"
EXPORTS
car @2

And in VB a declare the function and call it:

Public Declare Sub car Lib "brisi.dll" (ByVal h As Long)
car Form1.hWnd

parksie
Sep 29th, 2000, 05:04 PM
Try using:

#include "stdafx.h"
void __declspec(dllexport) car(HWND h)
{
MessageBox(h,"OK","KO",MB_OK);
}

for your code, and

LIBRARY "brisi.DLL"
EXPORTS
car

for your .def file.

gsc1ugs
Nov 23rd, 2000, 10:09 AM
If i dont pass a parameter to a function dll in c++ a can get a return from it. if i do this

extern "C" __declspec( dllexport ) long returnNothing(long nDays)
{
return nDays;
}

i get bad dll calling convention.

any help heres the declare.

Declare Function returnNothing Lib "D:\DEVELOP\ALL16_32\myproj\Debug\myproj.dll" (ByVal nDays As Long) As Long

gsc1ugs
Nov 23rd, 2000, 11:02 AM
help!!!

parksie
Nov 23rd, 2000, 12:19 PM
extern "C" __stdcall __declspec(dllexport) long returnNothing(long nDays) {
return nDays;
}

The __stdcall may need to go after the __declspec.

KENNNY
Nov 24th, 2000, 05:30 PM
extern "C" void __stdcall car(HWND h)
{
//code
}


will work. the extern "C" tells the compiler to do some name mangling thing so VB can understand it, and __stdcall means the func will clear up the stack afterwards, which VB requires.

def file:

LIBRARY "yourdll.dll"
EXPORTS
car

gsc1ugs
Nov 27th, 2000, 04:42 AM
extern "C" int __declspec(dllexport) CALLBACK returnNothing (int nDays)
{
return nDays;
}


vb:

Declare Function returnNothing Lib "D:\DEVELOP\ALL16_32\myproj\Debug\myproj.dll" Alias "_returnNothing@4" (ByVal nDays As Integer) As Integer


ps:

As a real problem with Long any suggestions

parksie
Nov 27th, 2000, 12:17 PM
If you use a .DEF file then you don't actually need the extern "C", since it causes the linker to rename the external references anyway.

KENNNY
Nov 27th, 2000, 05:11 PM
yeah, but its really weird, i've tried it with just __stdcall and a DEF file, and it doesn't work from VB for me. having extern "C" works though.

gsc1ugs
Nov 28th, 2000, 04:01 AM
Yep, got that working extern "C" _cdecl and _dllexport

Another problem arouse with VB, cannot send a LONG to c++ dll and return it .. any thoughts.?

Cheers

KENNNY
Nov 28th, 2000, 04:29 PM
do you mean something like:

Declare Sub AddOne Lib "lala.dll" (Num as Long)

the c++ func should be:

extern "C" void __stdcall AddOne(long *num)
{
*num ++;
}

the thing you're probably doing wrong is not declaring the number as a pointer in the C++ function if you simply do
..(long num) then any changes won't affect the original number.

gsc1ugs
Nov 29th, 2000, 04:50 AM
extern "C" long __stdcall AddOne(long *num)
{
return *num;
}

What, does this work, i'll try it in VB.
Cheers