U
user1976
Hi all, I have the following function that I execute from a service
running on
win2003 terminal server. This code creates a process in a client's
terminal session. I got this to work on win 2000 server. But when I
run in 2003 I always
keep getting ERROR_INVALID_PARAMETER from the function
CreateProcessAsUserW .
I verified the code and every thing seems right. I am trying to create
some thing as simple as c:\w2k3\notepad.exe and it still gives this
error. Any body with ideas please help me out
int createTSProcess(char* userName,char* domain,char* passwd,char*
commandStr,char* processDir,long sessionId){
HANDLE hToken;
PROCESS_INFORMATION pi;
STARTUPINFOW si;
LPVOID lpEnvironment = NULL;
// initialize STARTUPINFO structure
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
__try {
// obtain an access token for the given user
if (!LogonUser(userName,domain,passwd,LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,&hToken))
{
showError("LogonUser:");
return FAILURE_AUTH_FAILURE;
}
if(EnableSecurityRights(SE_TCB_NAME, TRUE) != ERROR_SUCCESS) {
showError("EnableSecurityRights:");
return FAILURE_SETTOKEN_FAILURE;
}
//cout<<"Successfully enabled priveleges\n";
DWORD dwSessionId = sessionId;
if(!SetTokenInformation(hToken, TokenSessionId, &dwSessionId,
sizeof(DWORD))) {
showError("SetTokenInformation:");
return FAILURE_SETTOKEN_FAILURE;
}
//cout<<"Successfully set token\n";
// Unicode usage due to CreateProcessAsUserA bug (Q257193)
wchar_t wszCommand[4096];
if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, commandStr, -1,
wszCommand, 4096)) {
showError("MultiByteToWideChar:");
return FAILURE_CREATEPROCESS_FAILURE;
} else {
if( !CreateProcessAsUserW(
hToken, // client's access token
NULL, // file to execute
wszCommand, // command line
NULL, // pointer to process SECURITY_ATTRIBUTES
NULL, // pointer to thread SECURITY_ATTRIBUTES
TRUE, // handles are not inheritable
NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT, // creation
flags
NULL, // pointer to new environment block
NULL, // name of current directory
&si, // pointer to STARTUPINFO structure
&pi) // receives information about new process
) {
showError("CreateProcessAsUserW:");
cout<<"Create process failed\n";
return FAILURE_CREATEPROCESS_FAILURE;
}
}
}
__finally {
// Close the opened handles.
if (hToken) CloseHandle(hToken);
if (pi.hProcess) CloseHandle(pi.hProcess);
if (pi.hThread) CloseHandle(pi.hThread);
if (lpEnvironment != NULL) DestroyEnvironmentBlock(lpEnvironment);
}
return pi.dwProcessId;
}
running on
win2003 terminal server. This code creates a process in a client's
terminal session. I got this to work on win 2000 server. But when I
run in 2003 I always
keep getting ERROR_INVALID_PARAMETER from the function
CreateProcessAsUserW .
I verified the code and every thing seems right. I am trying to create
some thing as simple as c:\w2k3\notepad.exe and it still gives this
error. Any body with ideas please help me out
int createTSProcess(char* userName,char* domain,char* passwd,char*
commandStr,char* processDir,long sessionId){
HANDLE hToken;
PROCESS_INFORMATION pi;
STARTUPINFOW si;
LPVOID lpEnvironment = NULL;
// initialize STARTUPINFO structure
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
__try {
// obtain an access token for the given user
if (!LogonUser(userName,domain,passwd,LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,&hToken))
{
showError("LogonUser:");
return FAILURE_AUTH_FAILURE;
}
if(EnableSecurityRights(SE_TCB_NAME, TRUE) != ERROR_SUCCESS) {
showError("EnableSecurityRights:");
return FAILURE_SETTOKEN_FAILURE;
}
//cout<<"Successfully enabled priveleges\n";
DWORD dwSessionId = sessionId;
if(!SetTokenInformation(hToken, TokenSessionId, &dwSessionId,
sizeof(DWORD))) {
showError("SetTokenInformation:");
return FAILURE_SETTOKEN_FAILURE;
}
//cout<<"Successfully set token\n";
// Unicode usage due to CreateProcessAsUserA bug (Q257193)
wchar_t wszCommand[4096];
if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, commandStr, -1,
wszCommand, 4096)) {
showError("MultiByteToWideChar:");
return FAILURE_CREATEPROCESS_FAILURE;
} else {
if( !CreateProcessAsUserW(
hToken, // client's access token
NULL, // file to execute
wszCommand, // command line
NULL, // pointer to process SECURITY_ATTRIBUTES
NULL, // pointer to thread SECURITY_ATTRIBUTES
TRUE, // handles are not inheritable
NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT, // creation
flags
NULL, // pointer to new environment block
NULL, // name of current directory
&si, // pointer to STARTUPINFO structure
&pi) // receives information about new process
) {
showError("CreateProcessAsUserW:");
cout<<"Create process failed\n";
return FAILURE_CREATEPROCESS_FAILURE;
}
}
}
__finally {
// Close the opened handles.
if (hToken) CloseHandle(hToken);
if (pi.hProcess) CloseHandle(pi.hProcess);
if (pi.hThread) CloseHandle(pi.hThread);
if (lpEnvironment != NULL) DestroyEnvironmentBlock(lpEnvironment);
}
return pi.dwProcessId;
}