Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ DLL

304 views
Skip to first unread message

Alex

unread,
Jun 15, 2002, 3:34:22 AM6/15/02
to
I know someone will kick me on this question but I just don't know which
newsgroup to ask this question. Forgive me if I offend someone here. I
think my question is not about DLL itself and I know how to write it in C on
Windows platform. But I need to learn how to implement it using C++.

So, here we go...how to write a DLL in C++ syntax? Thanks for your
tolerance!


Jakob Bieling

unread,
Jun 15, 2002, 3:40:16 AM6/15/02
to
"Alex" <AlexJ...@spam.no> schrieb im Newsbeitrag
news:26CO8.42615$UT.28...@bgtnsc05-news.ops.worldnet.att.net...

> I know someone will kick me on this question but I just don't know
which
> newsgroup to ask this question. Forgive me if I offend someone
here. I
> think my question is not about DLL itself and I know how to write it
in C on
> Windows platform. But I need to learn how to implement it using
C++.

A good place to ask would be
news://comp.os.ms-windows.programer.win32 since this newsgroup deals
with the Standard C++ Language only. And Dll's are not part of the
language but rather an extension of the specific OS your are using
(Windows).

hth
--
jb

(replace y with x to reply)


John Harrison

unread,
Jun 15, 2002, 4:35:59 AM6/15/02
to

"Jakob Bieling" <net...@gmy.net> wrote in message
news:aeeqvt$28f$01$1...@news.t-online.com...

> "Alex" <AlexJ...@spam.no> schrieb im Newsbeitrag
> news:26CO8.42615$UT.28...@bgtnsc05-news.ops.worldnet.att.net...
> > I know someone will kick me on this question but I just don't know
> which
> > newsgroup to ask this question. Forgive me if I offend someone
> here. I
> > think my question is not about DLL itself and I know how to write it
> in C on
> > Windows platform. But I need to learn how to implement it using
> C++.
>
> A good place to ask would be
> news://comp.os.ms-windows.programer.win32 since this newsgroup deals
> with the Standard C++ Language only. And Dll's are not part of the

Typo

news:comp.os.ms-windows.programmer.win32

john

Corno Schraverus

unread,
Jun 15, 2002, 4:37:07 AM6/15/02
to
Okay, lets assume that you have some existing pure C++ code and you want to
place that in a DLL without altering the code.
(if that is not the case, then use the wizard in MS Visual Studio and ask
questions about it at comp.os.ms-windows.programmer.win32).

What you'll have to do is not that much:
1) create (notepad) a yourdll.cpp file with the following code:

<begin code>
#include <windows.h>

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
<end code>

2 open it in MS Visual Studio, try to build it, VS will create a project for
you.
3 add all the existing C++ code to the project, build it again.
4 open a command prompt in the directory where the intermediate files are
build and type 'dumpbin /symbols *.obj > dumpbin.txt' or replace the * by a
specific file name.
6 create a yourdll.def with the following skeleton:
<begin code>
; yourdll.def : Declares the module parameters for the DLL.

LIBRARY "yourdll"
DESCRIPTION 'yourdll Windows Dynamic Link Library'

EXPORTS
; Explicit exports can go here
<end code>

5 open the dumpbin.txt and use find to find the function you want to export,
this function will be between parentheses. Copy the cryptic string to the
left (called decorated name) and place it in the def file below '; Explicit
exports can go here' followed by a tab and a @1
repeat this for all the functions you want to export and increase the
following number (@2 etcetera).
6 add the def file to the project and recompile.
That's it, you can now use the created .lib file in your other projects and
the dll will be linked

Corno

"Alex" <AlexJ...@spam.no> wrote in message
news:26CO8.42615$UT.28...@bgtnsc05-news.ops.worldnet.att.net...

Shabeer A.J

unread,
Jun 15, 2002, 6:12:39 AM6/15/02
to

Alex <AlexJ...@spam.no> wrote in message
news:26CO8.42615$UT.28...@bgtnsc05-news.ops.worldnet.att.net...
just use __declspec(dllexport) infront of the class
for example

class __declspec(dllexport) CFoo
{

};

