Click here to Skip to main content
15,919,358 members
Home / Discussions / C#
   

C#

 
AnswerRe: Dot matrix printer on crystal report in c# Pin
Shameel19-Jan-10 7:56
professionalShameel19-Jan-10 7:56 
GeneralRe: Dot matrix printer on crystal report in c# Pin
koncuk20-Jan-10 1:33
koncuk20-Jan-10 1:33 
AnswerRe: Dot matrix printer on crystal report in c# Pin
Murugesan Solaiyappan12-May-10 7:02
Murugesan Solaiyappan12-May-10 7:02 
QuestionMessage Removed Pin
18-Jan-10 20:05
user20518-Jan-10 20:05 
AnswerRe: Looking for C#.NET, XAML, WPF expert having wireless domain Pin
dan!sh 18-Jan-10 20:29
professional dan!sh 18-Jan-10 20:29 
QuestionJIT debugger Error. Pin
Sunil G18-Jan-10 19:55
Sunil G18-Jan-10 19:55 
AnswerRe: JIT debugger Error. Pin
OriginalGriff18-Jan-10 22:45
mveOriginalGriff18-Jan-10 22:45 
GeneralRe: JIT debugger Error. Pin
Sunil G18-Jan-10 23:20
Sunil G18-Jan-10 23:20 
Here is the code I am using to execute an executable file.
<code>
namespace ExecuteAnExeUsingCurrentLoggedOnUser
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct STARTUPINFO
        {
            public Int32 cb;
            public string lpReserved;
            public string lpDesktop;
            public string lpTitle;
            public Int32 dwX;
            public Int32 dwY;
            public Int32 dwXSize;
            public Int32 dwXCountChars;
            public Int32 dwYCountChars;
            public Int32 dwFillAttribute;
            public Int32 dwFlags;
            public Int16 wShowWindow;
            public Int16 cbReserved2;
            public IntPtr lpReserved2;
            public IntPtr hStdInput;
            public IntPtr hStdOutput;
            public IntPtr hStdError;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct PROCESS_INFORMATION
        {
            public IntPtr hProcess;
            public IntPtr hThread;
            public Int32 dwProcessID;
            public Int32 dwThreadID;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct SECURITY_ATTRIBUTES
        {
            public Int32 Length;
            public IntPtr lpSecurityDescriptor;
            public bool bInheritHandle;
        }

        public enum SECURITY_IMPERSONATION_LEVEL
        {
            SecurityAnonymous,
            SecurityIdentification,
            SecurityImpersonation,
            SecurityDelegation
        }

        public enum TOKEN_TYPE
        {
            TokenPrimary = 1,
            TokenImpersonation
        }

        public const int GENERIC_ALL_ACCESS = 0x10000000;
        public const int TOKEN_DUPLICATE = 2;
        public const int TOKEN_QUERY = 0X00000008;
        public const int TOKEN_IMPERSONATE = 0X00000004;

        [
           System.Runtime.InteropServices.DllImport("kernel32.dll",
              EntryPoint = "CloseHandle", SetLastError = true,
              CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)
        ]
        public static extern bool CloseHandle(IntPtr handle);

        [
           DllImport("advapi32.dll",
              EntryPoint = "CreateProcessAsUser", SetLastError = true,
              CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)
        ]
        public static extern bool
           CreateProcessAsUser(IntPtr hToken, string lpApplicationName, string lpCommandLine,
                               ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes,
                               bool bInheritHandle, Int32 dwCreationFlags, IntPtr lpEnvrionment,
                               string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo,
                               ref PROCESS_INFORMATION lpProcessInformation);

        [
           DllImport("advapi32.dll",
              EntryPoint = "DuplicateTokenEx")
        ]
        public static extern bool
           DuplicateTokenEx(IntPtr hExistingToken, Int32 dwDesiredAccess,
                            ref SECURITY_ATTRIBUTES lpThreadAttributes,
                            Int32 ImpersonationLevel, Int32 dwTokenType,
                            ref IntPtr phNewToken);

        [
            DllImport("advapi32", SetLastError = true),
                SuppressUnmanagedCodeSecurityAttribute
        ]
        public static extern int
            OpenProcessToken(
                System.IntPtr ProcessHandle, // handle to process
                int DesiredAccess, // desired access to process
                ref IntPtr TokenHandle // handle to open access token
                );
        [DllImport("advapi32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool OpenProcessToken(IntPtr ProcessHandle,
            UInt32 DesiredAccess, out IntPtr TokenHandle);
        static void Main(string[] args)
        {
            CreateProcessAsUser1("c:\\ServiceManager.exe", "");
        }
        private static void CreateProcessAsUser1(string strApplication, string strArg)
        {

                IntPtr hToken = IntPtr.Zero;
                Process[] proc = Process.GetProcessesByName("explorer");
                if (OpenProcessToken(proc[0].Handle,
                        TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE,
                        ref hToken) != 0)
                {
                }

                IntPtr hDupedToken = IntPtr.Zero;

                PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

                try
                {
                    SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
                    sa.Length = Marshal.SizeOf(sa);

                    bool result = DuplicateTokenEx(
                          hToken,
                          GENERIC_ALL_ACCESS,
                          ref sa,
                          (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
                          (int)TOKEN_TYPE.TokenPrimary,
                          ref hDupedToken
                       );

                    if (!result)
                    {
                        throw new ApplicationException("DuplicateTokenEx failed");
                    }


                    STARTUPINFO si = new STARTUPINFO();
                    si.cb = Marshal.SizeOf(si);
                    si.lpDesktop = String.Empty;

                    result = CreateProcessAsUser(
                                         hDupedToken,
                                         strApplication, strArg,
                                         ref sa, ref sa,
                                         false, 0, IntPtr.Zero,
                                         @"C:\", ref si, ref pi
                                   );

                    if (!result)
                    {
                        int error = Marshal.GetLastWin32Error();
                        string message = String.Format("CreateProcessAsUser Error: {0}", error);
                        throw new ApplicationException(message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Inner Catch " + ex.Message.ToString());
                }
            


            finally
            {
                if (pi.hProcess != IntPtr.Zero)
                    CloseHandle(pi.hProcess);
                if (pi.hThread != IntPtr.Zero)
                    CloseHandle(pi.hThread);
                if (hDupedToken != IntPtr.Zero)
                    CloseHandle(hDupedToken);
            }
        }

    }
}</code>

GeneralRe: JIT debugger Error. Pin
OriginalGriff18-Jan-10 23:42
mveOriginalGriff18-Jan-10 23:42 
GeneralRe: JIT debugger Error. [modified] Pin
Sunil G18-Jan-10 23:50
Sunil G18-Jan-10 23:50 
GeneralRe: JIT debugger Error. Pin
OriginalGriff19-Jan-10 1:07
mveOriginalGriff19-Jan-10 1:07 
GeneralRe: JIT debugger Error. Pin
Sunil G19-Jan-10 1:11
Sunil G19-Jan-10 1:11 
GeneralRe: JIT debugger Error. Pin
OriginalGriff19-Jan-10 1:16
mveOriginalGriff19-Jan-10 1:16 
GeneralRe: JIT debugger Error. Pin
Luc Pattyn19-Jan-10 1:14
sitebuilderLuc Pattyn19-Jan-10 1:14 
GeneralRe: JIT debugger Error. Pin
OriginalGriff19-Jan-10 1:19
mveOriginalGriff19-Jan-10 1:19 
GeneralRe: JIT debugger Error. Pin
Sunil G19-Jan-10 1:25
Sunil G19-Jan-10 1:25 
QuestionContextMenuStrip dynamic item widths Pin
DarrenShultz18-Jan-10 18:42
DarrenShultz18-Jan-10 18:42 
AnswerRe: ContextMenuStrip dynamic item widths Pin
April Fans19-Jan-10 17:52
April Fans19-Jan-10 17:52 
GeneralRe: ContextMenuStrip dynamic item widths Pin
DarrenShultz19-Jan-10 19:08
DarrenShultz19-Jan-10 19:08 
Questionexport DataGridView to excel 2007 in c# Pin
H.R18-Jan-10 18:21
H.R18-Jan-10 18:21 
AnswerRe: export DataGridView to excel 2007 in c# Pin
dan!sh 18-Jan-10 19:17
professional dan!sh 18-Jan-10 19:17 
QuestionKeep from erasing working code in a project Pin
tonyonlinux18-Jan-10 18:11
tonyonlinux18-Jan-10 18:11 
AnswerMessage Closed Pin
18-Jan-10 18:46
stancrm18-Jan-10 18:46 
GeneralRe: Keep from erasing working code in a project Pin
tonyonlinux18-Jan-10 20:29
tonyonlinux18-Jan-10 20:29 
GeneralRe: Keep from erasing working code in a project Pin
Bekjong18-Jan-10 22:02
Bekjong18-Jan-10 22:02 

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.