在C#中调用windows API函数

转自:http://www.91tech.net/Article/SoftTech/csharp/200410/77.html

对于windows 系统API函数的调用在程序设计中有时是必不可少的,各种编程语言都规范了调用的方法和接口,在C#语言中的调用方法如下(以下编程环境为Visual Studio .NET):

(1) 在工程项目中添加一个类新项,打开这个类文件,在文件头部加入对以下命名空间的引用

using System.Runtime.InteropServices; 

(2)在类定义主体中,以静态调用的方式加入对API的引用

本文以下的API调用为例:

/// 打开和关闭CD托盘.
[DllImport("winmm.dll" , EntryPoint="mciSendString", CharSet=CharSet.Auto)]
public static extern int mciSendString (string lpstrCommand,string lpstrReturnstring ,int uReturnLength,int hwndCallback);

/// <summary>
/// 显示和隐藏鼠标指针.
/// </summary>
[DllImport("user32.dll", EntryPoint="ShowCursor", CharSet=CharSet.Auto)]
public static extern int ShowCursor(int bShow);

/// 清空回收站.
[DllImport("shell32.dll", EntryPoint="SHEmptyRecycleBin", CharSet=CharSet.Auto)]

public static extern long SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, long dwFlags);

/// 打开浏览器

[DllImport("shell32.dll", EntryPoint="ShellExecute", CharSet=CharSet.Auto)]
public static extern int ShellExecute(IntPtr hwnd,string lpOperation,string lpFile,string lpParameters,string lpDirectory,int nShowCmd);

/// 最大化窗口,最小化窗口,正常大小窗口;
[DllImport("user32.dll", EntryPoint="ShowWindow", CharSet=CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd,int nCmdShow);

(3)有了上面的文件后,就可以在自己的窗体对象的事件处理中调用以上的API,方法如下:

以下strReturn是string类型的公有变量,ApiCalls指代第一步创建的类名。

///打开CD托盘:
long lngReturn = ApiCalls.mciSendString("set CDAudio door open", strReturn, 127, 0);

///关闭CD托盘:
long lngReturn = ApiCalls.mciSendString("set CDAudio door closed", strReturn, 127, 0);

///在应用程序窗体中显示鼠标指针:
ApiCalls.ShowCursor(1);

///在应用程序窗体中隐藏鼠标指针:
ApiCalls.ShowCursor(0);

///清空回收站:
ApiCalls.SHEmptyRecycleBin(Form.ActiveForm.Handle,"",0x00000000);

///打开浏览器窗口,textBox1.Text中表示要访问的URL地址:
Long lngReturn= ApiCalls.ShellExecute(Form.ActiveForm.Handle,"Open",textBox1.Text,"","",1);

///最大化窗口:
ApiCalls.ShowWindow(Form.ActiveForm.Handle,3); 

///最小化窗口:
ApiCalls.ShowWindow(Form.ActiveForm.Handle,2);

///恢复正常大小窗口:
ApiCalls.ShowWindow(Form.ActiveForm.Handle,1);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#调用Matlab函数可以通过以下两种方式实现: ### 1. 使用Matlab Engine API Matlab Engine API提供了一组接口,可以在C#加载和执行Matlab函数。具体步骤如下: 1. 安装Matlab Engine API。Matlab Engine API需要和Matlab一起安装,安装时需要选择Matlab Engine API选项。 2. 在C#项目添加对Matlab Engine API的引用。在Visual Studio,右键项目,选择“引用”,然后选择“添加引用”,在COM选项卡选择“Matlab Application”。 3. 在C#代码加载和执行Matlab函数。示例代码如下: ```csharp // 创建Matlab Engine对象 var matlab = new Matlab(); // 执行Matlab函数 matlab.Execute("addpath('C:\\path\\to\\matlab\\function\\folder')"); matlab.Execute("result = myfunction(arg1, arg2)"); // 获取Matlab函数的返回值 double[] result = matlab.GetVariable("result", "base") as double[]; // 释放Matlab Engine对象 matlab.Quit(); ``` ### 2. 使用Matlab Compiler SDK Matlab Compiler SDK可以将Matlab函数编译成.NET组件,然后在C#调用。具体步骤如下: 1. 安装Matlab Compiler SDK。Matlab Compiler SDK需要和Matlab一起安装,安装时需要选择Matlab Compiler SDK选项。 2. 在Matlab编译函数。在Matlab命令行输入以下命令: ```matlab mcc -W "dotnet:MyComponent,MyNamespace,1.0" -T link:lib myfunction.m ``` 其,MyComponent是.NET组件的名称,MyNamespace是.NET命名空间的名称,1.0是.NET组件的版本号,myfunction.m是需要编译的Matlab函数。 3. 在C#项目添加对.NET组件的引用。在Visual Studio,右键项目,选择“引用”,然后选择“添加引用”,选择“浏览”,找到编译出来的.NET组件,添加引用。 4. 在C#代码调用.NET组件。示例代码如下: ```csharp // 创建.NET组件对象 var component = new MyComponent(); // 调用Matlab函数 double[] result = component.myfunction(arg1, arg2); // 释放.NET组件对象 component.Dispose(); ``` 需要注意的是,使用Matlab Compiler SDK编译的.NET组件需要在部署时安装Matlab Runtime,以保证能够正确运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值