zoukankan      html  css  js  c++  java
  • 简单的动态连接库,例子

    首先,我们知道VC的三种Dll分别是

    1.non_MFC Dll

    2.MFC Regular Dll

    3.MFC Extension Dll

    平时我们使用在代码中的:

    #pragma comment(lib,"Test_of_dll.lib")


    的意思是指文中生成的obj文件应该与Test_of_dll.lib一起链接.或者可以在VC的工程中设置加载此lib

    下面,来做一个简单动态Dll

    新建一个Win32 Application,application setting中勾选Dll,完成.添加如下文件后,编译链接.

    lib.h文件如下:

    #ifndef LIB_H
    #define LIB_H
    extern "C" int __declspec(dllexport)add(int x,int y);
    #endif


    lib.cpp文件如下:

    #include"stdafx.h"
    #include"lib.h"
    
    int add(int x,int y)
    {
    	return x + y;
    }


    之后另外新建一个工程,调用此工程生成的Dll,Test_of_nonMFCdll.dll

    代码如下:

    // Test_of_dllCall.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include"stdio.h"
    #include"windows.h"
    
    typedef int(*lpAddFun)(int, int);	//宏定义函数指针类型
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	HINSTANCE hDll;		//Dll句柄
    	lpAddFun addFun;	//函数指针
    	hDll=LoadLibrary("..\\..\\Debug\\Test_of_nonMFCdll.dll");
    
    	if(NULL!=hDll)
    	{
    		addFun=(lpAddFun)GetProcAddress(hDll,"add");
    
    		if(NULL!=addFun)
    		{
    			int  result = addFun(2, 3);
    			printf("The result of addFun is %d \n",result);
    			getchar();
    		}
    
    
    		FreeLibrary(hDll);
    	}
    	return 0;
    }
    
    


    注意其中的路径,要根据Dll所在做变化,不然找不到相应的Dll:

    hDll=LoadLibrary("..\\..\\Debug\\Test_of_nonMFCdll.dll");
    


    调用后的结果:

    The result of addFun is 5


     

  • 相关阅读:
    [Beta阶段]发布说明
    [技术博客]使用微信机器人监听群内招聘信息的过程及遇到的问题
    [Beta阶段]第十次Scrum Meeting
    [Beta阶段]第九次Scrum Meeting
    [技术博客]django连接mysql数据库的方法及部分问题的解决方法
    python学习06循环
    python学习05条件分支
    python学习04数据
    python学习03字符串基本操作
    python学习01python入门一
  • 原文地址:https://www.cnblogs.com/wangyaning/p/4237057.html
Copyright © 2011-2022 走看看