Solved

make a DLL

Posted on 2000-02-15
20
306 Views
Last Modified: 2010-05-18
I'm now working with Visual C++ 4.0 under NT4.0 sp4
I want to make a DLL but it would be nice to be system independant and make portable code for (why not) UNIX.

Suppose I've the following class :

Class CElement
{
   CElement();
   CElement(overloading param);
   ~CElement();
   
   void firstfunction(some parameters)
   param *secondfunction(some parameters)
   
}

1- how to generate DLL ? (I've choosen new project / DLL (not appwizard DLL) but I suppose that there is a .def file to write and some settings to change...

2- in order to have portable code, would it be better to redefine all basic types switching on OS, I mean something like :
#ifdef UNIX
 #define my_int unsigned __int32
#elseif ........
or to use only independent system types like __intx (x = 8 / 16 /32 /64)

3- when choosing DLL as new project Platform checkbox was checked, should I let it as is ?

4- now how to use my DLL in a simple project (declaration, etc...) ?

Oliv'
0
Comment
Question by:Oliv
  • 10
  • 9
20 Comments
 

Author Comment

by:Oliv
Comment Utility
regarding DLL tutorial asked by Lecha, I'll be trying to make a .def file and use it in project but my questions about portable code are still available...
0
 

Author Comment

by:Oliv
Comment Utility
forget question 1 & 3

Now I've written my def file but :

because I want my code portable, I've looked for a standard include file which contains declaration of boolean type (I usually include stdafx.hto define BOOL  for apps but don't want to use it anymore for portable...) but don't find it so I redeclare

typedef enum bool { FALSE, TRUE};

which compile perfectly in my DLL BUT

when I try to use my DLL in my app, I've this problem :

error C2059: syntax error : 'int constant'

I've tried to rename bool with an arbitrary name but problem persists !


0
 
LVL 1

Expert Comment

by:wildy
Comment Utility
Use bool (this is the new standard C boolean type opposed to BOOl wich is an integer)
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
Comment Utility
>> I'm now working with Visual C++ 4.0 under NT4.0 sp4
I STRONGLY recommend you upgrade to VC 6.  Too many reasons to list.

>> make a DLL but it would be nice to be
>> system independant and make portable
>> code for (why not) UNIX.
This CAN be done, but may not be easy.  You may have to be careful and woud will probably have to use some conditional compilation (#if...#endif) to make it compile and work under both platforms.

continues.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> I suppose that there is a .def file to write
>> and some settings to change...
You don't have to use a .def file.  (Although you can, and do have to for some cases--like when linking to nonC++ code--but for your case you probably don't have to and it is easier if you don't).

Instead you can indicate if a function is exported using "_declspec(dllexport)".  For example,

__declspec(dllexport) int Increment(int i)
{
    return i + 1;
}

defined and exports a function called increment.  When you need to import the function you would declare it with dllimport, like

__declspec(dllimport) int Increment(int i);

Now this feature is probably not supported on your UNIX compiler, so you might have to use conditional compilation to turn it on/of.  Like

#ifndef ON_UNIX
#define ExportString __declspec(dllexport)
#else
#define ExportString
#endif

ExportString  int Increment(int i)
{
    return i + 1;
}

continues
0
 

Author Comment

by:Oliv
Comment Utility
for widly : which standard include ?

for nietod :
as soon as possible for a VC++ 6.0... credits are missing !

a friend tells me that I've to declare my functions members of my class virtual...
what about this ?

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> - in order to have portable code, would it be better
>> to redefine all basic types switching on OS,
Yes.  and I would recommend names that indicate the signed/unsigned attribute and the size, like "Sgn for signed and "Uns" for unsigned and then the size in either bits or bytes, like:

Sgn8, Uns8, Sgn16, Uns16, ....

Also be sure to use size_t whenever aplicable.  i.e. don't assume that address sizes an be stored in a value of any particular size other than size_t.

>> Platform checkbox was checked, should I let it as is ?
This check box doesn't exist in the new versions of VC, and I don't remember what it does, but you probably should leave it.  (You probably should upgrade!)

>> 4- now how to use my DLL in a
>> simple project (declaration, etc...) ?
Do you mean how to use it in a seperate project.  i.e. like how to use it from an EXE?  if so, you need to link to the DLL's export library (.lib) file.  You can do this by making the DLL project a sub-project of this EXE project.  Or if that is not convenient, (like if the DLL is used by multiple projects) you can just include the .lib file in the EXE project's workspace.

That will link the DLL to the EXE so it is loaded when the EXE runs.  To use the DLL's expoerts you need to declare them using __declspec(dllimport) as I showed you above.  Many programs write a header (.h) file for the DLL that is included into the EXE using the DLL and is included into the DLL source as well.  This header file will define the exported functions in the DLL for both the EXE and the DLL and it uses conditional compilation so that the functions are exported from the DLL and imported to the EXE, like

#ifdef MyDll
#define MyDllExports __declspec(dllexport)
#else
#define MyDllExports _declspec(dllimport)
#endif

So the DLL source code just needs to define "MyDll" before it includes the header file and the functions will be exported from it.  The exe does not define MyDll and the functions will be imported.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> I've tried to rename bool with an arbitrary
>> name but problem persists !
As wildy has sort of suggested, "bool" is the name of a built-in type.  Trying to define soemthing else with that name is like trying to define something with the name "int".

Now not all compilers support bool.  It was added to the langauges 5+ years ago, but old compilers might not support it and you seem to be using at least one old compiler.  (That is one reason why I suggest you upgrade.)  You might have to use conditional compilation so that bool isdefined as above on compilers that don't support it and is not defined on ones that do support it.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> for widly : which standard include ?
"bool" is a built-in type, like "int".  It is not defined in any include.

>> a friend tells me that I've to declare
>> my functions members of my class virtual...
>> what about this ?
If there are functions that need to be virtual, declare them virtual.  exporting from a DLL has no bearing on this (i.e they can be virtual or not as you see fit.)
0
 

Author Comment

by:Oliv
Comment Utility
sorry, but bool, boolean or BOOL don't seems to be defined...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Oliv
Comment Utility
ok, maybe it's because of my compiler, but the fact is when I include stdafx.h it runs.
but if I redefine bool as an enum type why does it says : error.... int constant ?
0
 

Author Comment

by:Oliv
Comment Utility
Adjusted points to 200
0
 

Author Comment

by:Oliv
Comment Utility
nietod,
 you sure help me, but I'm blocked with this f@!ù* boolean type : as I can't resolve this problem I'm blocked !
And I can't evaluate a large part of your answer (even if I know it's right and is functionning...)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> sorry, but bool, boolean or BOOL don't
>> seems to be defined...
it is "bool" and it is defined in standard C++ as a built-in type.  VC 5 supported it and 6 (of course) does.  I don't know if version 4 did.  perhaps it didn't.

