How to find out how my process started

We have a LOB winforms application, which under normal circumstances should be launched from the launch, which should check the basic version and load any updated components before laying out the main process.

One of the problems that we are observing is that some employees have found that it loads faster without starting the update application, however this may lead to people not having the latest features and supporting all types of headaches. .

What I would like to do is to throw a warning if they are not logged in through the initialized application. Ideally, I would like to do this without changing the application for the update (since this means that you are going to install and install a new MSI on each client), and the approach that arises is to find some way to find information about the process who started β€œme” and check on the white / black list, forever I can’t find a way to do this?


Also: of course, if I resorted to modifying the application for updating, I would probably change it to either pass the previously shared secret as a command line argument, or better yet, change the application so that I can just download it as class library and create the appropriate class through reflection. ClickOnce is excluded because it does not support the installation of multiple users

+5
source share
4 answers

See here: How to get the parent process in .NET in managed mode

From the link:

using System.Diagnostics;
PerformanceCounter pc = new PerformanceCounter("Process",
"Creating Process ID", Process.GetCurrentProcess().ProcessName);
return Process.GetProcessById((int)pc.NextValue());

[Edit: See also the System.Diagnostics FAQ for more information about this. Thanks to Justin for the link.]

+4

, , , , , ( - db/ftp, ), . , .

+3

PInvoke Kernel32, , . . , :

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
static class myProcessEx
{  
    //inner enum used only internally
    [Flags]
    private enum SnapshotFlags : uint
    {
    HeapList = 0x00000001,
    Process = 0x00000002,
    Thread = 0x00000004,
    Module = 0x00000008,
    Module32 = 0x00000010,
    Inherit = 0x80000000,
    All = 0x0000001F
    }
    //inner struct used only internally
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct PROCESSENTRY32
    {
    const int MAX_PATH = 260;
    internal UInt32 dwSize;
    internal UInt32 cntUsage;
    internal UInt32 th32ProcessID;
    internal IntPtr th32DefaultHeapID;
    internal UInt32 th32ModuleID;
    internal UInt32 cntThreads;
    internal UInt32 th32ParentProcessID;
    internal Int32 pcPriClassBase;
    internal UInt32 dwFlags;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
    internal string szExeFile;
    }

    [DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern IntPtr CreateToolhelp32Snapshot([In]UInt32 dwFlags, [In]UInt32 th32ProcessID);

    [DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern bool Process32First([In]IntPtr hSnapshot, ref PROCESSENTRY32 lppe);

    [DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern bool Process32Next([In]IntPtr hSnapshot, ref PROCESSENTRY32 lppe);

    // get the parent process given a pid
    public static Process GetParentProcess(int pid)
    {

    Process parentProc = null;
    try
    {
        PROCESSENTRY32 procEntry = new PROCESSENTRY32();
        procEntry.dwSize = (UInt32)Marshal.SizeOf(typeof(PROCESSENTRY32));
        IntPtr handleToSnapshot = CreateToolhelp32Snapshot((uint)SnapshotFlags.Process, 0);
        if (Process32First(handleToSnapshot, ref procEntry))
        {
        do
        {
            if (pid == procEntry.th32ProcessID)
            {
            parentProc = Process.GetProcessById((int)procEntry.th32ParentProcessID);
            break;

            }
        } while (Process32Next(handleToSnapshot, ref procEntry));
        }
        else
        {
        throw new ApplicationException(string.Format("Failed with win32 error code {0}", Marshal.GetLastWin32Error()));
        }
    }
    catch (Exception ex)
    {
        throw new ApplicationException("Can't get the process.", ex);
    }
    return parentProc;
    }

    // get the specific parent process
    public static Process CurrentParentProcess
    {
    get
    {
        return GetParentProcess(Process.GetCurrentProcess().Id);
    }
    }

    static void Main()
    {
    Process pr = CurrentParentProcess;

    Console.WriteLine("Parent Proc. ID: {0}, Parent Proc. name: {1}", pr.Id, pr.ProcessName);
    }
}
0

-, , , , , , , - "" . , - .

-, , - : , , . , , WMI . : .

Thirdly, I suppose you know about it, but I’ll say it anyway: your problem is the launcher. If it is so slow that regular users find a way around this, it is too slow. The best solution is not to fix the main application, but to fix the launch. Edit: See Anders Karlsson for a better mechanism.

0
source

All Articles