LPTSTR的测试值

Der*_*rek 0 c winapi

我想要比较两个字符串.我正在关注此页面的命名piper服务器/客户端示例:http://msdn.microsoft.com/en-us/library/windows/desktop/aa365592%28v=vs.85%29.aspx

无论如何,在我的"GetAnswerToRequest"中我定义了一个这样的字符串

LPTSTR选项= TEXT("这是测试");

然后我尝试将它与pchRequest参数进行比较,该参数与客户端发送的字符串相同.我找不到能够解决问题的工作比较.

当我将两个值都放在调试器中时,我看到了一些差异.对于选项,我相信它使用单字节字符:

-       option,15   
        [0x00000000]    0x0054  unsigned short
        [0x00000001]    0x0048  unsigned short
        [0x00000002]    0x0049  unsigned short
        [0x00000003]    0x0053  unsigned short
        [0x00000004]    0x0020  unsigned short
        [0x00000005]    0x0049  unsigned short
        [0x00000006]    0x0053  unsigned short
        [0x00000007]    0x0020  unsigned short
        [0x00000008]    0x0041  unsigned short
        [0x00000009]    0x0020  unsigned short
        [0x0000000a]    0x0054  unsigned short
        [0x0000000b]    0x0045  unsigned short
        [0x0000000c]    0x0053  unsigned short
        [0x0000000d]    0x0054  unsigned short
        [0x0000000e]    0x0000  unsigned short
Run Code Online (Sandbox Code Playgroud)

对于pchRequest值,它看起来像使用双字节字符:

-       pchRequest,15   
        [0x00000000]    0x4854  unsigned short
        [0x00000001]    0x5349  unsigned short
        [0x00000002]    0x4920  unsigned short
        [0x00000003]    0x2053  unsigned short
        [0x00000004]    0x2041  unsigned short
        [0x00000005]    0x4554  unsigned short
        [0x00000006]    0x5453  unsigned short
        [0x00000007]    0x5a00  unsigned short
        [0x00000008]    0x48c0  unsigned short
        [0x00000009]    0x18cf  unsigned short
        [0x0000000a]    0x0000  unsigned short
Run Code Online (Sandbox Code Playgroud)

所以这里的大多数十六进制值都匹配,但pchRequest中每个字符有两个,而我的选项变量中有一个.我认为这是lstrcmp()失败的原因.

此外,看起来pchRequest似乎还有一些额外的字符,但我无法分辨它们的来源.关于如何比较这两个字符串的任何想法,据我所知应该是相同的?

谢谢!

Jon*_*ter 5

你可能有一个Unicode构建,这意味着LPTSTR定义一个宽字符串.但是,您需要与ANSI字符串进行比较.

要执行此操作,只需删除TEXT()宏,然后使用char*.例如,

char* option = "THIS IS A TEST";
Run Code Online (Sandbox Code Playgroud)

然后使用显式lstrcmpA()调用ANSI版本lstrcmp.