zoukankan      html  css  js  c++  java
  • DLL静态(显示)调用,动态(隐式)调用

    静态调用:

    需要dll文件,lib文件,头文件

    lib文件在编译时用到,在主程序加载时,会装载dll,主程序运行期,dll不能卸载


    动态调用:

    需要dll文件

    使用LoadLibrary-> GetProcAddress调用,只有在LoadLibrary执行时,dll才被装载,dll可以随时被卸载


    动态调用代码例子

    #ifndef _IKPERSON_H_
    #define _IKPERSON_H_
    #ifdef DLL_EXPORT
    #define DLL_API	extern "C" __declspec(dllexport)
    #else
    #define DLL_API extern "C" __declspec(dllimport)
    #endif
    /*
    	设计这个接口类的作用:
    	能采用动态调用方式使用这个类
    */
    class IKPerson
    {
    public:
    	virtual ~IKPerson(void)	//对于基类,显示定义虚析构函数是个好习惯(注意,为什么请google)
    	{
    	}
    	virtual int GetOld(void) const = 0;
    	virtual void SetOld(int nOld) = 0;
    	virtual const char* GetName(void) const = 0;
    	virtual void SetName(const char* szName) = 0;
    };
    /* 导出函数声明 */
    DLL_API IKPerson* _cdecl GetIKPerson(void);
    typedef IKPerson* (__cdecl *PFNGetIKPerson)(void);
    #endif

    #pragma once
    #define DLL_EXPORT
    #include "ikperson.h"
    class CKChinese :
    	public IKPerson
    {
    public:
    	CKChinese(void);
    	~CKChinese(void);
    	virtual int GetOld(void) const;
    	virtual void SetOld(int nOld);
    	virtual const char* GetName(void) const;
    	virtual void SetName(const char* szName);
    private:
    	int m_nOld;
    	char m_szName[64];
    };
    

    #include "StdAfx.h"
    #include "KChinese.h"
    CKChinese::CKChinese(void) : m_nOld(0)
    {
    	memset(m_szName, 0, 64);
    }
    CKChinese::~CKChinese(void)
    {
    }
    int CKChinese::GetOld(void) const
    {
    	return m_nOld;
    }
    void CKChinese::SetOld(int nOld)
    {
    	this->m_nOld = nOld;
    }
    const char* CKChinese::GetName(void) const
    {
    	return m_szName;
    }
    void CKChinese::SetName(const char* szName)
    {
    	strncpy(m_szName, szName, 64);
    }
    /* 导出函数定义 */
    IKPerson* __cdecl GetIKPerson(void)
    {
    	IKPerson* pIKPerson = new CKChinese();
    	return pIKPerson;
    }

    // callclassExportDll.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    #include "../../classExportDll/classExportDll/IKPerson.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
    	HMODULE hDll = ::LoadLibrary(_T("classExportDll.dll"));
    	if (NULL != hDll)
    	{
    		PFNGetIKPerson pFun = (PFNGetIKPerson)::GetProcAddress(hDll, "GetIKPerson");
    		if (NULL != pFun)
    		{
    			IKPerson* pIKPerson = (*pFun)();
    			if (NULL != pIKPerson)
    			{
    				pIKPerson->SetOld(103);
    				pIKPerson->SetName("liyong");
    				cout << pIKPerson->GetOld() << endl;
    				cout << pIKPerson->GetName() << endl;
    				delete pIKPerson;			
    			}
    		}
    		::FreeLibrary(hDll);
    	}
    	return 0;
    }
    


  • 相关阅读:
    php数组常用函数
    java中Property类的基本用法
    properties文件不能输入中文
    Eclipse中Outline里各种图标的含义
    Eclipse的工程名有红色的感叹号,工程里面没有显示编译错误
    路径问题
    yum -y install 和yum install 的区别
    Linux下源码安装jdk
    Linux下安装rz、sz命令(文件上传下载)
    scp命令详解—跨服务器复制文件
  • 原文地址:https://www.cnblogs.com/nafio/p/9137705.html
Copyright © 2011-2022 走看看