wcscmp - 使用此功能时访问冲突 [英] wcscmp - Access violation while using this function

查看:20
本文介绍了wcscmp - 使用此功能时访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我试图将 *PSYSTEM_PROCESS_INFO spi(它的 ImageName 字段)与使用 wcsmp 的字符串进行比较,如下所示:

Basically, I was trying to compare the *PSYSTEM_PROCESS_INFO spi (its ImageName field) with a string using wcsmp as follows :

if (wcscmp(L"Test.exe", spi->ImageName.Buffer))

这似乎给了我一个访问冲突错误.同样据我所知,ImageName 是 UNICODE_STRING 类型,UNICODE_STRING 结构使用 PWSTR 作为 Buffer 字段.那么使用 wcscmp 比较 2 个 PWSTR 是否正确?如果不是,将 spi->ImageName.Buffer 与 C 中的字符串进行比较的替代方法是什么?

This seems to give me an access violation error. Also from what I know, ImageName is of the type UNICODE_STRING and UNICODE_STRING structure uses PWSTR for the Buffer field. So is it right to compare 2 PWSTR using wcscmp? If not what are the alternatives to compare the spi->ImageName.Buffer to a string in C?

推荐答案

wcscmp() 需要以空字符结尾的字符串,但 ImageNameUNICODE_STRING,不能保证以空值结尾.它有一个 Length 成员来表示它包含多少字节(除以 sizeof(WCHAR) 得到字符数).

wcscmp() expects null-terminated strings, but the ImageName is a UNICODE_STRING, which is not guaranteed to be null-terminated. It has a Length member to indicate how many bytes it contains (divide that by sizeof(WCHAR) to get the number of characters).

您可以使用wcsncmp() 代替:

You can use wcsncmp() instead:

if (wcsncmp(L"Test.exe", spi->ImageName.Buffer, spi->ImageName.Length / sizeof(WCHAR)))

否则,请使用 RtlEqualUnicodeString() 代替:

Otherwise, use RtlEqualUnicodeString() instead:

UNICODE_STRING fileName = RTL_CONSTANT_STRING(L"Test.exe");
if (!RtlEqualUnicodeString(&fileName, &(spi->ImageName), FALSE))

这篇关于wcscmp - 使用此功能时访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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