if not, lets try making it a little more standard.  Don't use the captalized "true" and "false" as they may be defined elsewhere (lik they are defined in windows.h)  besides the the standard values defined for "bool" are in lowercase, so use that to prepare for a version that supports "bool".  also get rid of that typedef.  so instead do

enum bool { false,true };

Does that help?

>> why does it says : error.... int constant ?
I suspect because the capitalized versions are already defined as something else.  Are you including and include files?  which ones?
0
 

Author Comment

by:Oliv
Comment Utility
only include stdlib.h and string.h
redifining as you suggested works fine...

BTW : we have some apps generated in VC4.0 but we have VC5.0 !
Never try to change from because : when you install VC5.0 it seems to overwrite vc4.0 components (dll...) and project isn't built as in vc4.0 : how can I update several apps from vc4.0 to vc5.0 (if it could be easy else I post a new question...)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> how can I update several apps from
>> vc4.0 to vc5.0
There is no magic.  Just install vc 5. and recompile those apps.  You are likely to get some small compiler errors that you will have to fix before the code compiles.  (because the compiler detects "errors" that 4 doesn't) That is all there is to it.  

The same will happed when you convert to 6.  Which I recommend.  version 6 was the first one to be released after the C++ standard was finished, so it is much closer to standard C++ 4 or 5.  it is much improved.  7 should be great!
0
 

Author Comment

by:Oliv
Comment Utility
Ok, as you suggest I wrote __declspec(dllexport)
but it seems to be not necessary to declare import functions because in my settings from VC4.0 I made an include of the dll and add the /lib as a project's file and include the header of my class..
neither load the dll by LoadLibrary functions...would it so "easy" under UNIX or will I have to make a special header with __declspec(dllimport) ?

Oliv'
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>>  it seems to be not necessary to declare
>> import functions
The function has to be declared or you can't call it.  i.e. you will get a syntax error if the function is not declared.

I don't understand the rest of what you are saying.
0
 

Author Comment

by:Oliv
Comment Utility
1- I include .lib of my dll with add files into project.
2- I include header of my class used in my dll (as it is with (export) keyword ...)
3- don't load library by using LoadLibrary("mydll.dll")
4- don't add my DLL in settings/debug/additional dlls

5- I'm using in my program functions that I've not especially declared as (import) and .... it works !

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
In the program that uses the DLL you can use the regular "extern" keyword instead of __declspec(dllimport), but it is not usually done that way.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now