나는이 기사를 시도했다. Code Project, 그것은 나를 위해 잘 작동하고있다. 나는 코드도 사용했다. 기사 스크린 샷과 함께 설명에 우수합니다.
나는 당신이 당신의 컴퓨터를 부팅하고 로그온하려고 한이 시나리오
에 필요한 설명을 추가하고있다. 로그온하면 시스템이 고유 한 세션 ID를 할당합니다. Windows Vista에서는 컴퓨터에 처음 로그온하는 사용자에게 OS에 의해 세션 ID 1이 할당됩니다. 로그온 할 다음 사용자에게 세션 ID 2가 할당됩니다. 작업 관리자의 사용자 탭에서 로그온 한 각 사용자에게 할당 된 세션 ID를 볼 수 있습니다.
그러나 Windows 서비스는 세션 ID가 0입니다.이 세션은 다른 세션과 분리되어 있습니다.궁극적으로 Windows 서비스가 사용자 세션에서 실행되는 응용 프로그램을 1 또는 2와 같이 호출하는 것을 방지합니다.
Windows 서비스에서 응용 프로그램을 호출하려면 표시된대로 현재 로그인 한 사용자 역할을하는 컨트롤을 Windows 서비스에서 복사해야합니다 아래 스크린 샷.
중요 코드
// obtain the process id of the winlogon process that
// is running within the currently active session
Process[] processes = Process.GetProcessesByName("winlogon");
foreach (Process p in processes)
{
if ((uint)p.SessionId == dwSessionId)
{
winlogonPid = (uint)p.Id;
}
}
// obtain a handle to the winlogon process
hProcess = OpenProcess(MAXIMUM_ALLOWED, false, winlogonPid);
// obtain a handle to the access token of the winlogon process
if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, ref hPToken))
{
CloseHandle(hProcess);
return false;
}
// Security attibute structure used in DuplicateTokenEx and CreateProcessAsUser
// I would prefer to not have to use a security attribute variable and to just
// simply pass null and inherit (by default) the security attributes
// of the existing token. However, in C# structures are value types and therefore
// cannot be assigned the null value.
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.Length = Marshal.SizeOf(sa);
// copy the access token of the winlogon process;
// the newly created token will be a primary token
if (!DuplicateTokenEx(hPToken, MAXIMUM_ALLOWED, ref sa,
(int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
(int)TOKEN_TYPE.TokenPrimary, ref hUserTokenDup))
{
CloseHandle(hProcess);
CloseHandle(hPToken);
return false;
}
STARTUPINFO si = new STARTUPINFO();
si.cb = (int)Marshal.SizeOf(si);
// interactive window station parameter; basically this indicates
// that the process created can display a GUI on the desktop
si.lpDesktop = @"winsta0\default";
// flags that specify the priority and creation method of the process
int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
// create a new process in the current User's logon session
bool result = CreateProcessAsUser(hUserTokenDup, // client's access token
null, // file to execute
applicationName, // command line
ref sa, // pointer to process SECURITY_ATTRIBUTES
ref sa, // pointer to thread SECURITY_ATTRIBUTES
false, // handles are not inheritable
dwCreationFlags, // creation flags
IntPtr.Zero, // pointer to new environment block
null, // name of current directory
ref si, // pointer to STARTUPINFO structure
out procInfo // receives information about new process
);
지금까지 내가 말할 수있는, 당신은 예외를 얻고있다 작성된 코드와 아무 문제가 없다? –
이 프로그램을 실행할 때 어떤 일이 발생했는지 알려주십시오. – NT88