【问题标题】:error C2664: 'wcscmp' : cannot convert parameter 1 from 'CHAR [260]' to 'const wchar_t *'错误 C2664:“wcscmp”:无法将参数 1 从“CHAR [260]”转换为“const wchar_t *”
【发布时间】:2021-02-14 21:28:48
【问题描述】:

我有这段代码可以在 vs2019 上运行

#include <stdio.h>
#include <winsock2.h>
#include <iostream>
#include <tlhelp32.h>
#include <wchar.h>
#include <atlstr.h>
#include <windows.h>
PROCESSENTRY32 pe32 = { 0 };
HANDLE    hSnap;
int       iDone;
int       iTime = 60;
bool      bProcessFound;
using namespace std;
int x = 1;
int i = 0;

int main() {
    system("cmd /C \"\"C:\\Windows\\System32\\tool.exe\"\"");
    /*WinExec("cmd /C \"\"C:\\Windows\\System32\\tool.exe\"\"", SW_HIDE);*/
    Sleep(1000);
    /*for (i = 0; i < x; i++) {

        Sleep(1000);
        cout << i << "\n";
        x++;
        if (i == 10) i = 0;
    }*/

    while (true)    // go forever
    {
        hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        pe32.dwSize = sizeof(PROCESSENTRY32);
        Process32First(hSnap, &pe32);     // Can throw away, never an actual app

        bProcessFound = false;   //init values
        iDone = 1;

        while (iDone)    // go until out of Processes
        {
            iDone = Process32Next(hSnap, &pe32);
            if (wcscmp(pe32.szExeFile, CString("tool.exe")) == 0)    // Did we find our process?
            {
                bProcessFound = true;
                iDone = 0;
                cout << "process found" << "\n";
            }
        }

        if (!bProcessFound)    // if we didn't find it running...
        {
            cout << "process not found" << "\n";
        }
        Sleep(iTime * 500);    // delay x amount of seconds.
    }

    
}

但是当我把它用旧版本的 vs(2003 版)编译时,我得到了这个错误

错误 C2664: 'wcscmp' : 无法将参数 1 从 'CHAR [260]' 转换为 'const wchar_t *' 指向的类型不相关;转换需要 reinterpret_cast、C-style cast 或 function-style cast

我该怎么办?

【问题讨论】:

  • 你需要在你的预处理器定义中添加UNICODE_UNICODE,所以像PROCESSENTRY32这样的东西解析为PROCESSENTRY32W
  • 我按照先生的建议做了,但还是没有运气。
  • “我按照你的建议做了”对我有用,你做错了什么,请发帖minimal reproducible example
  • @n.'pronouns'm。如果它对你有用,那么对你有好处。供您参考,上面发布的程序附加在一个非常大的系统上。
  • 如果它对我有用,但对你无效,这意味着我做对了,而你做错了。如果你想知道你做错了什么,你需要先展示你在做什么。 “我按照您的建议做了”并不能充分描述您所做的事情。

标签: c++ visual-c++


【解决方案1】:

如果字符串以空字符结尾(您的字符串看起来就是这样),您可以简单地将指针传递给第一个字母。应该可以使用 2003 编译器就好了。

这是一个复制粘贴的例子:

#include <string>
#include <iostream>

using namespace std;

int main() {
    wstring str1 = L"We are equal!";
    wstring str2 = L"We are equal!";
    wstring str3 = L"I'm special!";

    bool TWO_FIRST_EQUAL      = wcscmp(&str1[0], &str2[0]) == 0;
    bool LAST_AND_FIRST_EQUAL = wcscmp(&str1[0], &str3[0]) == 0;
    //                                 ^    ^^^  ^    ^^^

    wcout << L"Are the first two strings equal?: "    << (TWO_FIRST_EQUAL      ? L"YES" : L"NO") << endl << endl;
    wcout << L"Is the last one equal to the first?: " << (LAST_AND_FIRST_EQUAL ? L"YES" : L"NO") << endl << endl;
}

【讨论】:

  • 要比较两个wstring 变量,您只需执行str1 == str2。无需使用指针调用 C 函数。
猜你喜欢
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多