1.首先需要在调用窗体中申明using System.Runtime.InteropServices;
2.其次需要在C#语言源程序中声明外部方法,格式如下:
[DLLImport(“DLL文件”)]
修饰符 extern 返回变量类型 方法名称 (参数列表)
其中:DLL文件:包含定义外部方法的库文件;修饰符:访问修饰符,除了abstract以外在声明方法时可以使用的修饰符;返回变量类型:在DLL文件中你需调用方法的返回变量类型;方法名称:在DLL文件中你需调用方法的名称;参数列表:在DLL文件中你需调用方法的列表。
注意:外部方法的申明位置应该放在方法申明处,一般是放在类里的头部。
3.在调用出,直接调用方法即可。
下面以WinForm调用控制台程序进行举例说明:
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 WindowsFormsApplication1
{
}
附:其实调用控制台更多的是通过创建一个类来调用,原理是一样的,同样附上实例
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
}
这个时候,在调用的时候ConsoleShow pConsoleShow = new ConsoleShow (); pConsoleShow .AllocConsole();