您的位置:首页 > 编程语言 > C#

extern外部方法使用C#简单例子

2014-12-21 23:11 417 查看
外部方法使用C#简单例子

1、增加引用using System.Runtime.InteropServices;

2、声明和实现的连接[DllImport("kernel32", SetLastError = true)]

3、声明外部方法public static extern int GetCurrentDirectory(int a, StringBuilder b);

4、对外部方法操作 GetCurrentDirectory(300, pathstring);

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;//引用外部

namespace extern
{
public partial class DllImportForm : Form
{
public DllImportForm()
{
InitializeComponent();
}

[DllImport("kernel32", SetLastError = true)]//声明和实现的连接
public static extern int GetCurrentDirectory(int a, StringBuilder b);//外部方法

private void btnDisplay_Click(object sender, EventArgs e)
{
StringBuilder pathstring=new StringBuilder ();//返回路径
GetCurrentDirectory(300, pathstring);
this.listBox1.Items.Add (pathstring );

}
}
}


文件在运行时出现"vshost32.exe停止运行",发现编译的文件换个目录后就可以正常运行了。

此文件由朱朱编写,转载请注明出自朱朱家园http://blog.csdn.net/zhgl7688
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: