LPWSTR比较

LPWSTR comparison

如何正确处理本例中的if语句:

int n_args = 0;
int i;
LPWSTR *args = CommandLineToArgvW(GetCommandLineW(), &n_args);
if (args)
{
    if (n_args >= 2)
    {
        for (i = 1; i < n_args; i++)
        {
            std::cout << args[i] << "\n";
            if (args[i] == L"/D") // <-- here
            {
                std::cout << "Condition met\n";
            }
        }
    }
}

第一个std::cout说明命令行参数已经传递,但是后面的if语句失败

if (wcscmp(L"/D", args[i]) == 0)
{

您正在尝试比较两个指针值。您需要使用 strcmp 或者它的宽字符串版本 wcscmp。此函数returns当字符串包含相同内容时为0。

args[i] 是一个字符串。您需要使用 wcscmp 或 strcmp 来比较两个字符串。 https://msdn.microsoft.com/en-us/library/e0z9k731.aspx