C# Native and COM Interoperability
来源:互联网 发布:知金教育收费多少 编辑:程序博客网 时间:2024/04/29 08:08
C# 调用WIN32 API 要引用 System,Ssytem.Runtim.InteropServices 两个命名空间.
Calling into Native Dlls.
using System;
using System.Runtime.InteropServices;
class MsgBoxTest
{
[DllImport("user32.dll")]
static extern int MessageBox (IntPtr hWnd, string text, string caption,int type);[DllImport("kernel32.dll")]
static extern int GetWindowsDirectory (StringBuilder sb, int maxChars);public static void Main()
{
MessageBox (IntPtr.Zero,"Please do not press this again.", "Attention", 0);StringBuilder s = new StringBuilder (256);
GetWindowsDirectory (s, 256);
Console.WriteLine (s);
}
}
If you are unsure how to call a particular Win32 method, you will usually find an example on the Internet if you search for the
method name and DllImport. The site http://www.pinvoke.net is a wiki that aims to document all Win32 signatures.
Marshaling Common Types
[DllImport("...")]
static extern int Foo ( [MarshalAs (UnmanagedType.LPStr)] string s );
The UnmanagedType enumeration includes all the Win32 and COM types that the
marshaler understands. In this case, the marshaler was told to translate to LPStr,
which is a null-terminated single-byte ANSI string.
Marshaling Classes and Structs
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
class SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Milliseconds;
}
dir
struct SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Milliseconds;
}
[DllImport("kernel32.dll")]
static extern void GetSystemTime (SystemTime t);
static void Main()
{
SystemTime t = new SystemTime();
GetSystemTime (t);
Console.WriteLine (t.Year);
}
- C# Native and COM Interoperability
- COM and .NET Interoperability
- interoperability between COM and .Net Component
- [.NET]C++/CLI and C# Interoperability
- structural and semantic interoperability
- Java and iPhone AES interoperability
- CLR Inside Out: Best Practices for Managed and Native Code Interoperability (托管/非托管代码互通性最佳实践)
- Understanding Classic COM Interoperability With .NET Applications
- Guidelines for COM Interoperability from .NET
- Microsoft .NET and J2EE Interoperability Toolkit
- Performance Considerations for Direct3D9 and WPF Interoperability
- iOS 5 and TLS 1.2 Interoperability Issues
- Wireless Internet and Mobile Computing: Interoperability and Performance
- Walking Through Avalon and Windows Forms Interoperability in Code
- WS-Security Interoperability Issues: WebSphere, JBoss, Axis and .Net
- WS-Security Interoperability Workarounds: WebSphere, JBoss, Axis and .Net
- Libjingle - Google Talk Voice and P2P Interoperability Library
- Application Interoperability: Microsoft .NET and J2EE (Patterns & Practices)
- oracle全文检索的创建及使用
- 元芳,你怎么能随即生成m个数,让其和等于n?
- ai若无声,我宁沉默夜寂无声,伊人浅唱。
- 人应该怎么活着?
- Xcode运行相对应IOS以前版本的模拟器测试
- C# Native and COM Interoperability
- A Creed to Live By
- automake入门教程2
- ORA-01000: 超出打开游标的最大数(解决及原因)
- 追女孩子非常火的一片文章
- 概要设计文档
- JVM,JRE,JDK的区别
- Apache CXF 第一篇:HelloWorld
- vijos1025【动态规划】