Page 1 of 1

How to call a DLL using C++

Posted: Thu Nov 24, 2011 2:08 pm
by Saman
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;
}