C/C++/C#程序如何打成DLL动态库

C/C++程序如何打成DLL动态库:
1.在VS中新建main.h,添加如下内容:
extern "C" _declspec(dllexport) int onLoad();

2.新建main.cpp,并包含如下.h头部,然后实现该定义函数onLoad():
#include "mainFunc.h"
int onLoad()
{
    //func....
}

3.执行程序,生成对应DLL

4.使用该DLL:

i.在要使用的文件中声明:

形式:[DllImport("DllName")]

[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section, string key,
string def, StringBuilder retVal, int size, string filePath);

ii.然后和其他函数一样调用:

GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);

C#程序如何打成DLL:

方法1:图形界面的类库工程
1.文件->新建项目->Visual C#->类库,输入项目名称即DLL类库的名称如FuncUtil,确定,新建类库项目;
2.然后按照自己的功能需求新建C#源文件,可以有若干个.cs源文件,源文件间也可以相互调用,但必须有一个主类提供通用方法供外部调用(public method);

3.点击生成->生成FuncUtil,程序就会编译并生成FuncUtil.dll文件;

4.将如上生成的DLL文件以引用的方式导入到需要调用该DLL的项目中,如下图示:

5.调用方式和其他C#DLL的引用方式相同:

如:

using ComDll; //头部引用

Communicate comm = new Communicate(); // 主函数调用
comm.doSth();

。。。。

方法2:csc打包命令打成DLL

详见:http://msdn.microsoft.com/en-us/library/3707x96z(v=vs.110).aspx

csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs

Add.cs, Mult.cs打包成 MathLibrary.DLL

原文地址:https://www.cnblogs.com/bluestorm/p/3503482.html