Shell_NotifyIconA / Shell_NotifyIconW ...有什么区别? [英] Shell_NotifyIconA / Shell_NotifyIconW...whats the difference?
问题描述
我正在将一些Win32代码移植到C#,并且已经有几个具有相同名称和使用相同结构的函数,除了它们以A和W结尾。
I am porting some Win32 code to C#, and have come accross several functions which have the same names and use the same structures except that they end with A and W
例如:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("shell32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool Shell_NotifyIconA(uint dwMessage, ref NOTIFYICONDATAA lpData);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("shell32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool Shell_NotifyIconW(uint dwMessage, ref NOTIFYICONDATAW lpData);
我在C#中看到了一些实现,从函数名和函数名中省略了A和W,如下:
I have seen some implementations in C# which omit the A and W from both the function name, and the associated structure, like so:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("shell32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool Shell_NotifyIcon(uint dwMessage, ref NOTIFYICONDATA lpData);
我想知道的是:
-
A和W代表什么?
What do A and W denote?
在C#中,
推荐答案
A后缀(代表ANSI)意味着该函数与输入多字节字符串(可能是 NOTIFYICONDATA
是字符串),而W(代表Wide)意味着该函数与输入Unicode字符串一起使用。
The "A" suffix (which stands for ANSI) means that the function works with input multi-byte strings (probably some members of NOTIFYICONDATA
are strings), while "W" (which stands for "Wide") means the function works with input Unicode strings.
最好尽可能使用Unicode字符串,因为这是内部操作系统使用的。因此,如果您的项目通过传递多字节字符串调用Windows API,则最终会在内部进行到Unicode的转换(如果操作系统还会返回,则可能会第二次转换为多字节
In Windows, it is better to work with Unicode strings whenever possible, because that's what the OS internally uses. Thus, if your project invokes a Windows API by passing a multi-byte string, a conversion to Unicode will eventually happen internally (and possibly a second conversion back to multi-byte if the OS also returns you a string), slowing things down.
通常,当函数 X
的某个数据结构的两个版本存在时多字节和Unicode字符,它们分别给出名称(分别) XA
和 XW
,以及条件别名 X
定义为根据您的项目设置( #ifdef
切换)解析为一个或另一个。
Usually, when two versions of a certain data structure of function X
exist for multi-byte and Unicode characters, they are given the names (respectively) XA
and XW
, and a conditional alias X
is defined to resolve into one or the other based on your project's settings (an #ifdef
does the switch).
我对C#不够了解回答问题的第二部分。
I do not know enough about C# to answer the second part of the question.
如 David Heffernan ,p / invoke marshaller将把该名称解析为与 DllImport
属性中使用的 CharSet
匹配的名称。
As pointed out by David Heffernan, the p/invoke marshaller will resolve the name to the one that matches the CharSet
used in the DllImport
attribute.
这篇关于Shell_NotifyIconA / Shell_NotifyIconW ...有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!