extern(C# 参考)
来源:互联网 发布:卖家如何加入淘宝客 编辑:程序博客网 时间:2024/05/21 17:34
extern修饰符用于声明在外部实现的方法。
extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 属性一起使用;在这种情况下,该方法还必须声明为 static,如下面的示例所示:
[DllImport("avifil32.dll")]
private static extern void AVIFileInit();
注意
extern 关键字还可以定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。
将 abstract 和 extern 修饰符一起使用来修改同一成员是错误的。
使用
extern 修饰符意味着方法在 C# 代码的外部实现,而使用 abstract 修饰符意味着在类中未提供方法实现。注意 extern 关键字在使用上比在 C++ 中有更多的限制。
示例
在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 User32.dll库导入的 MessageBox 方法。
using System; using System.Runtime.InteropServices;
class MainClass
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);
static int Main()
{
string myString;
Console.Write("Enter your message: ");
myString = Console.ReadLine();
return MessageBox(0, myString, "My Message Box", 0);
}
}
此示例使用 C 程序创建一个 DLL,在下一示例中将从 C# 程序调用该 DLL。
// cmdll.c // compile with:
/LD int __declspec(dllexport) SampleMethod(int i)
{
return i*10;
}
该示例使用两个文件 CM.cs 和 Cmdll.c 来说明 extern。
C 文件是示例 2 中创建的外部 DLL,它从C# 程序内调用。
// cm.cs using System;
using System.Runtime.InteropServices;
public class MainClass {
[DllImport("Cmdll.dll")]
public static extern int SampleMethod(int x);
static void Main()
{
Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));
}
}
输出SampleMethod() returns 50. 备注生成项目: 使用 Visual C++ 命令行将 Cmdll.c 编译为 DLL: cl /LD Cmdll.c 使用命令行编译 CM.cs: csc CM.cs 这将创建可执行文件 CM.exe。运行此程序时,SampleMethod 将值 5 传递到 DLL 文件,该文件将此值乘以 10 返回。
extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 属性一起使用;在这种情况下,该方法还必须声明为 static,如下面的示例所示:
[DllImport("avifil32.dll")]
private static extern void AVIFileInit();
注意
extern 关键字还可以定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。
将 abstract 和 extern 修饰符一起使用来修改同一成员是错误的。
使用
extern 修饰符意味着方法在 C# 代码的外部实现,而使用 abstract 修饰符意味着在类中未提供方法实现。注意 extern 关键字在使用上比在 C++ 中有更多的限制。
示例
在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 User32.dll库导入的 MessageBox 方法。
using System; using System.Runtime.InteropServices;
class MainClass
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);
static int Main()
{
string myString;
Console.Write("Enter your message: ");
myString = Console.ReadLine();
return MessageBox(0, myString, "My Message Box", 0);
}
}
此示例使用 C 程序创建一个 DLL,在下一示例中将从 C# 程序调用该 DLL。
// cmdll.c // compile with:
/LD int __declspec(dllexport) SampleMethod(int i)
{
return i*10;
}
该示例使用两个文件 CM.cs 和 Cmdll.c 来说明 extern。
C 文件是示例 2 中创建的外部 DLL,它从C# 程序内调用。
// cm.cs using System;
using System.Runtime.InteropServices;
public class MainClass {
[DllImport("Cmdll.dll")]
public static extern int SampleMethod(int x);
static void Main()
{
Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));
}
}
输出SampleMethod() returns 50. 备注生成项目: 使用 Visual C++ 命令行将 Cmdll.c 编译为 DLL: cl /LD Cmdll.c 使用命令行编译 CM.cs: csc CM.cs 这将创建可执行文件 CM.exe。运行此程序时,SampleMethod 将值 5 传递到 DLL 文件,该文件将此值乘以 10 返回。
- extern(C# 参考)
- extern(C# 参考)
- switch(C# 参考)
- base(C# 参考)
- abstract(C# 参考)
- 接口(C# 参考)
- partial(C# 参考)
- ref(C# 参考)
- sealed(C# 参考)
- virtual(C# 参考)
- const(C# 参考)
- #region(C# 参考)
- #region(C# 参考)
- ref(C# 参考)
- base(C# 参考)
- enum(C# 参考)
- enum(C# 参考)
- as(C# 参考)
- 你适合学习程序设计吗?
- JAVA解析XML文件四种方式
- 我应该知道的。
- 女人最常用的拒绝的话(以及她们真正的意思...)
- C++反汇编揭秘1 – 一个简单C++程序反汇编解析
- extern(C# 参考)
- C#中如何使用Jmail库
- SQL UNION 和 UNION ALL
- ls 顏色設定(in Bash shell)
- Sql Server 2005 维护计划 调度修改
- SQL Plus常用命令
- 一次性加载树的所有节点在数据多的时候会使树的响应时间延长
- SqlServer 填充因子的说明
- Oracle数据库中管理表空间和数据文件