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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declare and call function of DLL in VC++

Status
Not open for further replies.

Hulisi

Programmer
Jan 28, 2001
31
TR
Can I declare and call function of DLL in VC++
Please help me.
 
Yes.
First you have to load the DLL with LoadLibrary function, and then you have to find the address of your DLL function by calling GetProcAddress.
MSDN has enough info on these functions but if you are interested in details, please reply

Lorand
 

thank you Lorand.

how can i it? please send me details.
 
This is a sample code how to call a DLL function. In this case the DLL is "Kernel32.dll" and the function is "CreateToolhelp32Snapshot".
Code:
HMODULE	handleToDLL; 
/* you will need this handle in the "GetProcAddress" function call*/

/* here you load the desired DLL in the memory
You may use the full path of the DLL if its location is not included in the search path. Kernel32 is in the System(32) dir, so we can refer to it just like this*/
handleToDLL=LoadLibrary("Kernel32.DLL");

// if the DLL cannot be loaded exit the function
if(handleToDLL == NULL) return;
else MessageBox("Library loaded!");

/*When you want to call a function from a DLL, first you need its base address (a pointer). The "GetProcAddress" function will return this address, so you will got a pointer to the DLL function. Hereunder there is a typedef for such a function pointer. We know that the function we want to call ("CreateToolhelp32Snapshot") returns a HANDLE and it takes two DWORD parameters, so we define the LPCREATETOOLHELP32SNAPSHOT type like follows: */
typedef HANDLE (WINAPI *LPCREATETOOLHELP32SNAPSHOT)(DWORD,DWORD);

/*declare the function pointer you will use to call the function */
LPCREATETOOLHELP32SNAPSHOT lpfCreateToolhelp32Snapshot; 

/*We need the base address of the function "CreateToolhelp32Snapshot" from the loaded DLL. Notice that the first parameter of the function is the handle to our loaded library, and the second one is the name of the function.*/
lpfCreateToolhelp32Snapshot=(LPCREATETOOLHELP32SNAPSHOT)GetProcAddress(handleToDLL,"CreateToolhelp32Snapshot");
		
if(!lpfCreateToolhelp32Snapshot)
{
	// if the function cannot be found, discard the DLL
     FreeLibrary(handleToDLL);
		return;
}
else
	MessageBox("Function address found!");

// And finally you can call the function !		
HANDLE hProcessList = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
//.....processing........

// When you don't need anymore the DLL, you discard it from the memory.		
FreeLibrary(handleToDLL);

I hope this helps, if you got more questions, just ask.

Lorand
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top