1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// This program tries to start "Disk Management" (diskmgmt.msc) and get the process ID so the program
// can wait for "Disk Management" to be closed before moving on.
// I know diskmgmt.msc can be started via cmd.exe via CreateProcessA but the process ID
// returned via pi.dwProcessId is not the pid for diskmgmt.msc (my guess it is for cmd.exe)
// diskmgmt.msc can also be started from system(diskmgmt.msc) BUT how can I get the process ID?
// Note: This program is compiled under Microsoft Visual Studio Community 2019 ver 16.2.3
// As a Console application no header files
// On Windows 10 home 64Bit
// ASUS Sabertooth Z77
// 16GB Ram
#include <iostream>
#include <shlwapi.h>
using namespace std;
int main(int argc, TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
// Start the child process.
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
string commandName, commandLine;
LPSTR commandName_LPSTR, commandLine_LPSTR;
commandName = "c:\\Windows\\System32\\diskmgmt.msc";
commandLine = commandName;
commandName_LPSTR = const_cast<char*>(commandName.c_str());
commandLine_LPSTR = const_cast<char*>(commandLine.c_str());
cout << "Try #1" << endl;
cout << "Trying to start \"Disk Management\" using call to \"c:\\Windows\\System32\\diskmgmt.msc\"" << endl;
cout << " Using \"CreateProcessA\"" << endl << endl;
cout << " Trying this way ALWAYS fails!!!" << endl;
cout << " Command Name := \"" << commandName_LPSTR << "\"" << endl;
cout << " Command Line := \"" << commandLine_LPSTR << "\"" << endl << endl;
if (!CreateProcessA(commandName_LPSTR, // Command Name
commandLine_LPSTR, // Command line
NULL, // Arguments to diskmgmt.msc
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
cout << "Try #1 " << commandLine << " failed " << GetLastError() << endl;
}
else
{
cout << "Process ID returned by CreateProcessA Try #1 was: " << pi.dwProcessId << endl;
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
// Try #2
// This process starts the program "Disk Management" but the process ID returns is not that of
// MMC.EXE (Microsoft Management Console) which is the started from diskmgmt.msc or any other running
// process
commandName = "c:\\Windows\\System32\\cmd.exe";
commandLine = commandName;
commandLine.append(" /c C:\\Windows\\System32\\diskmgmt.msc");
commandName_LPSTR = const_cast<char*>(commandName.c_str());
commandLine_LPSTR = const_cast<char*>(commandLine.c_str());
cout << endl << "Try #2" << endl;
cout << "Trying to start \"Disk Management\" using call to \"c:\\Windows\\System32\\cmd.exe\"" << endl;
cout << " with \" /c C:\\Windows\\System32\\diskmgmt.msc\" added to end of commandLine" << endl;
cout << " Using \"CreateProcessA\"" << endl << endl;
cout << " Command Name := \"" << commandName_LPSTR << "\"" << endl;
cout << " Command Line := \"" << commandLine_LPSTR << "\"" << endl << endl;
if (!CreateProcessA(commandName_LPSTR, // Command Name
commandLine_LPSTR, // Command line
NULL, // Arguments to diskmgmt.msc
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
cout << "CreateProcess #2 " << commandLine << " failed " << GetLastError() << endl;
}
else
{
WaitForSingleObject(pi.hProcess, INFINITE);
// The ProcessId returned here is NOT the one for MMC.exe (diskmgmt.msc) I think it was for cmd.exe
cout << "Process ID returned by CreateProcessA was: " << pi.dwProcessId << endl;
cout << "pi.hProcess returned was " << pi.hProcess << endl;
cout << " This is NOT the process ID for MMC.exe (diskmgmt.msc)" << endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
cout << endl << "Try #3" << endl;
cout << "Trying to start \"Disk Management\" using call to \"System(diskmgmt.msc)\"" << endl;
cout << "Start Disk Manager using \"System(diskmgmt.msc)\"" << endl;
system("diskmgmt.msc");
cout << "Disk Management was called, BUT Process ID is unknown" << endl;
// Is there a way to get the process ID from the system call??
// Prompt User to press any key
cout << "\n Press the \"ENTER\" key to Exit" << "\n";
cin.get();
}
|