• 动态链接库DLL


    静态链接库的使用方法

    示例代码:http://pan.baidu.com/s/1i303qZV

    1.创建工程

    image

    2.向工程中添加.cpp 和.h

    3.头文件代码

    #ifdef  MyDLL_API 
    
    #else
    #define  MyDLL_API _declspec(dllexport)
    
    #endif
    MyDLL_API int Add(int a,int b);
    MyDLL_API int Sub(int a,int b);
    4.CPP中的代码
    #define  MyDLL_API _declspec(dllexport)
    #include "DLL.h"
    int Add(int a,int b)
    {
    	return a+b;
    }	
    
    int Sub(int a,int b)
    {
    	return a-b;
    } 
    静态链接库的调用:
    1.讲.DLL .LIB  .H 复制到工程目录下
    2.工程--设置--链接  将.lib文件添加到链接中 多个用空格隔开
    3.添加头文件
    4.直接使用方法
     
    动态链接库的创建:http://pan.baidu.com/s/1qWJlVBm

    1.创建工程

    image

    2.向工程中添加.cpp 和.def

    3.cpp代码

     int _stdcall Add(int a,int b)
    {
    
    	return a+b;
    }	
    
    int  _stdcall Sub(int a,int b)
    {
    	return a-b;
    } 

    3.  .def代码

    LIBRARY DLL
    
    EXPORTS
    Add
    Sub
    动态链接库的使用:
    1.将DLL复制到工程目录下
    2.添加以下代码
    void CTestDllDlg::OnBtAdd() 
    {
    	// TODO: Add your control notification handler code here
    	CString str;
    	HINSTANCE hInst;
    	hInst=LoadLibrary("DLL.dll");
    	typedef int (_stdcall *ADDPROC)(int a,int b);
    	ADDPROC Add=(ADDPROC)GetProcAddress(hInst,"Add");
    	if(!Add)
    	{
    		MessageBox("获取函数地址失败!");
    		return;
    	}
    	str.Format("5+3=%d",Add(5,3));
    	MessageBox(str);
    }
    
    void CTestDllDlg::OnBtSub() 
    {
    	// TODO: Add your control notification handler code here
    	CString str;
    	HINSTANCE hInst;
    	hInst=LoadLibrary("DLL.dll");
    	typedef int (_stdcall *SUBPROC)(int a,int b);
    	SUBPROC Sub=(SUBPROC)GetProcAddress(hInst,"Sub");
    	if(!Sub)
    	{
    		MessageBox("获取函数地址失败!");
    		return;
    	}
    	str.Format("5-3=%d",Sub(5,3));
    	MessageBox(str);
    }
    
     
  • 相关阅读:
    最近吸收的html && CSS 知识
    Visual C++ 20111021
    递归变位数(练习)
    effective C++ 第五章
    二叉树的非递归遍历
    插入排序补充
    数的乘方,简单背包,组合
    在这个病毒猖獗的年代……
    元宵夜游城隍庙
    Cherish your work
  • 原文地址:https://www.cnblogs.com/yuqilihualuo/p/4198977.html
Copyright © 2020-2023  润新知