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

本文详细介绍了如何使用Visual Studio将C/C++程序和C#代码打包成DLL文件,包括创建项目、实现功能函数、生成及使用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

// File: Add.cs  
namespace UtilityMethods
{
    public class AddClass { public static long Add(long i, long j) { return (i + j); } } } ... // File: Mult.cs namespace UtilityMethods { public class MultiplyClass { public static long Multiply(long x, long y) { return (x * y); } } } ... // File: TestCode.cs using UtilityMethods; class TestCode { static void Main(string[] args) { System.Console.WriteLine("Calling methods from MathLibrary.DLL:"); if (args.Length != 2) { System.Console.WriteLine("Usage: TestCode <num1> <num2>"); return; } long num1 = long.Parse(args[0]); long num2 = long.Parse(args[1]); long sum = AddClass.Add(num1, num2); long product = MultiplyClass.Multiply(num1, num2); System.Console.WriteLine("{0} + {1} = {2}", num1, num2, sum); System.Console.WriteLine("{0} * {1} = {2}", num1, num2, product); } } /* Output (assuming 1234 and 5678 are entered as command-line arguments): Calling methods from MathLibrary.DLL: 1234 + 5678 = 6912 1234 * 5678 = 7006652 */

csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs
将Add.cs, Mult.cs打包成 MathLibrary.DLL

转载来自:http://www.cnblogs.com/qichunlin/p/7751786.html (已得到博主同意)

 

转载于:https://www.cnblogs.com/lls1350767625/p/7775059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值