Fork me on GitHub

Dll显式运行时链接

选自《程序员的自我修养》

#include<windows.h>
#include<stdio.h>

typedef double(*Func)(double, double);

int main(int argc, char** argv)
{
	Func func;
	double result;
	//Load dll
	HINSTANCE hinstlib = LoadLibrary("Math.dll");

	if (hinstlib == NULL)
	{
		printf("Error: unable to load dll\n");
		return 1;
	}

	//Get function address
	func = (Func)GetProcAddress(hinstlib, "Add");

	if (func == NULL)
	{
		printf("Error: unable to find dll function\n");
		FreeLibrary(hinstlib);
		return 1;
	}
	//Invoke function
	result = func(1.0, 2.0);

	//Unload dll file
	FreeLibrary(hinstlib);

	//Display result
	printf("Result = %f\n", result);

	return 0;

}

  

posted @ 2016-05-10 09:16  千秋此意  阅读(191)  评论(0编辑  收藏  举报