Create a new project as Win32 Dynamic link library and add your c++ files
and create the dll.

now u can construct an object of class CFoo in your driver(main) program .
Dont forget to copy the .lib file

regards,
Shabeer

Ferdi Smit

unread,
Jun 15, 2002, 10:18:25 AM6/15/02
to
Also note that you can't just use a C++ *class* in a loadtime linking DLL.
It's ok in a runtime linking DLL however.

These links might be interesting:

http://www.codeguru.com/mfc/comments/1156.shtml

http://www.gamedev.net/reference/articles/article928.asp


"Alex" <AlexJ...@spam.no> wrote in message
news:26CO8.42615$UT.28...@bgtnsc05-news.ops.worldnet.att.net...

Ferdi Smit

unread,
Jun 15, 2002, 10:20:21 AM6/15/02
to
> Also note that you can't just use a C++ *class* in a loadtime linking DLL.
> It's ok in a runtime linking DLL however.

That's the other way around...sorry my mistake.


josh

unread,
Jun 15, 2002, 9:56:09 AM6/15/02
to
On Sat, 15 Jun 2002 07:34:22 GMT, "Alex" <AlexJ...@spam.no>
wrote:

> how to write a DLL in C++ syntax?
Same as with C syntax. No difference, DLLs don't care about the
language they're written with.

Sam of Califirnia

unread,
Jun 15, 2002, 5:13:39 PM6/15/02
to
I have looked at a lot of the Windows SDK documentation and related articles
and I am not familiar with the terms "loadtime linking DLL" and "runtime
linking DLL". The terms are (relatively) unique to the gamedev article. The
corresponding terms used by most Windows programmers are "implicit linking"
and "explicit linking".

The CodeGuru comment is for a CodeGuru artricle which is also informative.
See:

http://www.codeguru.com/dll/expclass.shtml


"Ferdi Smit" <sm...@xs4all.nl> wrote in message
news:aeficm$6vu$1...@news1.xs4all.nl...

Sam of Califirnia

unread,
Jun 15, 2002, 5:30:00 PM6/15/02
to
Your instructions assume use of MS Visual Studio. If Visual Studio is being
used then there are many useful and relevant samples provided with it. Also
if Visual Studio is being used then there is no need to use notepad as the
editor.

There is no good reason I am aware of not to use the AppWizard to generate a
project instead of creating it using the automatic method you describe.

I am not sure whether a C++ program can call a member function in a DLL but
it is definitely not possible for a non-C++ program to call a non-static
member function in a DLL. Therefore it is essentailly not useful to export a
decorated name in the manner you describe. It is much more practical to
simply make all functions to be exported C functions, not C++ functions that
get decorated.


"Corno Schraverus" <corno@%spam%.dds.nl> wrote in message
news:aeeuc5$dji$1...@news.tudelft.nl...

Sam of Califirnia

unread,
Jun 15, 2002, 5:33:11 PM6/15/02
to
DLLs don't care but the compiler sure cares. For C++ function names are
decorated.


"josh" <jo...@xtreme.net> wrote in message
news:1105_1024149369@l2092in-wllka-2...

Sam of Califirnia

unread,
Jun 15, 2002, 5:36:32 PM6/15/02
to
Also, as mentionend by Shabeer, __declspec(dllexport) can be used to export
names. It is possible to eliminate the need for a def file most of the time
if names are exported that way.


"Corno Schraverus" <corno@%spam%.dds.nl> wrote in message
news:aeeuc5$dji$1...@news.tudelft.nl...

josh

unread,
Jun 15, 2002, 2:12:37 PM6/15/02
to
On Sat, 15 Jun 2002 21:33:11 GMT, "Sam of Califirnia"
<sam...@socal.rr.com> wrote:
> DLLs don't care but the compiler sure cares. For C++ function
> names are decorated.
So what? Export/import them, that's it. If you need classes,
export/import classes. VC allows for that. No difference
decorated or not. What's the problem you're having,
specifically?

Sam of Califirnia

unread,
Jun 15, 2002, 9:59:28 PM6/15/02
to
I don't have a problem because I only export C functions. The problem is
described quite clearly in the Windows documentation. This is why the
question should be asked in a Windows newsgroup, since the people there
would be very familiar with the problem.


