C#获得父进程PID编号的完整源源码
using System;
using System.Diagnostics;namespace RobvanderWoude
{class GetMyPID{static int Main( string[] args ){#region Command Line Parsingbool doCheck = false;bool verbose = false;Int64 check = -1;foreach ( string arg in args ){switch ( arg.ToLower( ) ){case "/v":case "/verbose":case "-v":case "--verbose":if ( verbose ){return WriteError( "Duplicate command line switch /Verbose" );}verbose = true;break;case "?":case "/?":case "-?":case "/h":case "-h":case "/help":case "--help":return WriteError( string.Empty );default:if ( doCheck ){return WriteError( "Invalid or duplicate command line argument " + arg.ToUpper( ) );}if ( !Int64.TryParse( arg, out check ) ){return WriteError( "Invalid command line argument " + arg.ToUpper( ) );}if ( check < 1 ){return WriteError( "Invalid process ID " + arg );}doCheck = true;break;}}if ( args.Length > 2 ){return WriteError( "Invalid command line argument(s)" );}#endregion Command Line Parsingtry{int pid = ParentProcess.ParentProcess.ProcessId;if ( verbose ){Console.WriteLine( "Parent name : {0}", ParentProcess.ParentProcess.ProcessName );Console.WriteLine( "Parent path : {0}", ParentProcess.ParentProcess.FullPath );Console.WriteLine( "Parent PID : {0}", pid );if ( doCheck ){Console.WriteLine( "Expected PID : {0}", check );Console.WriteLine( "Match : {0}", ( pid == check ? "Yes" : "No" ) );}}else{Console.WriteLine( pid.ToString( ) );}if ( doCheck ){return ( pid == check ? 0 : 2 );}else{return pid;}}catch ( Exception e ){return WriteError( e );}}#region Error Handlingpublic static int WriteError( Exception e ){return WriteError( e == null ? null : e.Message );}public static int WriteError( string errorMessage ){string exeName = Process.GetCurrentProcess( ).ProcessName;GetMyPID, Version 1.00Return or check the parent's (caller's) Process IDUsage: GETMYPID [ pid ] [ /V ]Where: pid is the expected PID to check the actual PID against/V display verbose information on the parent processNotes: With an expected PID specified, this program returns 'errorlevel'0 if the actual PID matches the expected value, or 2 if not;otherwise, it returns an 'errorlevel' equal to the PID.So to get a batch file's PID, use the following commands:GETMYPID.EXESET PID=%ErrorLevel%Credits: ParentProcess class by Jared Barneck www.rhyous.com/2010/04/30/how-to-get-the-parent-process-that-launched-a-c-application/Written by Rob van der Woudeif ( string.IsNullOrEmpty( errorMessage ) == false ){Console.Error.WriteLine( );Console.ForegroundColor = ConsoleColor.Red;Console.Error.Write( "ERROR: " );Console.ForegroundColor = ConsoleColor.White;Console.Error.WriteLine( errorMessage );Console.ResetColor( );}Console.Error.WriteLine( );Console.Error.WriteLine( "GetMyPID, Version 1.00" );Console.Error.WriteLine( "Return or check the parent's (caller's) Process ID" );Console.Error.WriteLine( );Console.Error.Write( "Usage: " );Console.ForegroundColor = ConsoleColor.White;Console.Error.WriteLine( "{0} [ pid ] [ /v ]", exeName.ToUpper( ) );Console.ResetColor( );Console.Error.WriteLine( );Console.Error.Write( "Where: " );Console.ForegroundColor = ConsoleColor.White;Console.Error.Write( "pid" );Console.ResetColor( );Console.Error.WriteLine( " is the expected PID to check the actual PID against" );Console.ForegroundColor = ConsoleColor.White;Console.Error.Write( " /V" );Console.ResetColor( );Console.Error.WriteLine( " display verbose information on the parent process" );Console.Error.WriteLine( );Console.Error.WriteLine( "Notes: With an expected PID specified, this program returns 'errorlevel'" );Console.Error.WriteLine( " 0 if the actual PID matches the expected value, or 2 if not;" );Console.Error.WriteLine( " otherwise, it returns an 'errorlevel' equal to the PID." );Console.Error.WriteLine( " So to get a batch file's PID, use the following commands:" );Console.Error.WriteLine( );Console.ForegroundColor = ConsoleColor.White;Console.Error.WriteLine( " GETMYPID.EXE" );Console.Error.WriteLine( " SET PID=%ErrorLevel%" );Console.ResetColor( );Console.Error.WriteLine( );Console.Error.Write( "Credits: ParentProcess class by Jared Barneck " );Console.ForegroundColor = ConsoleColor.DarkGray;Console.Error.WriteLine( "www.rhyous.com/2010/04/30" );Console.Error.WriteLine( " /how-to-get-the-parent-process-that-launched-a-c-application/" );Console.ResetColor( );Console.Error.WriteLine( );Console.Error.WriteLine( "Written by Rob van der Woude" );return 1;}#endregion Error Handling}
} 复制代码
C#获得父进程PID编号的完整源源码相关推荐
- VC获取父进程PID
// tt.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include &l ...
- linux进程号是几位数,Linux 进程 PID 编号最大为多少
问题中提到,通过提问者的测试(Ubuntu18.04操作系统下),Python脚本实现的分配进行pid脚本,渠道系统自动分配给进程的最大pid值是131070,这是一个并不特殊的值.(通常可以想到的上 ...
- C语言实现pid算法(附完整源码)
实现pid算法 pid结构体定义 C语言实现pid算法完整源码(定义,实现,main函数测试) pid结构体定义 struct pid {// Controller gainsfloat kP;flo ...
- windows 根据父进程pid查找所有子进程id(C++)
直接上代码: 大家直接调用即可 #include <iostream> #include <Windows.h> #include <tlhelp32.h> #in ...
- linux c 获取指定进程 父进程pid
示例如下: int GetFatherPid(int pid) {char dir[1024]={0};char path[1024] = {0};char buf[1024] = {0};int r ...
- python获取进程编号(目的、获取当前进程编号、根据编号杀死指定进程号、获取当前父进程编号)
1. 获取进程编号的目的 获取进程编号的目的是验证主进程和子进程的关系,可以得知子进程是由那个主进程创建出来的. 获取进程编号的两种操作 获取当前进程编号 获取当前父进程编号 2. 获 ...
- 进程控制 父进程子进程 fork pid
进程:正在运行的程序(分配资源:内存,CPU,IO等) 程序:有限指令的集合(静态:没有执行,存放在外存) 并发:多个任务"同时"执行. (处理器某一时刻最多只运行一个进程) 1. ...
- 父进程产生一系列子进程,每个子进程打印自己的PID然后退出,父进程最后打印PID
#include<stdio.h> #include<stdlib.h> #include <unistd.h> //父进程产生一系列子进程,每个子进程打印自己的P ...
- 问题:子进程父进程哪个先执行:【转】关于 fork 和父子进程的理解
2019独角兽企业重金招聘Python工程师标准>>> 关于 fork 和父子进程的理解 (http://blog.163.com/kingore@126/blog/static ...
最新文章
- APICloud:让你的APP边框显示原生的1px
- Win32 汇编子过程总结
- 你最喜欢的一张美女图片?
- 单核工作法17:简化协作(上)
- javase期末考试题_java期末考试试题及答案
- PowerDesigner新建模板
- 微软的创新还是败笔?Windows 8为苹果创造天赐良机
- linux go missing git command,安装beego出现go: missing Git command
- VMmware安装VMware Tools问题解决
- C++ 一定要使用strcpy_s()函数 等来操作方法c_str()返回的指针
- linux 添加硬盘
- 基于Oracle的SQL优化(社区万众期待 数据库优化扛鼎巨著)
- shell初学之PHP
- 计算机键盘不能用怎么办,电脑键盘空格键失灵无法使用怎么办|电脑键盘空格键失灵的解决方法...
- JNDI(Java Naming and Directory Interface)介绍
- 地铁发展黄金期 安防应用受考验
- 每日一问。2015.1.8
- 在Ubuntu中使用WinUSB创建Windows启动盘
- C语言基础(整形变量,实型数据)
- XStream的简单使用