Shoutbox

Need help: __cdecl and __stdcall - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Need help: __cdecl and __stdcall (/showthread.php?tid=26128)

Need help: __cdecl and __stdcall by Choli on 05-26-2004 at 08:59 PM

Ok... I have a DLL written in VC++ that exports a function that uses the __cdecl calling convention and I want to call the function from a VB program. However VB uses the __stdcall calling convention so I can't use the DLL (actually I haven't tested that, but I'm sure I won't be albe to use the DLL).

So the question is: Is there anything I can do to force VB use the __cdecl calling convention? What can I do? Ideas?

I'd prefer not to modify the DLL code, because the DLL will be used by another program too, and it needs to stay as __cdecl. I've thought about making another DLL (in VC++) that exports a __stdcall function that calls the __cdecl one in the original DLL, however I'm not very good in VC++ and don't know how to call an exported function of a DLL :$.

Thanks for your help! :)


RE: Need help: __cdecl and __stdcall by RaceProUK on 05-28-2004 at 12:01 PM

I believe all the functions in the Win32 API use __cdecl, but VB can still use them.

Typical VB API access declaration:
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

'Template':
Private Declare Function <functionname> Lib "<filename>.dll" Alias "<funcnamefromdll>" (<arglist>) As <returntype>

EDIT: Tell you what, you post the function header from your DLL, and I'll see if I can come up with a VB declaration statement to allow you to use it.


RE: Need help: __cdecl and __stdcall by Choli on 05-28-2004 at 02:15 PM

quote:
Originally posted by raceprouk
I believe all the functions in the Win32 API use __cdecl, but VB can still use them
No, the Windows API use __stdcall, that's why VB can use them.
quote:
Originally posted by raceprouk
Typical VB API access declaration:
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

'Template':
Private Declare Function <functionname> Lib "<filename>.dll" Alias "<funcnamefromdll>" (<arglist>) As <returntype>
yes, I know :P  I have several years of experience in calling APIs from VB ;)
quote:
Originally posted by raceprouk
EDIT: Tell you what, you post the function header from your DLL, and I'll see if I can come up with a VB declaration statement to allow you to use it.
the problem is not that i don't know who to declare it in VB... anyway, if you're happier with this :P :

the VC++ DLL (traductor.dll) exports this function:
code:
int __cdecl TRADUCIR(const char *p_fichero_iso,const char *p_fichero_mac,const char *p_fichero_mec)

in VB i have this declaration:
code:
Public Declare Function TRADUCIR Lib "traductor.dll" (ByVal iso As Long, ByVal mac As Long, ByVal mec As Long) As Long

and when I try to call it from VB it says wrong call convention :refuck: (logic)

well, I've solved that doing this (however I'm still interested if vb can call __cdelc functions or how would be done if I couldn't modify the dll code) :
in VC++, add and export this function
code:
int __stdcall TRADUCIR__stdcall(const char *p_fichero_iso,const char *p_fichero_mac,const char *p_fichero_mec) {
    return TRADUCIR(p_fichero_iso,p_fichero_mac,p_fichero_mec);
}

and in VB change the declaration by this one:
code:
Public Declare Function TRADUCIR Lib "traductor.dll" Alias "TRADUCIR__stdcall" (ByVal iso As Long, ByVal mac As Long, ByVal mec As Long) As Long
:)
RE: Need help: __cdecl and __stdcall by RaceProUK on 05-28-2004 at 03:10 PM

I've really got to start reading up on stuff before answering questions...


RE: Need help: __cdecl and __stdcall by Mnjul on 05-28-2004 at 05:09 PM

Well Choli, it seems that you haven't searched whole MSDN and Microsoft KB...:P (OK, I'm joking :P)

Would this work? :)

code:
Public Declare Function TRADUCIR CDecl Lib "traductor.dll" (ByVal iso As Long, ByVal mac As Long, ByVal mec As Long) As Long


RE: Need help: __cdecl and __stdcall by Choli on 05-28-2004 at 11:58 PM

quote:
Originally posted by Mnjul
Well Choli, it seems that you haven't searched whole MSDN and Microsoft KB...
lol, :p can you give me the link where you found that, please?
quote:
Originally posted by Mnjul
Would this work?
yes and no, dodgy things are happening here... err... I mean:

When in VB (btw, VB 6, not .net) I put the CDecl keywork, VB recognices it (yay! :banana:) but I've found this things: I call the function in the DLL and when I put the CDelc, VB says wrong calling convention and if i remove CDecl it works. That was a quick test, so I said, I'll repeat later. Now, I've exported 2 functions, one with __cdecl and the other with __stdcall. If un VB I use the CDecl I can't call the function with __stdcall but I can call it if don't use CDecl ((Y)), but I can't call the __cdecl using nor not using CDecl :'(

Also, if in VC++ I export a function without saying __cdecl nor __stdcall, I can't call it from VB either (the default setting at the VC++ proyect is __cdecl).

So I'm starting to be paranoid :P Once the DLL is built, how can I know the calling convention of an exported function (with the dependency walker I can see the exported functions, but not their calling convention).

I'm using VC++ .NET 2002, if that helps :-/

I'm sure I'm doing something wrong but it seems I missed it :rolleyes:

Well, thanks a lot for your help... After seeing how VB compiles the code with the CDecl keyword I'm sure that doing that is possible... Definetely I have to do more tests :P
RE: Need help: __cdecl and __stdcall by Jnrz7 on 05-29-2004 at 01:02 AM

Choli,

VB6 hasta donde se, solo puede usar una DLL que usen la convension STDCALL.
La unica forma que encuentro es que cambies la funcion de CDECL a STDCALL para que pueda ser usada por un programa de VB6.
Cuando lo quieras llamar a la DLL desde otro programa tienes que asigname STDCALL tambien porque si no leera pura basura la DLL.
te escribo en castellano porque mi ingles es malo :p
chao


RE: Need help: __cdecl and __stdcall by RaceProUK on 05-29-2004 at 10:01 AM

And in English?