View Single Post
Old 04-24-2005, 11:55 AM   #5 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
Yeah! After wading through the microsuck docs, I figured it out. Here it is for those who need to know.

How to explicitly link to a DLL:

In the DLL, you have to have 3 things.
1) The DllMain() MUST return true.
2) The function you want to export must have __declspec(dllexport) in front of it.
3) You need a .DEF file with a list of functions you are exporting
(Note that the microsuck docs claim you do not need #3 if you do #2, but it turns out you need them both for some reason.)

Code:
#include	<windows.h>

bool WINAPI DllMain(HINSTANCE hInstance, DWORD reason, LPVOID data)
{
	return true;
}

__declspec(dllexport) int Add(int a, int b) 
{
	return (a+b);
}
Here is the .DEF file

Code:
LIBRARY	C1
EXPORTS
   Add  DATA
In the app, you do not need any .LIB files that were generated by the DLL project.

Code:
// HMODULE worked too, I don't know which is technically correct
HINSTANCE my_dll = LoadLibrary("C1.dll");

// you could also typedef it like  DJMaze did
int (*myadd)(int, int) = (int (*)(int, int))GetProcAddress(my_dll1, "Add");

// use it normally
int foo = myadd(1, 2);

// free it when finished
FreeLibrary(my_dll);
QUantumAnenome is offline   Reply With Quote