欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c++动态库显示加载方法

程序员文章站 2022-06-09 17:21:03
...
#include <stdio.h>
#include <stdlib.h>
#include "iniparser.h"

#include <afx.h>

typedef dictionary * (*iniLoad)(const char * ininame);
typedef void(*iniDump)(dictionary * d, FILE * f);
typedef char * (*iniGetsecname)(dictionary * d, int n);
typedef int (*iniGetsecnkeys)(dictionary * d, const char * s);
typedef char * (*iniGetstring)(dictionary * d, const char * key, const char * def);
typedef int (*iniGetint)(dictionary * d, const char * key, int notfound);
typedef void (*iniFreedict)(dictionary * d);


int main(void)
{
	dictionary *ini;
	int n = 0;
	char *str;
	HINSTANCE hdll;
	iniLoad LoadFun;
	iniDump iniparser_dump;
	iniGetsecname iniparser_getsecname;
	iniGetsecnkeys iniparser_getsecnkeys;
	iniGetstring iniparser_getstring;
	iniGetint iniparser_getint;
	iniFreedict iniparser_freedict;


	hdll = LoadLibrary(_T("iniparer.dll"));
	if (NULL == hdll)
	{
		FreeLibrary(hdll);
		exit(-1);
	}

	LoadFun = (iniLoad)GetProcAddress(hdll, "iniparser_load");
	if (NULL == LoadFun)
	{
		FreeLibrary(hdll);
		exit(-1);
	}

	ini = LoadFun("example.ini");//parser the file
	if (ini == NULL)
	{
		fprintf(stderr, "can not open %s", "example.ini");
		exit(EXIT_FAILURE);
	}

	iniparser_dump = (iniDump)GetProcAddress(hdll, "iniparser_dump");
	if (NULL == iniparser_dump)
	{
		FreeLibrary(hdll);
		exit(-1);
	}
	printf("dictionary obj:\n");
	iniparser_dump(ini, stderr);//save ini to stderr

	iniparser_getsecnkeys = (iniGetsecnkeys)GetProcAddress(hdll, "iniparser_getsecnkeys");
	if (NULL == iniparser_getsecnkeys)
	{
		FreeLibrary(hdll);
		exit(-1);
	}
	printf("number of keys in %s is %d\n", "tcp", iniparser_getsecnkeys(ini, "tcp"));

	iniparser_getsecname = (iniGetsecname)GetProcAddress(hdll, "iniparser_getsecname");
	if (NULL == iniparser_getsecname)
	{
		FreeLibrary(hdll);
		exit(-1);
	}

	printf("\n%s:\n", iniparser_getsecname(ini, 0));//get section name

	iniparser_getint = (iniGetint)GetProcAddress(hdll, "iniparser_getint");
	if (NULL == iniparser_getint)
	{
		FreeLibrary(hdll);
		exit(-1);
	}
	n = iniparser_getint(ini, "tcp:port", -1);
	printf("port : %d\n", n);

	iniparser_getstring = (iniGetstring)GetProcAddress(hdll, "iniparser_getstring");
	if (NULL == iniparser_getstring)
	{
		FreeLibrary(hdll);
		exit(-1);
	}
	str = iniparser_getstring(ini, "tcp:ip", "null");
	printf("ip : %s\n", str);

	str = iniparser_getstring(ini, "tcp:family", "null");
	printf("family : %s\n", str);

	printf("\n%s:\n", iniparser_getsecname(ini, 1));
	n = iniparser_getint(ini, "serial port:speed", -1);
	printf("speed : %d\n", n);

	iniparser_freedict = (iniFreedict)GetProcAddress(hdll,"iniparser_freedict");
	if (NULL == iniparser_freedict)
	{
		FreeLibrary(hdll);
		exit(-1);
	}

	iniparser_freedict(ini);//free dirctionary obj
	FreeLibrary(hdll);
	return 0;
}
相关标签: 动态库