口语考试制作(三)——使用DllImport调用非托管函数
2008-12-07 13:34
267 查看
前一篇文章分析了制作口语考试系统的几种方法,JMF好像口碑不是很好,而且客户端还需要下载JMF的框架,RED5和FMS都没有使用过,而微软好像没有提供类似java的applet那样的网页端小程序,因为胖客户端应用已经被flash占了比较大的又是,silverlight也是刚刚开始推广。所以,我决定选择使用C/S的架构。
第一个要解决的问题就是就是在托管程序中调用非托馆函数的问题。
使用DllImport来调用win32 API. DllImport在命名空间System.Runtime.InteropServices中.
DllImport是用于指明声明的函数。
DllImport的使用形式为:[DllImport("User32.dll", EntryPoint = "MessageBox", CharSet = CharSet.Auto)]
其中,User32.dll是要调用的dll名,EntryPoint指明了调用的入口点,即是调用dll中的哪个函数。CharSet指明了调用的字符集。字符集包括ansi和unicode两种。
DllImport描述的函数必须是用static和extern加以修饰。
举例说明:
声明原型:
using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Runtime.InteropServices;
5
6namespace RecordTest
7
调用函数
Code
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Runtime.InteropServices;
9
10 namespace RecordTest
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 private void button1_Click(object sender, EventArgs e)
20 {
21 LipWrap.MsgBox(0, "hello,world", "njj", 0);
22 }
23
24
25
26 }
27
28
29
30
31 }
运行程序后,当点击窗口中的按钮时,就会出现一个hello world的弹出窗口。
附:由于托管代码和非托管代码的数据类型是不兼容的,必须使用托管代码中的数据类型对非托管代码的数据类型进行替换
下表引自MSDN,列出了非托管代码对应的托管代码的数据类型
下表列出了在 Win32 API(在 Wtypes.h 中列出)和 C 样式函数中使用的数据类型。许多非托管库包含将这些数据类型作为参数传递并返回值的函数。第三列列出了在托管代码中使用的相应的 .NET Framework 内置值类型或类。某些情况下,您可以用大小相同的类型替换此表中列出的类型。
下表将说明 Win32 API 中几个常用的 DLL。
win32API提供的多媒体API叫MCI,在system32目录下的winmm.dll中包含了该函数。
第一个要解决的问题就是就是在托管程序中调用非托馆函数的问题。
使用DllImport来调用win32 API. DllImport在命名空间System.Runtime.InteropServices中.
DllImport是用于指明声明的函数。
DllImport的使用形式为:[DllImport("User32.dll", EntryPoint = "MessageBox", CharSet = CharSet.Auto)]
其中,User32.dll是要调用的dll名,EntryPoint指明了调用的入口点,即是调用dll中的哪个函数。CharSet指明了调用的字符集。字符集包括ansi和unicode两种。
DllImport描述的函数必须是用static和extern加以修饰。
举例说明:
声明原型:
using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Runtime.InteropServices;
5
6namespace RecordTest
7
调用函数
Code
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Runtime.InteropServices;
9
10 namespace RecordTest
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 private void button1_Click(object sender, EventArgs e)
20 {
21 LipWrap.MsgBox(0, "hello,world", "njj", 0);
22 }
23
24
25
26 }
27
28
29
30
31 }
运行程序后,当点击窗口中的按钮时,就会出现一个hello world的弹出窗口。
附:由于托管代码和非托管代码的数据类型是不兼容的,必须使用托管代码中的数据类型对非托管代码的数据类型进行替换
下表引自MSDN,列出了非托管代码对应的托管代码的数据类型
下表列出了在 Win32 API(在 Wtypes.h 中列出)和 C 样式函数中使用的数据类型。许多非托管库包含将这些数据类型作为参数传递并返回值的函数。第三列列出了在托管代码中使用的相应的 .NET Framework 内置值类型或类。某些情况下,您可以用大小相同的类型替换此表中列出的类型。
Wtypes.h 中的非托管类型 | 非托管 C 语言类型 | 托管类名 | 说明 |
---|---|---|---|
HANDLE | void* | System.IntPtr | 在 32 位 Windows 操作系统上为 32 位,在 64 位 Windows 操作系统上为 64 位。 |
BYTE | unsigned char | System.Byte | 8 位 |
SHORT | short | System.Int16 | 16 位 |
WORD | unsigned short | System.UInt16 | 16 位 |
INT | int | System.Int32 | 32 位 |
UINT | unsigned int | System.UInt32 | 32 位 |
LONG | long | System.Int32 | 32 位 |
BOOL | long | System.Int32 | 32 位 |
DWORD | unsigned long | System.UInt32 | 32 位 |
ULONG | unsigned long | System.UInt32 | 32 位 |
CHAR | char | System.Char | 用 ANSI 修饰。 |
LPSTR | char* | System.String 或 System.Text.StringBuilder | 用 ANSI 修饰。 |
LPCSTR | Const char* | System.String 或 System.Text.StringBuilder | 用 ANSI 修饰。 |
LPWSTR | wchar_t* | System.String 或 System.Text.StringBuilder | 用 Unicode 修饰。 |
LPCWSTR | Const wchar_t* | System.String 或 System.Text.StringBuilder | 用 Unicode 修饰。 |
FLOAT | Float | System.Single | 32 位 |
DOUBLE | Double | System.Double | 64 位 |
DLL | 内容说明 |
---|---|
GDI32.dll | 用于设备输出的图形设备接口 (GDI) 函数,例如用于绘图和字体管理的函数。 |
Kernel32.dll | 用于内存管理和资源处理的低级别操作系统函数。 |
User32.dll | 用于消息处理、计时器、菜单和通信的 Windows 管理函数。 |
相关文章推荐
- C# DllImport 系统调用使用详解、托管代码的介绍、EntryPoint的使用
- 使用PDF制作vce考试文件
- 使用C# 跨平台调用 p/invoke DllImport
- 使用FlashCS制作UI导出swc在Actionscript中调用
- 使用C#制作简单dll文件,并测试调用
- eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?
- 使用FlashCS制作UI导出swc在Actionscript中调用
- C#使用DllImport调用非托管的代码
- C#使用DllImport调用非托管的代码的方法
- 动态链接库调用过程中不使用__declspec(dllimport)为何仍能调用?
- 使用installShield制作一个含调用设置、服务注册及驱动程序的安装包
- C#使用DllImport调用非托管的代码
- ecshop模板制作 中 foreach 循环次数的调用及使用技巧
- C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用
- C#中使用DllImport调用C++dll
- 在线口语考试制作(一)
- C#使用DllImport调用非托管的代码
- 手把手一个一个脚印制作使用jni(java调用c库)
- C#动态调用系统DLL函数的类(不使用DllImport)
- 【转帖】C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用