Click here to Skip to main content
15,893,594 members
Home / Discussions / C#
   

C#

 
AnswerRe: Execute an exe under particular user account? Pin
toronja7218-Jan-10 3:57
toronja7218-Jan-10 3:57 
AnswerRe: Execute an exe under particular user account? Pin
Eddy Vluggen18-Jan-10 3:57
professionalEddy Vluggen18-Jan-10 3:57 
GeneralRe: Execute an exe under particular user account? Pin
Sunil G18-Jan-10 4:04
Sunil G18-Jan-10 4:04 
GeneralRe: Execute an exe under particular user account? Pin
Eddy Vluggen18-Jan-10 4:43
professionalEddy Vluggen18-Jan-10 4:43 
AnswerRe: Execute an exe under particular user account? Pin
Covean18-Jan-10 4:30
Covean18-Jan-10 4:30 
GeneralRe: Execute an exe under particular user account? Pin
Sunil G 318-Jan-10 6:43
Sunil G 318-Jan-10 6:43 
GeneralRe: Execute an exe under particular user account? Pin
Luc Pattyn18-Jan-10 10:07
sitebuilderLuc Pattyn18-Jan-10 10:07 
GeneralRe: Execute an exe under particular user account? Pin
Covean18-Jan-10 21:11
Covean18-Jan-10 21:11 
Yes there you are right but I send him more than 300 lines of code.
I think this is tooo long for this forum.

But here a short version (with out DLLImports, structs, ...):

public static void StartProcessOnActiveConsole(string szApplication, string szArguments, string szDirectory)
{
    IntPtr hUserToken = IntPtr.Zero;
    IntPtr hDuplicatedUserToken = IntPtr.Zero;
    IntPtr lpEnvironment = IntPtr.Zero;
    IntPtr hProcess = IntPtr.Zero;
    IntPtr hThread = IntPtr.Zero;
    try
    {
        uint dwSessionId = 0xFFFFFFFF;
        dwSessionId = WTSGetActiveConsoleSessionId();
        if (dwSessionId == 0xFFFFFFFF)  // no active session
            return;

         if(!WTSQueryUserToken(dwSessionId, ref hUserToken))
            throw new Exception("WTSQueryUserToken failed. GetLastError() = " + Marshal.GetLastWin32Error());

        if(!DuplicateTokenEx(hUserToken, 0x02000000 /*MAXIMUM_ALLOWED*/, IntPtr.Zero, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenPrimary, ref hDuplicatedUserToken))
            throw new Exception("DuplicateTokenEx failed. GetLastError() = " + Marshal.GetLastWin32Error());

        if(!CreateEnvironmentBlock(ref lpEnvironment, hDuplicatedUserToken, false))
            throw new Exception("CreateEnvironmentBlock failed. GetLastError() = " + Marshal.GetLastWin32Error());

        STARTUPINFO startupInfo = new STARTUPINFO();
        startupInfo.dwSize = (uint)Marshal.SizeOf(startupInfo);
        startupInfo.pReserved = IntPtr.Zero;
        startupInfo.szDesktop = "winsta0\\default";
        startupInfo.pTitle = IntPtr.Zero;
        startupInfo.dwX = 0;
        startupInfo.dwY = 0;
        startupInfo.dwXSize = 0;
        startupInfo.dwYSize = 0;
        startupInfo.dwXCountChars = 0;
        startupInfo.dwYCountChars = 0;
        startupInfo.dwFillAttribute = 0;
        startupInfo.dwFlags = 128 /*STARTF_FORCEOFFFEEDBACK*/;
        startupInfo.wShowWindow = 0;
        startupInfo.wReserved2 = 0;
        startupInfo.lpReserved2 = IntPtr.Zero;
        startupInfo.hStdInput = IntPtr.Zero;
        startupInfo.hStdOutput = IntPtr.Zero;
        startupInfo.hStdError = IntPtr.Zero;

        PROCESS_INFORMATION processInformation = new PROCESS_INFORMATION();
        StringBuilder szCommandLineBuilder = new StringBuilder(32768);
        szCommandLineBuilder.Append("\"" + szApplication + "\" " + (string)((szArguments == null) ? string.Empty : szArguments));
        string szDir = ((szDirectory == null) || (szDirectory == string.Empty)) ? null : szDirectory;
        if(!CreateProcessAsUser(hDuplicatedUserToken, null, szCommandLineBuilder, IntPtr.Zero, IntPtr.Zero, false, 0x00000400 /*CREATE_UNICODE_ENVIRONMENT*/, lpEnvironment, szDir, startupInfo, ref processInformation))
            throw new Exception("CreateProcessAsUser failed. GetLastError() = " + Marshal.GetLastWin32Error());
        hProcess = processInformation.hProcess;
        hThread = processInformation.hThread;
    }
    finally
    {
        // clean up
    }
}


Greetings
Covean

Questionc# hook winspool.drv Pin
toronja7218-Jan-10 3:38
toronja7218-Jan-10 3:38 
QuestionReference combobox used on another form? Pin
kanchoette18-Jan-10 1:25
kanchoette18-Jan-10 1:25 
AnswerRe: Reference combobox used on another form? Pin
Keith Barrow18-Jan-10 1:34
professionalKeith Barrow18-Jan-10 1:34 
GeneralRe: Reference combobox used on another form? Pin
kanchoette18-Jan-10 1:45
kanchoette18-Jan-10 1:45 
QuestionHow to continue without exiting... Pin
<<Tash18>>18-Jan-10 1:03
<<Tash18>>18-Jan-10 1:03 
AnswerMessage Closed Pin
18-Jan-10 1:21
stancrm18-Jan-10 1:21 
GeneralRe: How to continue without exiting... Pin
<<Tash18>>18-Jan-10 1:51
<<Tash18>>18-Jan-10 1:51 
AnswerRe: How to continue without exiting... Pin
Harvey Saayman18-Jan-10 1:24
Harvey Saayman18-Jan-10 1:24 
GeneralRe: How to continue without exiting... Pin
<<Tash18>>18-Jan-10 1:50
<<Tash18>>18-Jan-10 1:50 
GeneralRe: How to continue without exiting... Pin
Harvey Saayman18-Jan-10 2:01
Harvey Saayman18-Jan-10 2:01 
QuestionHow to simulate click button on win32 form ? Pin
Yanshof18-Jan-10 0:55
Yanshof18-Jan-10 0:55 
AnswerRe: How to simulate click button on win32 form ? Pin
#realJSOP18-Jan-10 1:00
mve#realJSOP18-Jan-10 1:00 
GeneralRe: How to simulate click button on win32 form ? Pin
Yanshof18-Jan-10 1:09
Yanshof18-Jan-10 1:09 
GeneralRe: How to simulate click button on win32 form ? Pin
Wes Aday18-Jan-10 4:15
professionalWes Aday18-Jan-10 4:15 
GeneralRe: How to simulate click button on win32 form ? Pin
#realJSOP18-Jan-10 10:24
mve#realJSOP18-Jan-10 10:24 
GeneralRe: How to simulate click button on win32 form ? Pin
Yanshof18-Jan-10 17:20
Yanshof18-Jan-10 17:20 
GeneralRe: How to simulate click button on win32 form ? Pin
#realJSOP18-Jan-10 23:26
mve#realJSOP18-Jan-10 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.