All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
visual c++控件使用实例
Bind和Eval的区别详解_[Asp.Net教程]
.NET内置对象之Server对象_[Asp.Net教程]
php常用数据库备份类_[PHP教程]
Delphi带进度条的状态栏实例
PHP,内存管理器符号比较多个溢出漏洞_php资料_编程技术
php+mysq 修改用户密码(用password加密)_[PHP教程]
创建基于AJAX技术的Scribble应用程序_[Asp.Net教程]
Sql,server,2005的XML最佳实施策略(4)_[SQL,Server教程]
SQLSERVER2005的混和身份验证模式的设置以及SA登陆问题_[SQL Server教程]
Reading number is top 10 pictures
So beauty, will let you spray blood2
29 the belle stars after bath figure3
The hot big eye big breast beauty3
海底兵马俑
Absolutely shocked. National geographic 50 animal photographys1
做运动的校花1
2012 national geographic daily picture8
The woman fight much more than men
30 beautiful school beauty2
The money of more than 100 countries and regions22
Download software ranking
Unix video tutorial2
Eclipse 4.2.2 For Win64
Unix video tutorial3
SQL2000 For 4IN1
C语言教程TXT
WebService在.NET中的实战应用教学视频 → 第4集
Boxer's Top ten classic battle8
Boxer's Top ten classic battle7
美女游泳记
asp.net技术内幕
qq published in(发表于) 2014/7/11 9:21:57 Edit(编辑)
C#教程:C#调用其他编程语言编写的DLL

C#教程:C#调用其他编程语言编写的DLL

C#教程:C#调用其他编程语言编写的DLL

C#调用其他编程语言编写的DLL

在程序开发过程,有时会使用C#调用其他编程语言开发的DLL或调用Windows系统API函数,由于这些DLL都属于非托管动态链接库(DLL),要调用非托管动态链接库(DLL)则需要使用DllImport属性。

DllImport属性指示该属性化方法由非托管动态链接库(DLL)作为静态入口点公开,并提供对从非托管DLL导出的函数进行调用所必需的信息。作为最低要求,必须提供包含入口点的DLL的名称。在使用DllImport属性前,须引用命名空间System.Runtime.InteropServices。

下面的示例说明如何使用DllImport属性调用非托管的DLL。代码如下:

[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=True,

CharSet=CharSet.Unicode, ExactSpelling=True,

CallingConvention=CallingConvention.StdCall)]

public static extern bool MoveFile(String src, String dst);

其中KERNEL32.DLL为DLL文件,extern 修饰符用于声明在外部实现的方法(本示例为MoveFileW方法)。在以上代码中,涉及的参数及说明如表1所示。



表1 DllImport属性参数及说明

示例

C#调用Delphi语言编写的DLL

本示例调用非托管的projectdll.dll文件,并使用Sum方法实现加法运算,如图.1所示。



图1 C#调用Delphi语言编写的DLL实现加法运算

程序代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace _4_03

{

public partial class Form1 : Form

{

public class cCdll

{

[DllImport("projectdll.dll", EntryPoint = "Sum", SetLastError = True, CharSet = CharSet.Unicode, ExactSpelling = True, CallingConvention = CallingConvention.StdCall)]

public static extern double Sum(double x, double y);

}

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

textBox3.Text = cCdll.Sum(Convert.ToDouble(textBox1.Text), Convert.ToDouble (textBox2.Text)). ToString();

}

}

}

完整程序代码如下:

★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace _4_03

{

public partial class Form1 : Form

{

public class cCdll

{

[DllImport("projectdll.dll", EntryPoint = "Sum", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

public static extern double Sum(double x, double y);

}

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

textBox3.Text = cCdll.Sum(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text)).ToString();

}

}

}

★ ★★★★Form1.designer.cs窗体设计文件完整程序代码★★★★★

namespace _4_03

{

partial class Form1

{

///



/// 必需的设计器变量。

///


private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///


/// 如果应释放托管资源,为 true;否则为 false。

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

///


private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.textBox1 = new System.Windows.Forms.TextBox();

this.textBox2 = new System.Windows.Forms.TextBox();


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.