raindrop7 raindrop7 - 2 months ago 24
C++ Question

how to create task in c++?

I want to create a c++ program that can relaunch itself or other program with high privilege bypassing UAC dialog.

Answer

If you want to find notepad.exe in the Windows system directory, I'd advise just doing that a little more directly:

#include <string.h>
#include <windows.h>

int main() { 
    char path[_MAX_PATH];

    GetSystemDirectory(path, sizeof(path));

    strcat_s(path, "/notepad.exe");

    WinExec(path, SW_SHOWNORMAL);
}

To execute the result under a specified user account, you normally want to use CreateProcessWithLogonW (there are a few alternatives such as CreateProcessAsUser and CreateProcessWithTokenW, but CreateProcessWithLogonW is usually preferred).

This uses wide characters, so it's probably easiest to use wide characters throughout, something like this:

#include <string.h>
#include <windows.h>

template <size_t N>
DWORD GetSystemDirectory(wchar_t (&buffer)[N]) {
    return GetSystemDirectoryW(buffer, N);
}

template <size_t N>
void append(wchar_t (&buffer)[N], wchar_t const *src) {
    wcscat_s(buffer, N, src);
}

int main() { 
    wchar_t path[_MAX_PATH];

    GetSystemDirectory(path);

    append(path, L"/Notepad.exe");

    PROCESS_INFORMATION pi;
    CreateProcessWithLogonW(L"Username", L"machine_name", L"password", 0, path, NULL, CREATE_NEW_CONSOLE, NULL, NULL, NULL, &pi);
}
Comments