【发布时间】: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++