I have a printer port monitor that runs fine on Windows XP. In order to avoid "session 0 isolation" on Vista, dialogs are opened from a rpcserver process running in session 1 which is instructed by the printer port monitor through a pipe, running in session 0. When the port monitor is run on Vista it fails to create the rpcserver process because openthreadtoken fails (invoked by the print spooler service) with error 1008 (ERROR_NO_TOKEN). The complete function for creating the process is attached. Why is this working on Windows XP and not Windows Vista
Olav
void
CreateServer(){
unsigned short *szStringBinding = NULL;RPC_STATUS status;
TCHAR pszProtocolSequence[] = _T(
"ncacn_np");TCHAR pszEndpoint[] = _T(
\\pipe\\rpcserver);
status = RpcStringBindingCompose(
reinterpret_cast<unsigned short*>(pszProtocolSequence),NULL,
NULL,
reinterpret_cast<unsigned short*>(pszEndpoint),NULL,
&szStringBinding);
// String binding output.if (status)
{
::MessageBox(NULL,_T("rpcstringbindingcompose feilet"),_T("debug"),MB_OK);
}
status = RpcBindingFromStringBinding(
// The string binding to validate.szStringBinding,
&hRpc1Binding);
// Put the result in the implicit binding handle defined in the IDL file.if (status)
{
::MessageBox(NULL,_T("rpcbindingfromstringbinding feilet"),_T("debug"),MB_OK);
}
HANDLE hToken = NULL;
HANDLE hTokenDup = NULL;
LPVOID pEnvironment = NULL;
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(STARTUPINFO));
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = _T("Winsta0\\Default");
// This function fails with error 1008 on Windows Vista, but succeeds on Windows XP
if (!OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE, TRUE, &hToken))
{
DWORD error = GetLastError();
TCHAR tmp[500];
wsprintf(tmp,_T("openthreadtoken failed %d"), error);
::MessageBox(NULL,tmp,_T("debug"),MB_OK);
}
if (!DuplicateTokenEx(hToken,
TOKEN_IMPERSONATE|TOKEN_READ|TOKEN_ASSIGN_PRIMARY|TOKEN_DUPLICATE,
NULL, SecurityImpersonation, TokenPrimary, &hTokenDup))
{
::MessageBox(NULL,_T("duplicatetokenex failed"),_T("debug"),MB_OK);
}
CloseHandle(hToken);
if (!CreateEnvironmentBlock(&pEnvironment, hTokenDup, FALSE)){
"createenvironment failed"),_T("debug"),MB_OK);::MessageBox(NULL,_T(
}
if (!CreateProcessAsUser(hTokenDup,
settings->GetRpcServer(),
NULL,
NULL,
NULL,
FALSE,
CREATE_NO_WINDOW,
pEnvironment,
NULL,
&si,
&pi))
{
"createprocessasuser feilet"),_T("debug"),MB_OK);::MessageBox(NULL,_T(
}
DestroyEnvironmentBlock(pEnvironment);
CloseHandle(hTokenDup);
}