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

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 __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

Share

About the Author

atenney
Web Developer
Denmark Denmark
No Biography provided

Comments and Discussions

 
Questionhow to call vb dll function from c/c++ application Pinmemberbaranikumar_v12-Mar-11 4:12 
QuestionHow to access printer parallel prot using Inpout32.dll Pinmembermesseret24-Apr-08 21:48 
Questionhow to access the dll functions written in Devc++ in another Console application (DevC++) Pinmemberkkchennai10-Mar-06 1:14 
GeneralProblems with GetProcAdress Pinmemberplacca7-Nov-05 7:37 
GeneralRe: Problems with GetProcAdress PinmemberDon Box2-Jan-07 21:56 
GeneralLoading a dll and using a fucntion in the dll PinmemberJLTucker3-Jan-05 13:59 
GeneralFunction Pointer was not created by a delegate PinsussB Kalyan Chakravarthy29-Sep-04 1:55 
Generaldll calling error Pinsusssriniwas 24 m chennai17-Sep-04 1:53 
Generaldll calling error Pinsusssriniwas,24 m chennai17-Sep-04 1:44 
GeneralLibrary .h in VB Pinmemberpmcborges17-Jun-04 2:34 
GeneralOnKeyDown & PropSheets PinmemberCambalindo19-May-04 13:32 
GeneralRe: Sorry PinmemberCambalindo19-May-04 13:47 
Questionquestion ?????? PinsussAnonymous19-May-04 13:03 
Questionquestion ?????? PinsussAnonymous19-May-04 12:33 
GeneralVB and VC++ callbacks PinmemberPaolo Zaia11-Jul-03 2:18 
GeneralRe: VB and VC++ callbacks PinsussOleg Dobrokhotov13-Jun-04 19:08 
GeneralRe: VB and VC++ callbacks Pinsussbeerc0der23-Jun-04 10:15 
GeneralRe: VB and VC++ callbacks PinsussOleg Dobrokhotov23-Jun-04 11:22 
GeneralRe: VB and VC++ callbacks Pinsussbeerc0der23-Jun-04 11:39 
GeneralRe: VB and VC++ callbacks PinmemberTomaz Rotovnik9-Jan-05 21:01 
GeneralDLL exceptions in VB Pinmembercioina12-Jun-03 20:07 
GeneralCalling a DLL PinsussAnonymous7-May-03 6:40 
GeneralRe: Calling a DLL PinmemberGTS202012-Sep-03 4:00 
GeneralRe: Calling a DLL PinsussAnonymous15-Sep-04 23:19 
Generalcalling dll in VB in Win NT PinsussAnonymous7-Apr-03 4:37 
Questionhow to call a dll in vb Pinmemberchandsraj3-Apr-03 19:31 
AnswerRe: how to call a dll in vb Pinmembercioina12-Jun-03 20:12 
GeneralRe: how to call a dll in vb PinsussAnonymous30-Dec-03 18:32 
GeneralRe: how to call a dll in vb PinsussAnonymous21-May-04 12:52 
GeneralRe: how to call a dll in vb PinsussAnonymous29-Aug-04 21:02 
GeneralRe: how to call a dll in vb Pinmembervishalking23-May-04 10:46 
General.h from .lib Pinmemberbfadi2-Mar-03 15:21 
GeneralRe: .h from .lib Pinmemberdog_spawn2-Mar-03 15:50 
GeneralLoading C++ dll's into memory with VB PinsussAnonymous8-Jan-03 1:27 
Generalcalling vb dll funtions PinsussAnonymous14-Dec-02 1:17 
GeneralRe: calling vb dll funtions Pinmemberch50829-Jul-04 7:32 
GeneralRe: calling vb dll funtions PinsussAnonymous29-Aug-04 20:59 
GeneralRe: calling vb dll funtions PinsussAnonymous16-Sep-04 23:14 
GeneralCalling a COM in an exe through CPP PinsussChinthaka12-Nov-02 21:11 
GeneralRe: Calling a COM in an exe through CPP PinsussAnonymous29-Aug-04 20:57 
Generalhooks in vb. PinsussAnonymous21-Jul-02 20:18 
QuestionAre dlls made from vb and visual c++ different? PinmemberAnonymous7-Jun-02 21:35 
Generalpass a string to VB-dll from c++ Pinmembereynats21-May-02 3:55 
Generalquestion about passing strings to a dll from VB Pinmembermantrashrim11-Apr-02 10:43 
GeneralRe: question about passing strings to a dll from VB Pinmemberatenney12-Apr-02 6:46 
GeneralRe: question about passing strings to a dll from VB Pinmembernothingremained10-Feb-03 17:27 
GeneralRe: question about passing strings to a dll from VB PinsussAnonymous19-Jun-04 19:55 
GeneralVB calling C++ dll in System32 Pinmemberjcoffey25-Mar-02 15:06 
GeneralRe: VB calling C++ dll in System32 Pinmemberatenney26-Mar-02 4:32 
Question??? PinmemberAnonymous25-Mar-02 14:07 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 21 Mar 2002
Article Copyright 2002 by atenney
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid