Click here to Skip to main content
Click here to Skip to main content

Calling DLLs in VB and C++

By , 20 Mar 2002
 

DLL Calling conventions:

DLL functions have several calling conventions, with the two most common being __cdecl and __stdcall. Most Windows API functions are written with the __stdcall declaration, and it is the most commonly used in DLLs. These two declarations are very different, and though they can be interchanged in the calling routine in C++, doing so will wreak havoc on the program, creating serious memory leaks! The actual difference in these calling conventions is not really as important as making sure to not get them mixed up. One thing to remember: when using DLLs written in one language, and using them in another, the __stdcall calling convention is usually the most appropriate.

Calling DLLs dynamically in VB:

In VB, loading a DLL requires the use of a __stdcall calling convention DLL function.

Calling DLLs dynamically in C++:

In C++, both calling conventions of __stdcall and __cdecl can be used (but not interchanged!). When prototyping the DLL function, __cdecl is assumed and need not be explicitly typed into the typedef statement. However, when dynamically calling a __stdcall DLL function, it must be explicitly written into the typedef statement. There was some confusion on this, because the statically loaded Windows API functions do not require this anywhere when calling the DLL functions. However, this is because the typedefs already exist in the system-level files of the compiler.

If anyone has questions on this, don't hesitate to ask. As simple as this looks, I racked my brain trying to figure out why, when I was dynamically calling the DLL functions (which were written as __stdcall for VB portability, and declared default in C++, thus __cdecl), my program would run fine until sometime after the DLL call. At that point, all hell broke loose, with no warning. When I would recompile the DLLs using the __cdecl calling convention, everything was just fine. Now I know, and thought I'd save you all some grief at some point down the road, if you should also run into this.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

atenney
Web Developer
Denmark Denmark
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionhow to call vb dll function from c/c++ applicationmemberbaranikumar_v12 Mar '11 - 3:12 
QuestionHow to access printer parallel prot using Inpout32.dllmembermesseret24 Apr '08 - 20:48 
Questionhow to access the dll functions written in Devc++ in another Console application (DevC++)memberkkchennai10 Mar '06 - 0:14 
GeneralProblems with GetProcAdressmemberplacca7 Nov '05 - 6:37 
GeneralRe: Problems with GetProcAdressmemberDon Box2 Jan '07 - 20:56 
GeneralLoading a dll and using a fucntion in the dllmemberJLTucker3 Jan '05 - 12:59 
GeneralFunction Pointer was not created by a delegatesussB Kalyan Chakravarthy29 Sep '04 - 0:55 
Generaldll calling errorsusssriniwas 24 m chennai17 Sep '04 - 0:53 
Generaldll calling errorsusssriniwas,24 m chennai17 Sep '04 - 0:44 
GeneralLibrary .h in VBmemberpmcborges17 Jun '04 - 1:34 
GeneralOnKeyDown & PropSheetsmemberCambalindo19 May '04 - 12:32 
GeneralRe: SorrymemberCambalindo19 May '04 - 12:47 
Questionquestion ??????sussAnonymous19 May '04 - 12:03 
Questionquestion ??????sussAnonymous19 May '04 - 11:33 
GeneralVB and VC++ callbacksmemberPaolo Zaia11 Jul '03 - 1:18 
GeneralRe: VB and VC++ callbackssussOleg Dobrokhotov13 Jun '04 - 18:08 
GeneralRe: VB and VC++ callbackssussbeerc0der23 Jun '04 - 9:15 
GeneralRe: VB and VC++ callbackssussOleg Dobrokhotov23 Jun '04 - 10:22 
GeneralRe: VB and VC++ callbackssussbeerc0der23 Jun '04 - 10:39 
GeneralRe: VB and VC++ callbacksmemberTomaz Rotovnik9 Jan '05 - 20:01 
GeneralDLL exceptions in VBmembercioina12 Jun '03 - 19:07 
GeneralCalling a DLLsussAnonymous7 May '03 - 5:40 
GeneralRe: Calling a DLLmemberGTS202012 Sep '03 - 3:00 
GeneralRe: Calling a DLLsussAnonymous15 Sep '04 - 22:19 
Generalcalling dll in VB in Win NTsussAnonymous7 Apr '03 - 3:37 
Questionhow to call a dll in vbmemberchandsraj3 Apr '03 - 18:31 
AnswerRe: how to call a dll in vbmembercioina12 Jun '03 - 19:12 
GeneralRe: how to call a dll in vbsussAnonymous30 Dec '03 - 17:32 
GeneralRe: how to call a dll in vbsussAnonymous21 May '04 - 11:52 
GeneralRe: how to call a dll in vbsussAnonymous29 Aug '04 - 20:02 
GeneralRe: how to call a dll in vbmembervishalking23 May '04 - 9:46 
General.h from .libmemberbfadi2 Mar '03 - 14:21 
GeneralRe: .h from .libmemberdog_spawn2 Mar '03 - 14:50 
GeneralLoading C++ dll's into memory with VBsussAnonymous8 Jan '03 - 0:27 
Generalcalling vb dll funtionssussAnonymous14 Dec '02 - 0:17 
GeneralRe: calling vb dll funtionsmemberch50829 Jul '04 - 6:32 
GeneralRe: calling vb dll funtionssussAnonymous29 Aug '04 - 19:59 
GeneralRe: calling vb dll funtionssussAnonymous16 Sep '04 - 22:14 
GeneralCalling a COM in an exe through CPPsussChinthaka12 Nov '02 - 20:11 
GeneralRe: Calling a COM in an exe through CPPsussAnonymous29 Aug '04 - 19:57 
Generalhooks in vb.sussAnonymous21 Jul '02 - 19:18 
QuestionAre dlls made from vb and visual c++ different?memberAnonymous7 Jun '02 - 20:35 
Generalpass a string to VB-dll from c++membereynats21 May '02 - 2:55 
Generalquestion about passing strings to a dll from VBmembermantrashrim11 Apr '02 - 9:43 
GeneralRe: question about passing strings to a dll from VBmemberatenney12 Apr '02 - 5:46 
GeneralRe: question about passing strings to a dll from VBmembernothingremained10 Feb '03 - 16:27 
GeneralRe: question about passing strings to a dll from VBsussAnonymous19 Jun '04 - 18:55 
GeneralVB calling C++ dll in System32memberjcoffey25 Mar '02 - 14:06 
GeneralRe: VB calling C++ dll in System32memberatenney26 Mar '02 - 3:32 
Question???memberAnonymous25 Mar '02 - 13:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 21 Mar 2002
Article Copyright 2002 by atenney
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid