net平台上,调用dll文件有2种含义:
1、调用托管dll,即使用.net平台开发的dll,属于托管代码。
2、调用非托管dll,即传统的dll,一般是C++,VB,DELPHI等等开发出来的,属于非托管代码。
对于托管调用,举例如下:
比如一段坐标转换的程序,在名字空间MyDll里,有三个类
在函数类里有两个方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//坐标转换方法
namespace MyDll
{
public class zbTrans_4para
{
public double disX; // X方向平移(米)
public double disY; // Y方向平移(米)
public double angX; // X方向旋转(弧度)
public double m; // 比例因子(PPm)
}
public class zb_XY
{
public double X;
public double Y;
}
public class TransCode
{
//-----------------------------------------------------------------------
//坐标转换参数赋值
double disX, disY, angle, scalem; //定义
disX = para4.disX; disY = para4.disY; //平移
angle = para4.angX; //旋转
scalem = para4.m; //比例因子
// -----------------------------------------------------------------------
//变换顺序:平移->旋转->缩放
outx = ((disX + x) * Math.Cos(Trans_jd_hd(angle)) + (disY + y) * Math.Sin(Trans_jd_hd(angle))) * (1 + scalem / 1000000);
outy = ((disY + y) * Math.Cos(Trans_jd_hd(angle)) - (disX + x) * Math.Sin(Trans_jd_hd(angle))) * (1 + scalem / 1000000);
// -----------------------------------------------------------------------
return 1;
}
public static double Trans_jd_hd(double ang)
{
double Pi;
Pi = 4 * Math.Atan(1.0);
double ang_d, ang_f, ang_m;
double ang_hd;
// int n , j , i ;
bool flag_1;
flag_1 = true;
if (ang < 0)
{
ang = Math.Abs(ang);
flag_1 = false;
}
ang_d = Math.Floor(ang);
ang_f = (ang * 100 - ang_d * 100);
//120.35 的计算会出现误差,计算的分值为34.999999999 这样会引起很大的偏差 下面的if语句来处理这个问题
if (Math.Abs(Math.Floor(ang_f) - ang_f) > 0.6)
{
ang_f = Math.Ceiling(ang_f);
}
else
{
ang_f = Math.Floor(ang_f);
}
ang_m = (ang * 10000 - ang_d * 10000 - ang_f * 100);
if (ang_m < Math.Pow(10, -10))
{
ang_m = 0;
}
ang_hd = (((ang_d * 3600 + ang_f * 60 + ang_m) / 3600) * Pi) / 180;
if (flag_1 == false)
{
return (-1) * ang_hd;
}
else
{
return ang_hd;
}
}
}
}
将上述代码保存为*.CS文件,通过调用:
csc/target:library /out:name.dll *.cs
即可以编译成name.dll文件
补充:win+r 、cmd、在cmd窗口,输入cd加一个空格 将C:\Windows\Microsoft.NET\Framework\v4.0.30319文件夹拖到窗口
不同版本的net.framework文件名不一样。然后就可以调用上面的函数了。
library后面要有一个空格,否则会报错
fatal error CS2019: /target 的目标类型无效:
必须指定“exe”、“winexe”、“library”或“module”
调用时:
1、项目里的References上右键,选择Reference,选择Brouse选项卡,浏览找到编译的DLL,确定添加
,该DLL就被添加到项目的BIN里。
2、如果有名称空间,要添加名称空间,如我的是MyDll,如果没有名称空间,则不用添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Runtime.InteropServices;
using MyDll;
namespace DLL
{
//[DllImport("mymethod.dll")]
class Program
{
static void Main(string[] args)
{
zb_XY zbxy = new zb_XY();
zbTrans_4para para = new zbTrans_4para();
TransCode tcode = new TransCode();
para.disX = 5.488;
para.disY = 100.964;
para.angX = 0.2;
para.m = 0;
zbxy.X = 234567.43234;
zbxy.Y = 345678.3445;
double outx;
double outy;
tcode.Trans_XY_Fmodel(zbxy.X, zbxy.Y, out outx, out outy, para);
Console.WriteLine("{0}\n{1}", outx, outy);
Console.ReadKey();
}
}
}
3、对于有名称空间的dll,如果没用引用名称空间,当需要实例化对象时,要带上名称空间
如 MyDll.zb_xy zbxy=new zb_xy();静态方法不用实例化对象。用MyDll.Trans_jd_hu直接调用
1、调用托管dll,即使用.net平台开发的dll,属于托管代码。
2、调用非托管dll,即传统的dll,一般是C++,VB,DELPHI等等开发出来的,属于非托管代码。
对于托管调用,举例如下:
比如一段坐标转换的程序,在名字空间MyDll里,有三个类
在函数类里有两个方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//坐标转换方法
namespace MyDll
{
public class zbTrans_4para
{
public double disX; // X方向平移(米)
public double disY; // Y方向平移(米)
public double angX; // X方向旋转(弧度)
public double m; // 比例因子(PPm)
}
public class zb_XY
{
public double X;
public double Y;
}
public class TransCode
{
//坐标转换函数 四参数
public int Trans_XY_Fmodel(double x, double y, out double outx, out double outy, zbTrans_4para para4){
//-----------------------------------------------------------------------
//坐标转换参数赋值
double disX, disY, angle, scalem; //定义
disX = para4.disX; disY = para4.disY; //平移
angle = para4.angX; //旋转
scalem = para4.m; //比例因子
// -----------------------------------------------------------------------
//变换顺序:平移->旋转->缩放
outx = ((disX + x) * Math.Cos(Trans_jd_hd(angle)) + (disY + y) * Math.Sin(Trans_jd_hd(angle))) * (1 + scalem / 1000000);
outy = ((disY + y) * Math.Cos(Trans_jd_hd(angle)) - (disX + x) * Math.Sin(Trans_jd_hd(angle))) * (1 + scalem / 1000000);
// -----------------------------------------------------------------------
return 1;
}
public static double Trans_jd_hd(double ang)
{
double Pi;
Pi = 4 * Math.Atan(1.0);
double ang_d, ang_f, ang_m;
double ang_hd;
// int n , j , i ;
bool flag_1;
flag_1 = true;
if (ang < 0)
{
ang = Math.Abs(ang);
flag_1 = false;
}
ang_d = Math.Floor(ang);
ang_f = (ang * 100 - ang_d * 100);
//120.35 的计算会出现误差,计算的分值为34.999999999 这样会引起很大的偏差 下面的if语句来处理这个问题
if (Math.Abs(Math.Floor(ang_f) - ang_f) > 0.6)
{
ang_f = Math.Ceiling(ang_f);
}
else
{
ang_f = Math.Floor(ang_f);
}
ang_m = (ang * 10000 - ang_d * 10000 - ang_f * 100);
if (ang_m < Math.Pow(10, -10))
{
ang_m = 0;
}
ang_hd = (((ang_d * 3600 + ang_f * 60 + ang_m) / 3600) * Pi) / 180;
if (flag_1 == false)
{
return (-1) * ang_hd;
}
else
{
return ang_hd;
}
}
}
}
将上述代码保存为*.CS文件,通过调用:
csc/target:library /out:name.dll *.cs
即可以编译成name.dll文件
补充:win+r 、cmd、在cmd窗口,输入cd加一个空格 将C:\Windows\Microsoft.NET\Framework\v4.0.30319文件夹拖到窗口
不同版本的net.framework文件名不一样。然后就可以调用上面的函数了。
library后面要有一个空格,否则会报错
fatal error CS2019: /target 的目标类型无效:
必须指定“exe”、“winexe”、“library”或“module”
调用时:
1、项目里的References上右键,选择Reference,选择Brouse选项卡,浏览找到编译的DLL,确定添加
,该DLL就被添加到项目的BIN里。
2、如果有名称空间,要添加名称空间,如我的是MyDll,如果没有名称空间,则不用添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Runtime.InteropServices;
using MyDll;
namespace DLL
{
//[DllImport("mymethod.dll")]
class Program
{
static void Main(string[] args)
{
zb_XY zbxy = new zb_XY();
zbTrans_4para para = new zbTrans_4para();
TransCode tcode = new TransCode();
para.disX = 5.488;
para.disY = 100.964;
para.angX = 0.2;
para.m = 0;
zbxy.X = 234567.43234;
zbxy.Y = 345678.3445;
double outx;
double outy;
tcode.Trans_XY_Fmodel(zbxy.X, zbxy.Y, out outx, out outy, para);
Console.WriteLine("{0}\n{1}", outx, outy);
Console.ReadKey();
}
}
}
3、对于有名称空间的dll,如果没用引用名称空间,当需要实例化对象时,要带上名称空间
如 MyDll.zb_xy zbxy=new zb_xy();静态方法不用实例化对象。用MyDll.Trans_jd_hu直接调用