dll. i want write and use a simpe dll

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 :)
LVL 10
makerpAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nietodConnect With a Mentor Commented:
example coming.
0
 
makerpAuthor Commented:
thank you.

now remember simple. no wizards etc.

just Win32 dll from new projects as this does nothing for you.
0
 
nietodCommented:

************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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
nietodCommented:
// 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
 
nietodCommented:
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
 
makerpAuthor Commented:
on build i get this :

error C2282: 'BOOL' is followed by 'APIENTRY' (missing ','?)
0
 
nietodCommented:
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
 
nietodCommented:
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
 
nietodCommented:
>> error C2282: 'BOOL' is followed by 'APIENTRY' (missing ','?)

Opps, add

#include <windows.h>

to the top of the SimpleDll.cpp file.
0
 
makerpAuthor Commented:
what about the header for thr dll ?

do i need to add it to the top of my main.cpp in client
0
 
makerpAuthor Commented:
Adjusted points from 100 to 150
0
 
makerpAuthor Commented:
ive added the .lib

error LNK2001: unresolved external symbol _test_add_two

_test_add_two is my test function
0
 
nietodCommented:
>> 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
 
makerpAuthor Commented:
Adjusted points from 150 to 200
0
 
makerpAuthor Commented:
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
 
nietodCommented:
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
 
makerpAuthor Commented:
thanx, yove gave me a good start :)

probably speek again soon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.