Click here to Skip to main content
16,002,185 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
i took one submit button and textbox in that i assigned the text is "java -jar sample.jar"(this is small jar file output: Hello),

when calling this jar file in windows c# program, result of the file(Hello) directly(without executing the file) displayed in command prompt. this is done by clicking submit button.

I save jar file in E: drive.
Posted
Comments
nishantcomp2512 10-Nov-11 7:05am    
so what you want to do????
Member 8288629 10-Nov-11 7:10am    
your solution program is not executed,
Unsafe code may only appear if compiling with /unsa
Rajesh Anuhya 10-Nov-11 7:12am    
Are you implemented/tested with System. Diagnostics.Process ???
Rajesh Anuhya 10-Nov-11 7:11am    
How many times you will post this Question
Smithers-Jones 10-Nov-11 7:33am    
Repost. Reported.

1 solution

The following is the C# program to execute a program and get the return code. (This example launches NOTEPAD in "C:\Windows" directory.)
I believe you can convert the program into your desired one.



C#
using System;
using System.Text;
using System.Runtime.InteropServices;

class Program
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public UInt32 dwProcessId;
        public UInt32 dwThreadId;
    }

    const UInt32 WAIT_OBJECT_0 = 0x00000000;
    const UInt32 INFINITE = 0xFFFFFFFF;

    [StructLayout(LayoutKind.Sequential)]
    internal struct SECURITY_ATTRIBUTES
    {
        public int nLength;
        public IntPtr lpSecurityDescriptor;
        public bool bInheritHandle;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct STARTUPINFO
    {
        public int cb;
        public IntPtr lpReserved;
        public IntPtr lpDesktop;
        public IntPtr lpTitle;
        public int dwX;
        public int dwY;
        public int dwXSize;
        public int dwYSize;
        public int dwXCountChars;
        public int dwYCountChars;
        public int dwFillAttribute;
        public int dwFlags;
        public short wShowWindow;
        public short cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [DllImport("kernel32.dll", EntryPoint = "CreateProcess", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool CreateProcess(string appName, StringBuilder cmdLine, IntPtr processAttr, IntPtr threadAttr, bool inheritHandles, int creationFlag, IntPtr environment, IntPtr curDir, ref STARTUPINFO startupInfo, ref PROCESS_INFORMATION processInfo);

    [DllImport("kernel32.dll", EntryPoint = "WaitForSingleObject", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 timeOut);

    [DllImport("kernel32.dll", EntryPoint = "GetExitCodeProcess", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
    static extern bool GetExitCodeProcess(IntPtr hHandle, out Int32 timeOut);

    [DllImport("kernel32.dll", EntryPoint = "CloseHandle", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
    static extern bool CloseHandle(IntPtr hHandle);

    unsafe static void Main(string[] args)
    {
        PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
        STARTUPINFO si = new STARTUPINFO();
        si.cb = Marshal.SizeOf(si);
        if (CreateProcess(@"C:\WINDOWS\notepad.exe", null /* parameter */, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, IntPtr.Zero, ref si, ref pi))
        {
            uint result = WaitForSingleObject(pi.hProcess, INFINITE);
            if (result == WAIT_OBJECT_0)
            {
                Int32 returnCode;
                if (GetExitCodeProcess(pi.hProcess, out returnCode))
                    Console.Out.WriteLine(returnCode);
            }
            else
            {
                // error handler
            }

            CloseHandle(pi.hProcess);
            CloseHandle(pi.hThread);
        }
    }
}



click Accept Answer button if solves your problem.. it motivates :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900