Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

First-time DLL 1

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
I want to try to learn how to create a DLL, but have no idea where to start.

My first project is extremely simple. I want my DLL to contain a function called "addone" which takes an Integer as a parameter, adds 1 to it, and returns the new number.

After creating a Win32 DLL project in VC++, I have this as a starting CPP file:
[tt]
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
[/tt]
I have absolutely no idea where to go from here. Is the DllMain function called when the DLL is loaded? Do I then simply add whatever functions I want available below this? Is there an identifier I have to put on my functions to allow access to them from a client application?

Ian
 
Hello:

DllMain() is used for any start up code that your .dll might need. You don't even have to define this function, as the compiler will provide a default version for you. Here is the protoype:

BOOL WINAPI DllMain( HINSTANCE hInstance,
ULONG What,
LPVOID NotUsed );

DllMain() must return non-zero if successful and zero on error.

As for your AddOne() function, to be able to call this function from outside your .DLL, you must export it. I usually #define a macro to make the syntax easier:

#define DllEx extern "C" __declspec(dllexport)

DllEx int AddOne( int i );

Now when calling this function from another program (using either load-time or run-time linking), you must import this function. I usually #include a header file:

//This is dllName.h
#define DllIm extern "C" __declspec(dllimport)

DllIm int AddOne( int i );

I would #include this header in the file that will be calling the .dll routine.

I normally use load-time linking, which means I must use the .lib file that was created when compiling my .dll file:

//This would go in the translation unit that calls the .dll routine.
#include "dllName.h"
#pragma comment( lib,"dllName.lib" )

If you opt to use run-time linking, then you must use the api call LoadLibrary().


Mike L.G.
mlg400@linuxmail.org
 
Thanks Mike...I'll give that a try :eek:)

Ian
 
Ok, I'm having limited success.

I created a very simple DLL file with a single function, "addone", which takes an integer
parameter, adds one, and returns the result.

I created a test console app with the following:
[tt]
#include <iostream.h>
#include &quot;testme.h&quot;
#pragma comment(lib,&quot;mylib.lib&quot;)

DllIm int addone(int a);

int main()
{
cout<<&quot;Here is addone(4)&quot;<<endl;
cout<<addone(4)<<endl;
return 0;
}
[/tt]
This works. The output looks like this:
[tt]
Here is addone(4)
5
[/tt]
Being the perfectionist that I am, though, I wanted to figure out run-time linking as well as
load-time linking, so I removed the &quot;#pragma comment&quot; and inserted this line at the start of my
main() function:
[tt]
HINSTANCE h=LoadLibrary(&quot;mylib.dll&quot;);
[/tt]
However, the Linker gives me this:

error LNK2001: unresolved external symbol __imp__addone

What am I missing? Where should LoadLibrary() be placed? (I don't know if it should be in
main() or not) Is there anything else I should be including to tell the Linker to use the DLL
file for the function?

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top