I am trying to create an elevated token with SetTokenInformation
, but it fails and keeps returning error code 87.
This is my code:
#include <Windows.h>
int main()
{
HANDLE currentProcessToken, newTok;
OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, ¤tProcessToken);
DuplicateTokenEx(currentProcessToken, TOKEN_ALL_ACCESS, nullptr, SecurityImpersonation, TokenPrimary, &newTok);
CloseHandle(currentProcessToken);
TOKEN_ELEVATION elev = { 1 };
BOOL setTokenInfo = SetTokenInformation(newTok, TokenElevation, &elev, sizeof(TOKEN_ELEVATION));
DWORD error = GetLastError(); // is 87 which is "the parameter is incorrect"
return 0;
}