C# 에서 process invoke(fork)

c#에서 다른 프로세서를 호출하고 리턴값을 받기위한 소스이다.

==일단 c++=====================================

#include "stdafx.h"
 
#include<iostream>
 
int _tmain(int argc, _TCHAR* argv[])
{
 
 return -12;
}

=====================================

== c#에서는 =================================


using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
 
namespace ConsoleCreateProcess
{
 
    public struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public uint dwProcessId;
        public uint dwThreadId;
    }
 
    public struct STARTUPINFO
    {
        public uint cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public uint dwX;
        public uint dwY;
        public uint dwXSize;
        public uint dwYSize;
        public uint dwXCountChars;
        public uint dwYCountChars;
        public uint dwFillAttribute;
        public uint dwFlags;
        public short wShowWindow;
        public short cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }
 
    public struct SECURITY_ATTRIBUTES
    {
        public int length;
        public IntPtr lpSecurityDescriptor;
        public bool bInheritHandle;
    }
 
    public struct PROCESS_QUERY_INFORMATION
    {
 
    }
 
    public class Program
    {
        public static void Main()
        {
            int exitcode =0;
 
            STARTUPINFO si = new STARTUPINFO();
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
            CreateProcess(@"G:\MSVS-projects\연습\Release\\ConsoleReturnInt.exe"nullIntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, nullref si, out pi);
            Console.WriteLine(pi.hProcess);
            WaitForSingleObject(pi.hProcess, 100000);
 
            GetExitCodeProcess(pi.hProcess,ref exitcode);
 
            Console.Write("exitcode : " + exitcode);
            Console.ReadLine();
        }
 
        
        [DllImport("kernel32.dll")]
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes,
                                bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment,
                                string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
 
        [DllImport("kernel32.dll")]
        static extern bool GetExitCodeProcess(IntPtr hProcess,ref int lpExitCode );
 
        [DllImport("kernel32.dll")]
        static extern bool WaitForSingleObject(IntPtr hProcess, int waitforsecond);
    }
}

=============================================

인터넷 찾아보고 몇군데 참고했다.

댓글

이 블로그의 인기 게시물

C#에서 포인터 사용

WPF RichTextBox 와 Document의 바인딩

WPF 이미지위에 라인 그리기(WPF DrawLine on exist Image)