一、生成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();
        }

VS2010 DLL库生成和使用的更多相关文章

  1. dll库生成和使用

    抄自http://www.cnblogs.com/fangyukuan/archive/2010/06/20/1761464.html 1. VS2010中新建Win32-Win32项目,输入名称Dl ...

  2. vs2010 dll生成,使用问题[good]

    VS2010 动态库开发——第一章 演练:创建和使用动态链接库 (C++) 转载自[http://www.cnblogs.com/sdlypyzq/archive/2012/01/17/2324215 ...

  3. #Lua:Lua调用C++生成的DLL库

    Lua调用C++生成的DLL库 本文参考了某大佬的博客,写得十分详细,推荐!!! 需求: 在之前的求解器中添加了Lua库,使得程序可以在Lua脚本中实现自定义函数功能,考虑到未来可能需要与第三方程序库 ...

  4. [转] lib和dll 区别,生成及使用方法

    lib 和 dll 的区别.生成以及使用详解 [目录] lib dll介绍 生成动态库 调用动态库 生成静态库 调用静态库 首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代 ...

  5. VS2010动态链接库的生成及调用(C++)

    一.动态链接库的生成 首先利用VS2010新建一个空的工程或者win32工程 2.在工程中添加头文件和源文件 3.工程属性配置 3.1 可以在解决方案目录下新建以下几个文件夹 bin (用于存放Rel ...

  6. boost库生成文件命名和编译

    生成文件命名规则:boost中有许多库,有的库需要编译.而有的库不需要编译,只需包含头文件就可以使用.编译生成的文件名字普遍较长,同一个库根据编译链接选项不同,又可以生成多个不同名字的文件.生成的文件 ...

  7. Python 调用 C# dll库最简方法

    1.为什么要跨平台编程?双平台编程或多平台编程,只是为提供更好开发更兼容的解决方案的一种手段,编程时服务于产品和客户的,也是因地制宜. 先安装python所需的库clr ,我这里已经安装了,可以去对应 ...

  8. Unity中调用DLL库

    DLL -- Dynamic Link Library(动态链接库文件),这里以Window平台为例. Unity支持的两种语言生成的DLL库(C++.C#),这里以C#为例,C++网上可以搜索很详细 ...

  9. Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库

    1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...

随机推荐

  1. SpringMVC访问静态页面

    Spring MVC显示静态页面 在前面搭建spring MVC环境时,我们设置了spring-mvc配置,通过tomcat来访问了index.jsp 页面,但是当我将页面换成.thml的静态面之后就 ...

  2. NHibernate Configuring

    NHibernate引用程序中有几个关键组件,如下图所示: 初始化时,NHibernate应用程序将生成一个配置对象.本节中,我们通过设置App.config文件来生成该配置对象.该对象负责加载映射信 ...

  3. win8升级8.1提示卸载sentinel runtime drivers

    Win8升级8.1时提示需卸载sentinel runtime drivers的解决方法 第一步:打开sentinelcustomer.safenet-inc.com/sentineldownload ...

  4. matlab 人面检测

    Create a detector object. faceDetector = vision.CascadeObjectDetector;Read input image. I = imread(' ...

  5. Latex中为作者添加多个单位属性(IEEE模板)

    \author{ \IEEEauthorblockN{name1 name1\IEEEauthorrefmark{1}\IEEEauthorrefmark{2},  name2 name2\IEEEa ...

  6. MySQL 中文乱码解决

    測试环境:服务端和client均为win7,MySql数据库.表字符集为utf-8,字段字符集与表一致. 1.使用mysql命令进行操作时的乱码问题解决. (1)设置当前字符集 set names g ...

  7. JDBC的批量插入操作

    在今天之前,当我遇到需要使用JDBC对数据库进行批量插入操作的时候,我使用的方法如下: ①使用Connection建立数据库连接: ②使用PreparedStatement提交SQL语句,将数据插入: ...

  8. go语言基础之字符串类型 和 字符与字符串类型的区别

    1.字符串类型 示例1: package main //必须有一个main包 import "fmt" func main() { var str1 string str1 = & ...

  9. 【帧动画总结】AnimationDrawable Frame

    Drawable Animation 开发者文档 位置:/sdk/docs/guide/topics/graphics/drawable-animation.html Drawable animati ...

  10. ASP.NET缺少程序集引用怎么办

    如下所示,提示缺少Windows的引用,在using System.Windows.Forms;有一个下划线 错误代码为: 错误    1    命名空间"System"中不存在类 ...