これは簡単なプログラムです。その目的は、システム プロセス ID とプロセス リスト内の場所を表示して、単純に自分自身を再起動することです。コードは次のとおりです。
#include <Windows.h>
#include <stdio.h>
#include "pch.h"
#include <iostream>
using namespace std;
void StartClone(int nCloneID)
{
// extract the file name used for the current
//executable file
TCHAR szFilename[MAX_PATH];
GetModuleFileName(NULL, szFilename, MAX_PATH);
// Format the command line for the child process
//and notify its EXE filename and clone ID
TCHAR szCmdLine[MAX_PATH];
sprintf(szCmdLine, "\"%s\" %d", szFilename, nCloneID);
// STARTUPINFO structure for child processes
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
// must be the size of this structure
si.cb = sizeof(si);
// returned process information for the child process
PROCESS_INFORMATION pi;
// Create processes using the same executable and command line, and
//assign the nature of their child processes
BOOL bCreateOK = ::CreateProcess(
szFilename, // The name of the application that generated the EXE
szCmdLine, // tell the flag that behaves like a child process
NULL, // default process security
NULL, // default thread safety
FALSE, // does not inherit the handle
CREATE_NEW_CONSOLE, // use the new console
NULL, // new environment
NULL, // Current directory
&si, // start information
&pi); // returned process information
// release the reference to the child process
if (bCreateOK)
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
int main(int argc, char* argv[])
{
// Determine the number of processes derived, and the location of the derived process in the process list
int nClone = 0;
// Modify the statement : int nClone;
//First modification: nClone=0;
if (argc > 1)
{
// Extract the clone ID from the second parameter
::sscanf(argv[1], "%d", &nClone);
}
//Second modification: nClone=0;
// Show the process location
std::cout << "Process ID:" << ::GetCurrentProcessId()
<< ", Clone ID:" << nClone
<< std::endl;
// Check if there is a need to create a child process
const int c_nCloneMax = 5;
if (nClone < c_nCloneMax)
{
// Send the command line and clone number of the new process
StartClone(++nClone);
}
// Wait for the response keyboard input to end the process
getchar();
return 0;
}
しかし、コンパイルすると、非常に多くのエラーが発生します。そのうちのいくつかは
Error C2065 'TCHAR': undeclared identifier OS_EX2-1
Error C2146 syntax error: missing ';' before identifier 'szFilename'
Error C2065 'MAX_PATH': undeclared identifier OS_EX2-1
Visual Studio 2010 では問題なく動作しています。「Character Set」プロパティを「Not Set」に設定するだけです。アイデアを持っている人はいますか?? どうもありがとう