Click here to Skip to main content
13,348,874 members (78,601 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 20 Mar 2002

Calling DLLs in VB and C++

, 20 Mar 2002
Rate this:
Please Sign up or sign in to vote.
Short description of use of __stdcall and __cdecl in VB and C++.

DLL Calling conventions:

DLL functions have several calling conventions, with the two most common being <CODE lang=mc++>__cdecl and <CODE lang=mc++>__stdcall. Most Windows API functions are written with the <CODE lang=mc++>__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 <CODE lang=mc++>__stdcall calling convention is usually the most appropriate.

Calling DLLs dynamically in VB:

In VB, loading a DLL requires the use of a <CODE lang=mc++>__stdcall calling convention DLL function.

Calling DLLs dynamically in C++:

In C++, both calling conventions of <CODE lang=mc++>__stdcall and <CODE lang=mc++>__cdecl can be used (but not interchanged!). When prototyping the DLL function, <CODE lang=mc++>__cdecl is assumed and need not be explicitly typed into the <CODE lang=mc++>typedef statement. However, when dynamically calling a <CODE lang=mc++>__stdcall DLL function, it must be explicitly written into the <CODE lang=mc++>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 <CODE lang=mc++>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 <CODE lang=mc++>__stdcall for VB portability, and declared default in C++, thus <CODE lang=mc++>__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 <CODE lang=mc++>__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.


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

Web Developer
Denmark Denmark
No Biography provided

You may also be interested in...

Comments and Discussions

Questionhow to call vb dll function from c/c++ application Pin
baranikumar_v12-Mar-11 4:12
memberbaranikumar_v12-Mar-11 4:12 
QuestionHow to access printer parallel prot using Inpout32.dll Pin
messeret24-Apr-08 21:48
membermesseret24-Apr-08 21:48 
Questionhow to access the dll functions written in Devc++ in another Console application (DevC++) Pin
kkchennai10-Mar-06 1:14
memberkkchennai10-Mar-06 1:14 
GeneralProblems with GetProcAdress Pin
placca7-Nov-05 7:37
memberplacca7-Nov-05 7:37 
GeneralRe: Problems with GetProcAdress Pin
Don Box2-Jan-07 21:56
memberDon Box2-Jan-07 21:56 
GeneralLoading a dll and using a fucntion in the dll Pin
JLTucker3-Jan-05 13:59
memberJLTucker3-Jan-05 13:59 
GeneralFunction Pointer was not created by a delegate Pin
B Kalyan Chakravarthy29-Sep-04 1:55
sussB Kalyan Chakravarthy29-Sep-04 1:55 
Generaldll calling error Pin
sriniwas 24 m chennai17-Sep-04 1:53
susssriniwas 24 m chennai17-Sep-04 1:53 
Generaldll calling error Pin
sriniwas,24 m chennai17-Sep-04 1:44
susssriniwas,24 m chennai17-Sep-04 1:44 
GeneralLibrary .h in VB Pin
pmcborges17-Jun-04 2:34
memberpmcborges17-Jun-04 2:34 
GeneralOnKeyDown &amp; PropSheets Pin
Cambalindo19-May-04 13:32
memberCambalindo19-May-04 13:32 
GeneralRe: Sorry Pin
Cambalindo19-May-04 13:47
memberCambalindo19-May-04 13:47 
Questionquestion ?????? Pin
Anonymous19-May-04 13:03
sussAnonymous19-May-04 13:03 
Questionquestion ?????? Pin
Anonymous19-May-04 12:33
sussAnonymous19-May-04 12:33 
GeneralVB and VC++ callbacks Pin
Paolo Zaia11-Jul-03 2:18
memberPaolo Zaia11-Jul-03 2:18 
GeneralRe: VB and VC++ callbacks Pin
Oleg Dobrokhotov13-Jun-04 19:08
sussOleg Dobrokhotov13-Jun-04 19:08 
GeneralRe: VB and VC++ callbacks Pin
beerc0der23-Jun-04 10:15
sussbeerc0der23-Jun-04 10:15 
GeneralRe: VB and VC++ callbacks Pin
Oleg Dobrokhotov23-Jun-04 11:22
sussOleg Dobrokhotov23-Jun-04 11:22 
GeneralRe: VB and VC++ callbacks Pin
beerc0der23-Jun-04 11:39
sussbeerc0der23-Jun-04 11:39 
GeneralRe: VB and VC++ callbacks Pin
Tomaz Rotovnik9-Jan-05 21:01
memberTomaz Rotovnik9-Jan-05 21:01 
GeneralDLL exceptions in VB Pin
cioina12-Jun-03 20:07
membercioina12-Jun-03 20:07 
GeneralCalling a DLL Pin
Anonymous7-May-03 6:40
sussAnonymous7-May-03 6:40 
GeneralRe: Calling a DLL Pin
GTS202012-Sep-03 4:00
memberGTS202012-Sep-03 4:00 
GeneralRe: Calling a DLL Pin
Anonymous15-Sep-04 23:19
sussAnonymous15-Sep-04 23:19 
Generalcalling dll in VB in Win NT Pin
Anonymous7-Apr-03 4:37
sussAnonymous7-Apr-03 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 21 Mar 2002
Article Copyright 2002 by atenney
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid