Click here to Skip to main content
15,888,401 members
Articles / Programming Languages / Visual Basic
Article

Calling DLLs in VB and C++

Rate me:
Please Sign up or sign in to vote.
2.21/5 (17 votes)
20 Mar 20022 min read 242.4K   26   50
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


Written By
Web Developer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to call a dll in vb Pin
chandsraj3-Apr-03 18:31
chandsraj3-Apr-03 18:31 
AnswerRe: how to call a dll in vb Pin
cioina12-Jun-03 19:12
cioina12-Jun-03 19:12 
GeneralRe: how to call a dll in vb Pin
Anonymous30-Dec-03 17:32
Anonymous30-Dec-03 17:32 
GeneralRe: how to call a dll in vb Pin
Anonymous21-May-04 11:52
Anonymous21-May-04 11:52 
GeneralRe: how to call a dll in vb Pin
Anonymous29-Aug-04 20:02
Anonymous29-Aug-04 20:02 
GeneralRe: how to call a dll in vb Pin
vishalking23-May-04 9:46
vishalking23-May-04 9:46 
General.h from .lib Pin
Fad B2-Mar-03 14:21
Fad B2-Mar-03 14:21 
GeneralRe: .h from .lib Pin
dog_spawn2-Mar-03 14:50
dog_spawn2-Mar-03 14:50 
GeneralLoading C++ dll's into memory with VB Pin
Anonymous8-Jan-03 0:27
Anonymous8-Jan-03 0:27 
Generalcalling vb dll funtions Pin
Anonymous14-Dec-02 0:17
Anonymous14-Dec-02 0:17 
GeneralRe: calling vb dll funtions Pin
ch50829-Jul-04 6:32
ch50829-Jul-04 6:32 
GeneralRe: calling vb dll funtions Pin
Anonymous29-Aug-04 19:59
Anonymous29-Aug-04 19:59 
GeneralRe: calling vb dll funtions Pin
Anonymous16-Sep-04 22:14
Anonymous16-Sep-04 22:14 
GeneralCalling a COM in an exe through CPP Pin
Chinthaka12-Nov-02 20:11
Chinthaka12-Nov-02 20:11 
GeneralRe: Calling a COM in an exe through CPP Pin
Anonymous29-Aug-04 19:57
Anonymous29-Aug-04 19:57 
Generalhooks in vb. Pin
Anonymous21-Jul-02 19:18
Anonymous21-Jul-02 19:18 
QuestionAre dlls made from vb and visual c++ different? Pin
7-Jun-02 20:35
suss7-Jun-02 20:35 
Generalpass a string to VB-dll from c++ Pin
eynats21-May-02 2:55
eynats21-May-02 2:55 
Generalquestion about passing strings to a dll from VB Pin
mantrashrim11-Apr-02 9:43
mantrashrim11-Apr-02 9:43 
GeneralRe: question about passing strings to a dll from VB Pin
atenney12-Apr-02 5:46
atenney12-Apr-02 5:46 
GeneralRe: question about passing strings to a dll from VB Pin
nothingremained10-Feb-03 16:27
nothingremained10-Feb-03 16:27 
GeneralRe: question about passing strings to a dll from VB Pin
Anonymous19-Jun-04 18:55
Anonymous19-Jun-04 18:55 
GeneralVB calling C++ dll in System32 Pin
jcoffey25-Mar-02 14:06
jcoffey25-Mar-02 14:06 
GeneralRe: VB calling C++ dll in System32 Pin
atenney26-Mar-02 3:32
atenney26-Mar-02 3:32 
Question??? Pin
25-Mar-02 13:07
suss25-Mar-02 13:07 

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.