Solved

dll. i want write and use a simpe dll

Posted on 2000-05-18
17
255 Views
Last Modified: 2010-04-02
Hi C++ dudes

my needs are simple and i am willing to give lots of point away for a good awnswer. (up to 200 * 4)

I use VC++ 5.

I need the instructions for the following :

write a simple dll in C (no classes) that implements one method called test .
char * test(char *input).

write a simple client program to use the dll (in C). call the function passing it "hello world" , it will return "paul - hello world"

now this aint a home work question, im just sick of looking through msdn an seeing MFC examples.......all i need is a start

Cheers :)
0
Comment
Question by:makerp
  • 9
  • 8
17 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2821079
example coming.
0
 
LVL 10

Author Comment

by:makerp
ID: 2821096
thank you.

now remember simple. no wizards etc.

just Win32 dll from new projects as this does nothing for you.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2821101

************BasicDll.h***********
// Used to allow this one .h file to be used in the DLL and
// in anything that uses the DLL.  the DLL defines
// BasicDLL_Source so that it export the stuff defined in this .h
// from the DLL.  The things that use the DLL don't define
// this and will import the things defined in this .hD
#ifndef BasicDLL_Source
#define BasicDLLExp _declspec(dllimport)
#else
#define BasicDLLExp _declspec(dllexport)
#endif

BasiDllExp char * test(char *input);

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2821129
// Define BasicDll_Source to make this compilation
// export, not import.
#define BasicDll_Source
#include "BasicDll.h"

//  sample exported function.
char *test(char *input)
{
    return input;
}

// Sample DLLMain.  Used for intialization and clean up.  Can be ignord if not needed.
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:
    .
    .
    .
    }
    return TRUE;
}

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2821139
That previous post was supposed to be for a file called SimpleDll.Cpp

*****************SimpleClient.cpp*************
#include "SimpleDll.h"

int main()
{
   char *Text = "ABCDEF";
   Test(Text);
   return 0;
}

continues
0
 
LVL 10

Author Comment

by:makerp
ID: 2821145
on build i get this :

error C2282: 'BOOL' is followed by 'APIENTRY' (missing ','?)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2821166
I woudl start by creating the DLL.  Create a new project in VC--briefly:

 go to the File menu
choose new
go to the projects tab
choose "Win32 Dynamic link Library" from the list
make the project name "SimpleDll"
make sure "Create new Workspace" is selected
then press "next"
chose to create an "empty DLL project"
choose finish.

Then you need to add the DLL source to the project.  Place SimpleDll.cpp in the project's directory.  Then add the file to the project by going to:

Project
Add to Project
Files

select the SimpleDll.cpp from the file chooser window.  The file should then be listed in the workspace window.

Compile the project.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2821214
When you compile you should get a SimpleDll.dll file and a SimpleDll.lib file.  These will be needed later for the simpleclient.

Create a seperate project for the simple client.  Again

go to the File menu
choose new
go to the projects tab
choose "Win32 Console Application" from the list
make the project name "SimpleClient"
make sure "Create new Workspace" is selected
then press "next"
chose to create an "empty DLL project"
choose finish.

Now add the SimpleClient.cpp file to the workspace just as you did the SimpleDll.cpp to its workspace.

Copy the DLL and LIB files from the SimpleDll directory to the SimpleClient directories.  You will only need on .lib file, but you might have more than one .DLL from the DLL project--one DLL from the debug build and one from the release build, potentially more if you define more builds.   So I would place the .lib file in the main directory (c:\SimpleClient?) and place the DLLs file in the coresponding debug and release directories ("C:\SimpleClient\Debug? and C:\SimpleClient\Release).

Make the SimpleClient link to the SimpleDll's export library (SimpleDll.lib) by adding this file to the project.  This is done the same way you added the source to the project.  this will also appear in the project workspace window.

Compile the project.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 2821224
>> error C2282: 'BOOL' is followed by 'APIENTRY' (missing ','?)

Opps, add

#include <windows.h>

to the top of the SimpleDll.cpp file.
0
 
LVL 10

Author Comment

by:makerp
ID: 2821276
what about the header for thr dll ?

do i need to add it to the top of my main.cpp in client
0
 
LVL 10

Author Comment

by:makerp
ID: 2821288
Adjusted points from 100 to 150
0
 
LVL 10

Author Comment

by:makerp
ID: 2821289
ive added the .lib

error LNK2001: unresolved external symbol _test_add_two

_test_add_two is my test function
0
 
LVL 22

Expert Comment

by:nietod
ID: 2821398
>> what about the header for thr dll ?
that is a seperate file.   place it in the SampleDll's main directory.
Then either copy it to the SampleSlient directory, or specify the path to the the file in the SampleDll directory like

#include "C:\SampleDll\SampleDll.h"

or make sure the Sample Dll directory is included in the directories to be searched for include files.

(There's lots if different ways of arganizing this stuff.  Some are better from some cases, some for others.  (All will work, its just a matter of avoiding making copies vs avoiding have partial changes affect other projects etc.))

Where does "test_add_two" come from?
0
 
LVL 10

Author Comment

by:makerp
ID: 2821429
Adjusted points from 150 to 200
0
 
LVL 10

Author Comment

by:makerp
ID: 2821430
super cool.. its works..

just a few things then the points are yours.

i first saved/compiled simpledll as a .cpp and it gave me that linker error but it was fine when it was simpledll.c

why. whats the difference.

if i want to encapsulte a class in a dll do i just export the class. if so whats the syntax. or each method/ var

whats the syntax to export varibales

I really appriciate this , thanx alot. :):):)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2821587
The dll and client must both be .c or both be .cpp.  (or you can get around this using extern "C").  The reason is that a .c file is considered C, not C++ so it does not have the extra C++ features.  One such feature is function overloading.  Function overloading is where you have two or more seperate functions with the same name, but different parameters.  The way function overloading is implimented is to append a code ("decoration") to the end of a function's name that expresses the function's parameter.  This way the two overloads don't "really" have the same name.  The problem with mixing .c and .cpp files is that one file produces a decorated name and the is looking for a non decorated name, or vice versa.  You can use "extern "C" in a .cpp file to produce (or look for) a non-decorated name.  So you could get around this

>>  i want to encapsulte a class in a dll do i
>> just export the class. if so whats the syntax
You can selectively export member functions by using the same syntax as above.  i.e. place _declspec(dllexport) or _declspec(dllimport) before each member function.  (you can use a #define for this as above.)   OR you can export all the member functions by placing the _declspec() at the start of the class definiton, like

BasicDllExp class X
{
public:
   X(); // exported constructor
  ~X(); // exported destructor.
};

>> whats the syntax to export varibales
Basically the same.  In a DLL

BasicDllExp int X = 5;

In somethign that uses the DLL

BasicDllExp int X;

Note you might need to add an "extern" to that one, like

BasicDllExp extern int X;

I can't remembe for sure.  But one or the other is right.
0
 
LVL 10

Author Comment

by:makerp
ID: 2821607
thanx, yove gave me a good start :)

probably speek again soon
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

943 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

4 Experts available now in Live!

Get 1:1 Help Now