
Calling a win32 DLL in a vb app - Need help
Guy 's Thanks for the replies . But the problem still exists :-( .
Of course this is for testing purposes . This time I used a DEF file and _stdcall convention with Aliases ans specifying the reurn type as long,But still no luck . The error I got was ' Cant find DLL entry point x in x.dll ' .. Again I get this problem with integers .However this works in Debug mode .
This time I 'll attch my C code . Maybe that could shed some light on the matter . I 've been wading thru MSDN all day with no success . :-(
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
Quote:
}
long _stdcall aa(int x)
{
int y=0;
y = x * x;
return y;
Quote:
}
You have four problems:
1) You should either use a DEF file, or an Alias clause in your Declare statement with the munged name.
2) The function should be _stdcall, not _cdecl
3) Your Declare statement should return a Long, you're currently returning the default VB type of Variant. This in and of itself can give you a bad calling convention because the Variant return type is passed on the stack and Long is passed in registers, so the two functions have a different stack size.
4) You should never hard code a path in your Declare statements. I'll give you the benefit of the doubt and assume this is for testing purposes only, not for your final product.
Also, please tell us you're doing something other than squaring and integer in your Dll. You should just do this in VB. -Matt
Hello ,
I have written a static DLL which takes in an INTEGER and returns it 's Square value .also ineger . I exported the function 'Calc' by using the keyword __declspec(dllexport) in the function's definition . I have not used DEF files to export functions
My problem is this .. I checked the functionailty with an MFC and Console application in C and it worked fine . However when I tried the same in VB I got
a 'Bad DLL Calling Convention ' Error .
Does this have something todo with the dll calling mechanism specific to VB ? .
Here 's my code .Any help is most apprecaited .
J .
=====
Private Declare Function Calc Lib "D:\Projects\iSquare.dll" (ByVal iSqr As Long) <-- here 's where the dll is called
Private Sub Command1_Click()
On Error GoTo errD:
Dim iRes As Integer
iRes = 0
iRes = Calc(CInt(Text1))
Text2 = CStr(iRes)
Exit Sub
errD:
MsgBox Err.Description
Exit Sub
Resume
End Sub
=====