How to call a DLL using C++

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to call a DLL using C++

Post by Saman » Thu Nov 24, 2011 2:08 pm

Simple code that demonstrates calling of a DLL function from C++.

Code: Select all

int CallMyDLL(void){ 

	/* get handle to dll */
	HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\MyDLL.dll");

	/* get pointer to the function in the dll*/
	FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL), "MyFunc");

	/*	Define the Function in the DLL for reuse.
		This is just prototyping the dll's function.
		A mock of it. Use "stdcall" for maximum compatibility.
	*/ 
	typedef int (__stdcall * pICFUNC)(char *, int);

	pICFUNC MyFunc;
	MyFunc = pICFUNC(lpfnGetProcessID);

	/* The actual call to the function contained in the dll */
	int intMyReturnVal = MyFunc("hello", 5);

	/* Release the Dll */
	FreeLibrary(hGetProcIDDLL);

	/* The return val from the dll */
	returnintMyReturnVal;
}
Post Reply

Return to “C/C++ Programming”