Click here to Skip to main content
15,915,324 members
Home / Discussions / C#
   

C#

 
QuestionMessage Removed Pin
18-Jan-10 6:19
professionalN_tro_P18-Jan-10 6:19 
AnswerRe: Application Info and Error logging Pin
Eddy Vluggen18-Jan-10 7:19
professionalEddy Vluggen18-Jan-10 7:19 
GeneralMessage Removed Pin
18-Jan-10 8:19
professionalN_tro_P18-Jan-10 8:19 
GeneralRe: Application Info and Error logging Pin
Eddy Vluggen18-Jan-10 10:33
professionalEddy Vluggen18-Jan-10 10:33 
AnswerRe: Application Info and Error logging Pin
Not Active18-Jan-10 7:26
mentorNot Active18-Jan-10 7:26 
AnswerRe: Application Info and Error logging Pin
Bardy8518-Jan-10 8:33
Bardy8518-Jan-10 8:33 
AnswerRe: Application Info and Error logging Pin
Keith Barrow18-Jan-10 23:30
professionalKeith Barrow18-Jan-10 23:30 
QuestionPhone dialer application with C# Pin
Drs201018-Jan-10 4:16
Drs201018-Jan-10 4:16 
AnswerRe: Phone dialer application with C# Pin
Covean18-Jan-10 5:36
Covean18-Jan-10 5:36 
GeneralRe: Phone dialer application with C# Pin
Drs201018-Jan-10 13:11
Drs201018-Jan-10 13:11 
GeneralRe: Phone dialer application with C# [modified] Pin
Covean18-Jan-10 21:36
Covean18-Jan-10 21:36 
GeneralRe: Phone dialer application with C# Pin
Drs201019-Jan-10 3:22
Drs201019-Jan-10 3:22 
GeneralRe: Phone dialer application with C# Pin
Covean19-Jan-10 3:54
Covean19-Jan-10 3:54 
GeneralGood Pin
ADNANE-Dev18-Jan-10 4:11
ADNANE-Dev18-Jan-10 4:11 
AnswerRe: What do you think about this article ? Pin
dan!sh 18-Jan-10 4:49
professional dan!sh 18-Jan-10 4:49 
AnswerRe: What do you think about this article ? Pin
Not Active18-Jan-10 5:03
mentorNot Active18-Jan-10 5:03 
QuestionExecute an exe under particular user account? Pin
Sunil G18-Jan-10 3:43
Sunil G18-Jan-10 3:43 
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

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.