cannot convert parameter 2 from 'const char[15]' to 'LPWSTR';

来源:互联网 发布:无线传感器网络的用途 编辑:程序博客网 时间:2024/05/16 12:26

用于监控另一个进程,发现该进程关掉了就自动把它重启。

可以用脚本程序,如vbs或者bat实现。下面程序使用C++实现:用于监视目标程序HTServer.exe,如果目标程序没有在运行,则运行目标程序。

代码如下:

[cpp] view plaincopyprint?
  1. // HTServerMonitor.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include <iostream>  
  5. #include <windows.h>  
  6. #include <stdio.h>  
  7. #include <tchar.h>  
  8.   
  9. using namespace std;  
  10. int _tmain(int argc, _TCHAR *argv[])  
  11. {  
  12.     STARTUPINFO si;  
  13.   
  14.     PROCESS_INFORMATION pi; //进程信息:  
  15.   
  16.     ZeroMemory(&si, sizeof(si));  
  17.     si.cb = sizeof(si);  
  18.     ZeroMemory(&pi, sizeof(pi));  
  19.     do{  
  20.         // 创建子进程,判断是否执行成功  
  21.         if(!CreateProcess( NULL,"cmd /c C:\\Users\\hk\\Desktop\\HTVersions\\HTServer\\HTServerEditVersion4.2\\Debug\\HTServer.exe",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))  
  22.         {  
  23.             cout << "创建进程失败.." << GetLastError() << endl;  
  24.             system("pause"); //用于测试  
  25.             return 0;  
  26.         }  
  27.         //进程执行成功,打印进程信息  
  28.         cout << "以下是子进程的信息:" << endl;  
  29.         cout << "进程ID pi.dwProcessID: " << pi.dwProcessId << endl;  
  30.         cout << "线程ID pi.dwThreadID : " << pi.dwThreadId << endl;  
  31.         // 等待知道子进程退出...  
  32.         WaitForSingleObject( pi.hProcess, INFINITE);//检测进程是否停止  
  33.         //WaitForSingleObject()函数检查对象的状态,如果是未确定的则等待至超时  
  34.         //子进程退出  
  35.         cout << "子进程已经退出..." << endl;  
  36.         //关闭进程和句柄  
  37.         CloseHandle(pi.hProcess);  
  38.         CloseHandle(pi.hThread);  
  39.         //system("pause");//执行完毕后等待  
  40.     }while(true);//如果进程推出就再次执行方法  
  41.     exit(0);  
  42.     return 0;  
  43. }   
以上代码自:http://flylynne.iteye.com/blog/580751

在VC6下编译运行没有问题,但是当用VS08的时候,就会报错:

 error C2664:'CreateProcessW' : cannot convert parameter 2 from 'const char[15]' to 'LPWSTR';

解决方法:打开 属性->常规->字符集设置成“未设置”。

原创粉丝点击