"josh" <jo...@xtreme.net> wrote in message

news:1104_1024164757@l2092in-wllka-2...

josh

unread,
Jun 15, 2002, 7:11:23 PM6/15/02
to
On Sun, 16 Jun 2002 01:59:28 GMT, "Sam of Califirnia"
<sam...@socal.rr.com> wrote:
> I don't have a problem because I only export C functions. The
> problem is described quite clearly in the Windows
>documentation.
Now you say you got no problem, now you say it's described in
the Windows documentation. How confusing.

Corno Schraverus

unread,
Jun 16, 2002, 3:42:27 AM6/16/02
to

"Sam of Califirnia" <sam...@socal.rr.com> wrote in message
news:ArOO8.8803$1D2.1...@twister.socal.rr.com...

> Also, as mentionend by Shabeer, __declspec(dllexport) can be used to
export
> names. It is possible to eliminate the need for a def file most of the
time
> if names are exported that way.
>
true, but your dll will be less robust to change,
see http://home.hiwaay.net/~georgech/WhitePapers/Exporting/Exp.htm for
details.

Corno


Corno Schraverus

unread,
Jun 16, 2002, 3:49:17 AM6/16/02
to

"Sam of Califirnia" <sam...@socal.rr.com> wrote in message
news:slOO8.8797$1D2.1...@twister.socal.rr.com...

> Your instructions assume use of MS Visual Studio. If Visual Studio is
being
> used then there are many useful and relevant samples provided with it.
Also
> if Visual Studio is being used then there is no need to use notepad as the
> editor.
>
> There is no good reason I am aware of not to use the AppWizard to generate
a
> project instead of creating it using the automatic method you describe.
>
One good reason is that it gives you more insight in what is going on. I
really hate wizards of which I don't know what they do (I guess that's why
they are called wizards :)

> I am not sure whether a C++ program can call a member function in a DLL
but
> it is definitely not possible for a non-C++ program to call a non-static
> member function in a DLL. Therefore it is essentailly not useful to export
a
> decorated name in the manner you describe. It is much more practical to
> simply make all functions to be exported C functions, not C++ functions
that
> get decorated.

C++ can call member functions but you are right about the rest.

Corno


josh

unread,
Jun 16, 2002, 3:04:34 PM6/16/02
to
On Sun, 16 Jun 2002 09:42:27 +0200, "Corno Schraverus"
<corno@%spam%.dds.nl> wrote:
>
> "Sam of Califirnia" <sam...@socal.rr.com> wrote in message
> news:ArOO8.8803$1D2.1...@twister.socal.rr.com...
> > Also, as mentionend by Shabeer, __declspec(dllexport) can
be used to
> export
> > names. It is possible to eliminate the need for a def file
most of the
> time
> > if names are exported that way.
> >
> true, but your dll will be less robust to change,
DLLs are *not* objects, they are not "robust to change" by
definition, no matter what you do, be it assigning ordinal
numbers or whatever.

At the same time, using _declspec has performance advantages
(besides eliminating another piece - .def file - from your
configuration.) It is definitely a preferred way of exporting
stuff.

Sam of California

unread,
Jun 20, 2002, 8:53:25 PM6/20/02
to
Okay, I am sorry I am confusing. I guess I should have said that the problem
with exporting C++ functions is
described quite clearly in the Windows documentation. I don't have a problem
because I only export C functions. The following article is, in my opinion,
not well written. However it specifies that a DEF file is needed and/or a
Linker Mapfile must be used for C++ functions. I prefer only C functions
since then I can simply use __declspec(dllexport).

Calling DLL Functions from Visual Basic Applications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HT
ML/_core_calling_dll_functions_from_visual_basic_applications.asp

I guess the main reason I only use C functions is because there is no need
for C++ functions since it is not possible to call a (non-static) member
function from programs that are not written in C++. Perhaps it is possible
but it is dangerous and I think most C++ programmers would not try to.


"josh" <jo...@xtreme.net> wrote in message

news:1105_1024182683@l2092in-wllka-2...

0 new messages