Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 24 章 本地和 COM 互操作性

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章介绍如何与本地(非托管)动态链接库(DLL)和组件对象模型(COM)组件集成。除非另有说明,本章提到的类型都存在于SystemSystem.Runtime.InteropServices 命名空间中。

调用本地 DLL

P/InvokePlatform Invocation Services 平台调用服务)的简称,它允许你访问非托管 DLLUnix 上的共享库)中的函数、结构体和回调。

例如,Windows DLLuser32.dll 中定义的MessageBox 函数如下:

int MessageBox (HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

您可以通过声明同名静态方法、应用extern 关键字并添加DllImport 属性来直接调用该函数:

using System;
using System.Runtime.InteropServices;

MessageBox (IntPtr.Zero,
            "Please do not press this again.", "Attention", 0);

[DllImport("user32.dll")]
static extern int MessageBox (IntPtr hWnd, string text, string caption,
                              int type);

System.WindowsSystem.Windows.Forms 命名空间中的MessageBox 类本身也调用类似的非托管方法。

下面是 Ubuntu Linux 的DllImport 示例:

Console.WriteLine ($"User ID: {getuid()}");

[DllImport("libc")]
static extern uint getuid();

CLR 包含一个 marshaler,它知道如何在 .NET 类型和非托管类型之间转换参数和返回值。在 Windows 示例中,int 参数直接转换为函数所期望的四字节整数,而字符串参数则转换为 Unicode 字符(UTF-16 编码)的空端数组。IntPtr 是一个结构体,用于封装非托管句柄;在 32 位平台上它的宽度为 32 位,在 64 位平台上它的宽度为 64 位。在 Unix 上也会发生类似的转换。(从 C# 9 开始,您也可以使用nint 类型,它映射到IntPtr 。)

类型和参数编排

常见类型的编排

在 非托管端,表示给定数据类型的方法可能不止一种。例如,字符串可以包含单字节 ANSI 字符或 UTF-16 Unicode 字符,可以是长度前缀、空端或固定长度。通过MarshalAs 属性,您可以向 CLR 编译器指定所使用的变体,这样它就能提供正确的翻译。下面是一个例子:

[DllImport("...")]
static extern int Foo ( [MarshalAs (UnmanagedType.LPStr)] string s );

UnmanagedType 枚举包括元帅器能理解的所有 Win32 和 COM 类型。在本例中,Marshaler 被告知翻译为 ,这是一个空端单字节 ANSI 字符串。LPStr

在 .NET 方面,您还可以选择使用哪种数据类型。例如,非托管句柄可以映射到 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038