博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DLLImport
阅读量:7082 次
发布时间:2019-06-28

本文共 4111 字,大约阅读时间需要 13 分钟。

namespace Wintellect.Interop.Sound{    using System;     using System.Runtime.InteropServices;     using System.ComponentModel;     sealed class Sound    {         public static void MessageBeep(BeepTypes type)        {             if(!MessageBeep((UInt32) type))             {                     Int32 err = Marshal.GetLastWin32Error();                      throw new Win32Exception(err);              }         }        [DllImport("User32.dll", SetLastError=true)]         static extern Boolean MessageBeep(UInt32 beepType);                 private Sound()        {        }     }     enum BeepTypes{          Simple = -1,          Ok = 0x00000000,          IconHand = 0x00000010,          IconQuestion = 0x00000020,          IconExclamation = 0x00000030,         IconAsterisk = 0x00000040          }  }

 C 和 Win32 的一些公共数据类型及其规范,以及一个具有匹配规范的公共语言运行库类型

Win32 Types Specification CLR Type
char, INT8, SBYTE, CHAR† 8-bit signed integer System.SByte
short, short int, INT16, SHORT 16-bit signed integer System.Int16
int, long, long int, INT32, LONG32, BOOL†, INT 32-bit signed integer System.Int32
__int64, INT64, LONGLONG 64-bit signed integer System.Int64
unsigned char, UINT8, UCHAR†, BYTE 8-bit unsigned integer System.Byte
unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR†, __wchar_t 16-bit unsigned integer System.UInt16
unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT 32-bit unsigned integer System.UInt32
unsigned __int64, UINT64, DWORDLONG, ULONGLONG 64-bit unsigned integer System.UInt64
float, FLOAT Single-precision floating point System.Single
double, long double, DOUBLE Double-precision floating point System.Double
†In Win32 this type is an integer with a specially assigned meaning; in contrast, the CLR provides a specific type devoted to this meaning.

 指针参数

指针增加了封送数据的复杂性,因为它们增加了一个间接层。使用托管代码表示此附加间接层的方式有多种。

FileEncryptionStatus API 函数

BOOL FileEncryptionStatus(  LPCTSTR lpFileName,  // file name  LPDWORD lpStatus     // encryption status);

工作方式是调用程序向该函数传递指向一个 DWORD 变量的指针,而该 API 函数用状态值填充指向的内存位置。

 调用非托管 FileEncryptionStatus 函数的可能外部方法定义:

[DllImport("Advapi32.dll", CharSet=CharSet.Auto)]static extern Boolean FileEncryptionStatus(String filename, out UInt32 status);

该定义使用 out 关键字来为 UInt32 状态值指示 by-ref 参数。这里我也可以选择 ref 关键字,实际上在运行时会产生相同的机器码。out 关键字只是一个 by-ref 参数的规范,它向 C# 编译器指示所传递的数据只在被调用的函数外部传递。相反,如果使用 ref 关键字,则编译器会假定数据可以在被调用的函数的内部和外部传递。

(The definition uses the out keyword to indicate a by-ref parameter for the UInt32 status value. I could have selected the ref keyword here as well, and in fact both result in the same machine code at run time. The out keyword is simply a specialization of a by-ref parameter that indicates to the C# compiler that the data being passed is only being passed out of the called function. In contrast, with the ref keyword the compiler assumes that data may flow both in and out of the called function.)

封送不透明 (Opaque) 指针:一种特殊情况

当一个不透明指针返回给您的应用程序(或者您的应用程序期望得到一个不透明指针)时,您应该将参数或返回值封送为 CLR 中的一种特殊类型 — System.IntPtr。

请记住,任何返回或接受句柄的 API 函数其实操作的就是不透明指针。您的代码应该将 Windows 中的句柄封送成 System.IntPtr 值。

当使用 Windows API 函数时,因为指针应是不透明的,所以除了存储和传递给外部方法外,不能将它们另做它用。

这种“只限存储和传递”规则的两个特例是当您需要向外部方法传递 null 指针值和需要比较 IntPtr 值与 null 值的情况。为了做到这一点,您不能将零强制转换为 System.IntPtr,而应该在 IntPtr 类型上使用 Int32.Zero 静态公共字段,以便获得用于比较或赋值的 null 值。

封送文本

  • 是您的应用程序向 API 函数传递文本数据,还是 API 函数向您的应用程序返回字符串数据?或者二者兼有?

  • 您的外部方法应该使用什么托管类型

  • API 函数期望得到的是什么格式的非托管字符串

 

UINT_PTR SHAppBarMessage(  _In_     DWORD dwMessage,  _Inout_  PAPPBARDATA pData);
typedef struct _AppBarData {  DWORD  cbSize;  HWND   hWnd;  UINT   uCallbackMessage;  UINT   uEdge;  RECT   rc;  LPARAM lParam;} APPBARDATA, *PAPPBARDATA;
typedef struct _RECT {  LONG left;  LONG top;  LONG right;  LONG bottom;} RECT, *PRECT;
struct AppBarData    {        public UInt32 cbSize;        public IntPtr hWnd;        public UInt32 uCallbackMessage;        public UInt32 uEdge;        public AppBarRect rc;        public IntPtr lParam;    }    struct AppBarRect    {        public Int32 left;        public Int32 top;        public Int32 right;        public Int32 bottom;    }

 

转载于:https://www.cnblogs.com/niaomingjian/p/3921181.html

你可能感兴趣的文章
Python面向对象编程 - 一个记事本程序范例(一)
查看>>
马桶餐厅
查看>>
我对程序员技能的一些认识
查看>>
在linux下如何修改oracle的sys和system的密码
查看>>
【C语言】01-C语言概述
查看>>
mysql FullText全文索引的问题
查看>>
空格&nbsp在不同浏览器中显示距离不一致问题解决方法
查看>>
Dynamic CRM 2013学习笔记(八)过滤查找控件 (类似省市联动)
查看>>
iOS执行时与method swizzling
查看>>
SQL点滴21—几个有点偏的语句
查看>>
Android各种效果集合
查看>>
【转】Geary's C
查看>>
Linux中查看socket状态(转)
查看>>
public-private-protected-默认缺省 的区别
查看>>
React Native上手
查看>>
0919 - iPaste 上架 App Store
查看>>
iKcamp&掘金Podcast直播回顾(12月2号和9号的两场)
查看>>
Java简短知识点
查看>>
Hibernate第八篇【懒加载】
查看>>
[面试∙网络] TCP/IP(四):TCP 与 UDP 协议简介
查看>>