Im learning to write a DLL in Win32 C++ and I've found that its pretty straightforward :) I just have a few questions:

1.) Do I have to have a DllMain function?
2.) Why do some functions look like this:

void _stdCall bla()
{
...
}

And some look like this:

_declspec (dllexport) void bla()
{
...
}

What are the differences?

Thank you.

You can have a DLL without DllMain function. Its purpose is for Windows to notify your DLL of various events that might be of interest to it, e.g. whenever another EXE or DLL loads it etc.

_declspec (dllexport) is a Microsoft specific extension for the compiler to tell it to export your function "bla", so that other DLLs and EXEs can link to it and use it. Other way to accompilsh the same is to use the DEF file and specify your exports there.

void _stdcall bla() is just a normal function visible to your DLL only. _stdcall is specifying standard (or PASCAL) calling convention (as opposed to _cdecl or C calling convention).

Ah, thanks. So if i specify _stdCall, then other programs can't use it? And if i use _declspec (dllexport) then other programs can use it? AND THEN if I just make it a function w/o either of those, and put it in a definition file, then its the same as doing _declspec (dllexport)?

Ah, thanks. So if i specify _stdCall, then other programs can't use it? And if i use _declspec (dllexport) then other programs can use it? AND THEN if I just make it a function w/o either of those, and put it in a definition file, then its the same as doing _declspec (dllexport)?

Yes to all. One note, _stdcall has nothing to do with exporting. It specifies the calling convention for your function.

_declspec (dllexport) void _stdcall foo(int a, int b); or
_declspec (dllexport) void _cdecl foo(int a, int b); or
_declspec (dllexport) void foo(int a, int b); - uses compiler default

You should check your compiler documentation for the explanation of calling conventions.

This article has been dead for over six months. Start a new discussion instead.