I am having issues with my c++ code. When I run it via Visual Studio, it works properly and no issues or errors are being logged. When I try to run a CMD as administrator and run the program, I get an error that’s being logged in my code.
This is what I’ve tried:
int FindSysmainSvc()
{
SC_HANDLE scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);
if (!scm) {
std::cerr << "Failed to open service control manager: " << GetLastError() << std::endl;
return -1;
}
SC_HANDLE service = OpenService(scm, L"SysMain", SERVICE_QUERY_STATUS);
if (!service) {
std::cerr << "Failed to open service: " << GetLastError() << std::endl;
CloseServiceHandle(scm);
return -1;
}
SERVICE_STATUS_PROCESS status;
DWORD bytesNeeded;
if (!QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO, reinterpret_cast<LPBYTE>(&status), sizeof(status), &bytesNeeded)) {
std::cerr << "Failed to query service status: " << GetLastError() << std::endl;
CloseServiceHandle(service);
CloseServiceHandle(scm);
return -1;
}
DWORD processId = status.dwProcessId;
CloseServiceHandle(service);
CloseServiceHandle(scm);
return processId;
}
int main() {
int pid = FindSysmainSvc();
if (pid == -1) {
std::cout << "[!] Failed to find pidn";
return -1;
}
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
if (hProcess == nullptr) {
std::cout << "[!] OpenProcess(): FAILED";
return -1;
}
CloseHandle(hProcess);
return 0;
}
The pid is returned successfully on both visual studio execution and CMD one, but the process handle only gets retrieved on visual studio’s end, where in CMD it returns as null.