Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Shell\u NotifyIconA/Shell\u NotifyIconW…有什么区别?_C#_C++_Winapi_Interop_Pinvoke - Fatal编程技术网

C# Shell\u NotifyIconA/Shell\u NotifyIconW…有什么区别?

C# Shell\u NotifyIconA/Shell\u NotifyIconW…有什么区别?,c#,c++,winapi,interop,pinvoke,C#,C++,Winapi,Interop,Pinvoke,我正在将一些Win32代码移植到C#,并且已经访问了几个具有相同名称和使用相同结构的函数,只是它们以A和W结尾 例如: [return: MarshalAs(UnmanagedType.Bool)] [DllImport("shell32.dll", CallingConvention = CallingConvention.StdCall)] public static extern bool Shell_NotifyIconA(uint dwMessage, ref NOTIFYICONDA

我正在将一些Win32代码移植到C#,并且已经访问了几个具有相同名称和使用相同结构的函数,只是它们以A和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,如下所示:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("shell32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool Shell_NotifyIcon(uint dwMessage, ref NOTIFYICONDATA lpData);
我想知道的是:

  • A和W代表什么

  • 在C#中,我应该使用A和W来实现上面所示的函数,还是省略A和W而使用单个实现更好

  • “A”后缀(代表ANSI)表示该函数可用于输入多字节字符串(可能是
    notifyiconda
    的某些成员是字符串),而“W”(代表“Wide”)表示该函数可用于输入Unicode字符串

    在Windows中,最好尽可能使用Unicode字符串,因为操作系统内部使用Unicode字符串。因此,如果您的项目通过传递一个多字节字符串来调用Windows API,那么最终将在内部发生到Unicode的转换(如果操作系统也返回字符串,则可能会再次转换回多字节),从而降低速度

    通常,当函数
    X
    的某个数据结构存在多字节和Unicode字符的两个版本时,它们分别被命名为
    XA
    XW
    ,并且定义了一个条件别名
    X
    ,以根据项目的设置解析为一个或另一个(一个
    #ifdef
    执行该开关)

    我对C的了解不够,无法回答问题的第二部分

    编辑:


    正如所指出的,p/invoke marshaller会将名称解析为与
    DllImport
    属性中使用的
    CharSet
    相匹配的名称。

    感谢您的回答。我的直觉是,C的互操作性层会像您所说的那样,根据运行时平台自动将X解析为XA或XW。+1对于答案!@Tolga,在将其标记为解决方案之前,我通常会等待一段时间,以查看是否还有更多答案。如果在几个小时内没有其他答案,则我将将其标记为解决方案。p/invoke封送拆收器将选择与字符集匹配的名称后缀。@DavidHeffernan,如果没有指定字符集属性怎么办?这是默认设置字符集,即ANSI