Click here to Skip to main content
Licence 
First Posted 20 Mar 2002
Views 189,866
Bookmarked 25 times

Calling DLLs in VB and C++

By atenney | 20 Mar 2002
Short description of use of __stdcall and __cdecl in VB and C++.
10 votes, 58.8%
1
2 votes, 11.8%
2
1 vote, 5.9%
3
1 vote, 5.9%
4
3 votes, 17.6%
5
1.47/5 - 35 votes
3 removed
μ 1.95, σa 2.83 [?]

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


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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow to call vb dll function from c/c++ application Pinmemberbaranikumar_v4:12 12 Mar '11  
QuestionHow to access printer parallel prot using Inpout32.dll Pinmembermesseret21:48 24 Apr '08  
Questionhow to access the dll functions written in Devc++ in another Console application (DevC++) Pinmemberkkchennai1:14 10 Mar '06  
GeneralProblems with GetProcAdress Pinmemberplacca7:37 7 Nov '05  
GeneralRe: Problems with GetProcAdress PinmemberDon Box21:56 2 Jan '07  
GeneralLoading a dll and using a fucntion in the dll PinmemberJLTucker13:59 3 Jan '05  
GeneralFunction Pointer was not created by a delegate PinsussB Kalyan Chakravarthy1:55 29 Sep '04  
Generaldll calling error Pinsusssriniwas 24 m chennai1:53 17 Sep '04  
Generaldll calling error Pinsusssriniwas,24 m chennai1:44 17 Sep '04  
GeneralLibrary .h in VB Pinmemberpmcborges2:34 17 Jun '04  
GeneralOnKeyDown & PropSheets PinmemberCambalindo13:32 19 May '04  
GeneralRe: Sorry PinmemberCambalindo13:47 19 May '04  
Questionquestion ?????? PinsussAnonymous13:03 19 May '04  
Questionquestion ?????? PinsussAnonymous12:33 19 May '04  
GeneralVB and VC++ callbacks PinmemberPaolo Zaia2:18 11 Jul '03  
GeneralRe: VB and VC++ callbacks PinsussOleg Dobrokhotov19:08 13 Jun '04  
GeneralRe: VB and VC++ callbacks Pinsussbeerc0der10:15 23 Jun '04  
GeneralRe: VB and VC++ callbacks PinsussOleg Dobrokhotov11:22 23 Jun '04  
GeneralRe: VB and VC++ callbacks Pinsussbeerc0der11:39 23 Jun '04  
GeneralRe: VB and VC++ callbacks PinmemberTomaz Rotovnik21:01 9 Jan '05  
GeneralDLL exceptions in VB Pinmembercioina20:07 12 Jun '03  
GeneralCalling a DLL PinsussAnonymous6:40 7 May '03  
GeneralRe: Calling a DLL PinmemberGTS20204:00 12 Sep '03  
GeneralRe: Calling a DLL PinsussAnonymous23:19 15 Sep '04  
Generalcalling dll in VB in Win NT PinsussAnonymous4:37 7 Apr '03  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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