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


外部方法使用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);

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 );
 
         }
     }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM