VS2010 DLL库生成和使用

一、生成dll文件(VS2010 Win32 程序)

CreateDll.h

// 下列 ifdef 块是创建使从 DLL 导出更简单的
// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 CREATEDLL_EXPORTS
// 符号编译的。在使用此 DLL 的
// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
// CREATEDLL_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
// 符号视为是被导出的。
#ifdef CREATEDLL_EXPORTS
#define CREATEDLL_API __declspec(dllexport)
#else
#define CREATEDLL_API __declspec(dllimport)
#endif

// 此类是从 CreateDll.dll 导出的
class CREATEDLL_API CCreateDll {
public:
    CCreateDll(void);
    // TODO: 在此添加您的方法。
};

extern CREATEDLL_API int nCreateDll;

CREATEDLL_API int fnCreateDll(void);

CREATEDLL_API int printMax(int& x, int& y);

CREATEDLL_API int printMax(int& x, int& y, int& z);

// CreateDll.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "CreateDll.h"
#include <iostream>
using namespace std;

// 这是导出变量的一个示例
CREATEDLL_API int nCreateDll = 0;

// 这是导出函数的一个示例。
CREATEDLL_API int fnCreateDll(void)
{
    cout<<"the default function"<<endl;
    return 42;
}

CREATEDLL_API int printMax(int& x, int& y)
{
    cout<<"among("<<x<<","<<y<<")the bigger is:"<<(x > y ? x : y)<<endl;
    return 0;
}

CREATEDLL_API int printMax(int& x, int& y, int& z)
{
    cout<<"among("<<x<<","<<y<<","<<z<<")the max is:"<<((x > y ? x : y)> z ? (x > y ? x : y) : z)<<endl;
    return 0;
}


// 这是已导出类的构造函数。
// 有关类定义的信息,请参阅 CreateDll.h
CCreateDll::CCreateDll()
{
    cout<<"the default class."<<endl;
    return;
}

CreateDll.def

LIBRARY CreateDLL  
EXPORTS  
pMaxA2 = ?printMax@@YAXAAH0@Z  
pMaxA3 = ?printMax@@YAXAAH00@Z  

二、使用Dll文件(VS2010 Win32 程序):

// UseDll.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;

typedef void(* FUNA)(int&, int&);
typedef void(* FUNB)(int&, int&, int&);

int _tmain(int argc, _TCHAR* argv[])
{
    const char* dllName = "CreateDLL.dll";  
    /*const char* funName1 = "printMax";  
    const char* funName2 = "printMax";  */

    /*const char* funName1 = "?printMax@@YAXAAH0@Z";  
    const char* funName2 = "?printMax@@YAXAAH00@Z";  */

    const char* funName1 = "pMaxA2";  
    const char* funName2 = "pMaxA3";  

    int x(100), y(200), z(300);  
    HMODULE hDLL = LoadLibraryA(dllName);  
    if(hDLL != NULL)  
    {  
        //怎样获取类:

        //怎样获取参数:

        FUNA fp1 = FUNA(GetProcAddress(hDLL,funName1));  
        if(fp1 != NULL)  
        {  
            std::cout<<"Input 2 Numbers:";  
            std::cin>>x>>y;  
            fp1(x,y);  
        }  
        else  
        {  
            std::cout<<"Cannot Find Function "<<funName1<<std::endl;  
        }  
        FUNB fp2 = FUNB(GetProcAddress(hDLL,funName2));  
        if(fp2 != NULL)  
        {  
            std::cout<<"Input 3 Numbers:";  
            std::cin>>x>>y>>z;  
            fp2(x,y,z);  
        }  
        else  
        {  
            std::cout<<"Cannot Find Function "<<funName2<<std::endl;  
        }  
        FreeLibrary(hDLL);  
    }  
    else  
    {  
        std::cout<<"Cannot Find "<<dllName<<std::endl;  
    }  
    system("pause");
    return 0;
}

二、生成DLL和在C#中使用:

1、在stdafx.h中加入以下内容:

#ifndef DLL_EXPORT
#define DLL_EXPORT 1
#endif

2、ExportDll.h文件:

#ifndef __EXPORTDLL_H__
#define __EXPORTDLL_H__

//#ifdef EXPORTDLL_EXPORTS
#ifdef  DLL_EXPORT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif

//
//函数定义

extern "C" DLL_API int InitObject();

extern "C" DLL_API int UninitObject();

extern "C" DLL_API int ReadXML();

extern "C" DLL_API int WriteXML();

#endif
3、ExportDll.cpp文件:

// ExportDll.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "ExportDll.h"
#include <string>
#include <iostream>
using namespace std;

//
//声明类

class Object
{
private:
    string xml;
public:
    Object(const char* xmlName);

    bool ReadXML();
    bool WriteXML();

    ~Object();
};

//
//定义类

Object::Object(const char* xmlName)
{
    xml = xmlName;
}

bool Object::ReadXML()
{
    cout<<"Read"<<xml<<endl;
    return true;
}

bool Object::WriteXML()
{
    cout<<"Write"<<xml<<endl;
    return true;
}

Object::~Object()
{
    //xml = nullptr;
}

//
//定义全局变量

Object* obj =  nullptr ;

//
//导出函数

extern  Object* obj;

extern "C" DLL_API int InitObject()
{
    if (obj != nullptr)
    {
        return -1;
    }
    const char* xml = "Dll.Xml";
    obj = new Object(xml);
    //delete obj;
    if (nullptr == obj)
    {
        return -1;
    }
    return 0;
}

extern "C" DLL_API int ReadXML()
{
    if (nullptr == obj)
    {
        return -1;
    }
    obj->ReadXML();
    return 0;
}

extern "C" DLL_API int WriteXML()
{
    if (nullptr == obj)
    {
        return -1;
    }
    obj->WriteXML();
    return 0;
}

extern "C" DLL_API int UninitObject()
{
    if (nullptr == obj)
    {
        return -1;
    }
    delete obj;
    cout<<obj<<endl;
    obj = nullptr;
    return 0;
}
4、将相应的lib、dll文件拷贝到工作目录下:

5、 [DllImport("ExportDll.dll", CharSet = CharSet.Unicode, EntryPoint = "InitObject", CallingConvention = CallingConvention.Cdecl)]
        public static extern int InitObject();
        [DllImport("ExportDll.dll", CharSet = CharSet.Unicode, EntryPoint = "ReadXML", CallingConvention = CallingConvention.Cdecl)]
        public static extern int ReadXML();
        [DllImport("ExportDll.dll", CharSet = CharSet.Unicode, EntryPoint = "WriteXML", CallingConvention = CallingConvention.Cdecl)]
        public static extern int WriteXML();
        [DllImport("ExportDll.dll", CharSet = CharSet.Unicode, EntryPoint = "UninitObject", CallingConvention = CallingConvention.Cdecl)]
        public static extern int UninitObject();

        static void Main(string[] args)
        {
            int a = InitObject();
            int b = ReadXML();
            int c = WriteXML();
            int d = UninitObject();
            d = UninitObject();
            d = InitObject();
            d = InitObject();
            d = UninitObject();
        }

转载于:https://www.cnblogs.com/shenchao/p/3201666.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值