DllImport-ANSI与Uni​​code [英] DllImport - ANSI vs. Unicode

查看:77
本文介绍了DllImport-ANSI与Uni​​code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于以下测试题的可能答案,我有一些疑问:

I have some questions about the possible answers for the test question bellow:

问题:您编写了以下代码段,以使用平台调用从Win32应用程序编程接口(API)调用函数.

Question: You write the following code segment to call a function from the Win32 Application Programming Interface (API) by using platform invoke.

string personName = "N?el";
string msg = "Welcome" + personName + "to club"!";
bool rc = User32API.MessageBox(0, msg, personName, 0);

您需要定义一个可以最好地封送字符串数据的方法原型.您应该使用哪个代码段?

You need to define a method prototype that can best marshal the string data. Which code segment should you use?

// A.
[DllImport("user32", CharSet = CharSet.Ansi)]
public static extern bool MessageBox(int hWnd, string text, string caption, uint type);
}

// B.
[DllImport("user32", EntryPoint = "MessageBoxA", CharSet = CharSet.Ansi)]
public static extern bool MessageBox(int hWnd,
[MarshalAs(UnmanagedType.LPWStr)]string text,
[MarshalAs(UnmanagedType.LPWStr)]string caption, uint type);
}

// C. - Correct answer
[DllImport("user32", CharSet = CharSet.Unicode)]
public static extern bool MessageBox(int hWnd, string text, string caption, uint type);
}

// D.
[DllImport("user32", EntryPoint = "MessageBoxA", CharSet = CharSet.Unicode)]
public static extern bool MessageBox(int hWnd,
[MarshalAs(UnmanagedType.LPWStr)]string text,
[MarshalAs(UnmanagedType.LPWStr)]string caption,
uint type);
}

为什么正确答案C正确?难道不是A吗?唯一的区别是它将是ANSI而不是Unicode.

Why exactly is the correct answer C? Couldn't it just as well have been A? The only difference is that it would be ANSI instead of Unicode.

我知道它不可能是D,因为我们选择Unicode作为字符集,然后将ANSI函数作为入口点.

I understand that it couldn't be D because we choose Unicode as a character set and then have an ANSI function as an entrypoint.

B为什么不工作?

推荐答案

 string personName = "N?el";

该字符串被该问题所问的确切问题弄乱了.毫无疑问,它看起来像这样:

This string was garbled by the exact problem this question is asking about. No doubt it looked like this in the original:

 string personName = "Nöel";

ö往往是一个问题,它的字符代码不在ASCII字符集中,并且默认系统代码页不支持 .固定MessageBox的ANSI版本(也称为MessageBoxA)时使用的是哪种.真正的功能是MessageBoxW,它采用utf-16编码的Unicode字符串.

The ö tends to be a problem, it has a character code that is not in the ASCII character set and might not be supported by the default system code page. Which is what is used when you pinvoke the ANSI version of MessageBox, aka MessageBoxA. The real function is MessageBoxW, the one that takes a utf-16 encoded Unicode string.

MessageBoxA是Windows的旧版本中使用的旧功能,可以追溯到以前程序仍使用8位字符串的时代.它并没有完全消失,许多C和C ++程序仍然倾向于使用8位编码. MessagBoxA通过将8位编码的字符串转换为Unicode,然后调用MessageBoxW来实现.如果您首先使用Unicode字符串,则with会很慢且有损耗.

MessageBoxA is a legacy function that was used in old versions of Windows, back in the olden days when programs still used 8-bit character strings. It isn't completely gone, lots of C and C++ programs still tend to be stuck with 8-bit encodings. MessagBoxA is implementing by converting the 8-bit encoded strings to Unicode and then calling MessageBoxW. With is slow and lossy if you had a Unicode string in the first place.

因此对4个版本进行评分:

So rating the 4 versions:

A:使用MessageBoxA + 8位编码,很有风险.
B:使用MessageBoxA + Unicode,失败.
C:使用MessageBoxW + Unicode,很好
D:使用MessageBoxA + Unicode,失败.

A: uses MessageBoxA + 8-bit encoding, risky.
B: uses MessageBoxA + Unicode, fail.
C: uses MessageBoxW + Unicode, good
D: uses MessageBoxA + Unicode, fail.

这篇关于DllImport-ANSI与Uni​​code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