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!

Compiling DLL Files With Microsoft Visual C++ 6.0

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello. I am a new user to MV VC++. I know how to get around pretty well when working with programs but I've never compiled a dll file. I have the complete source code for my dll file. I need to export its functions, make my main program recognize these functions, and compile the dll file itself. How is this done in MS VC++.
 
Here's a post from another thread. It might help get you started.

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:

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

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

LiquidBinary (Programmer) Apr 26, 2002
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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top