亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

防止進程看到現有實例

防止進程看到現有實例

C#
哈士奇WWW 2022-01-15 16:47:26
這是情況。我有一個應用程序,出于所有意圖和目的,我必須將其視為黑匣子。我需要能夠打開這個應用程序的多個實例,每個實例都帶有一組文件。打開 this 的語法是executable.exe file1.ext file2.ext.如果我在executable.exe沒有參數的情況下運行 x 次,則新實例可以正常打開。如果我executable.exe file1.ext隨后運行,executable.exe file2.ext則第二次調用會在現有窗口中打開文件 2,而不是創建新實例。這會干擾我的其他解決方案,這就是問題所在。我的解決方案包裝了這個應用程序并對其執行各種管理操作,這是我的包裝類之一:public class myWrapper{    public event EventHandler<IntPtr> SplashFinished;    public event EventHandler ProcessExited;    private const string aaTrendLocation = @"redacted";    //private const string aaTrendLocation = "notepad";    private readonly Process _process;    private readonly Logger _logger;    public myWrapper(string[] args, Logger logger =null)    {        _logger = logger;        _logger?.WriteLine("Intiialising new wrapper object...");        if (args == null || args.Length < 1) args = new[] {""};        ProcessStartInfo info = new ProcessStartInfo(aaTrendLocation,args.Aggregate((s,c)=>$"{s} {c}"));        _process = new Process{StartInfo = info};    }    public void Start()    {        _logger?.WriteLine("Starting process...");        _logger?.WriteLine($"Process: {_process.StartInfo.FileName} || Args: {_process.StartInfo.Arguments}");        _process.Start();        Task.Run(()=>MonitorSplash());        Task.Run(() => MonitorLifeTime());    }在我開始提供文件參數并且這種意外的實例化行為開始之前,我的包裝類都可以正常工作。我無法修改目標應用程序,也無法訪問其源代碼來確定此實例化是使用互斥鎖還是通過其他某些功能進行管理的。因此,我需要確定是否有辦法阻止新實例看到現有實例。有人有什么建議嗎?TLDR:如何防止僅限于單個實例的應用程序確定已經有一個實例正在運行為了澄清(在可疑評論之后),我公司的研發團隊寫道executable.exe,但我沒有時間等待他們在這件事上的幫助(我有幾天而不是幾個月)并且有權做任何需要的事情來提供所需的功能(有一個我的解決方案比這個問題提到的更多)迅速。通過一些反編譯工作,我可以看到正在使用以下內容來查找現有實例。Process[] processesByName = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);有什么辦法可以解決創建具有不同名稱的應用程序的多個副本的情況嗎?我考慮過在Process運行中重命名,但顯然這不可能不寫內核漏洞......
查看完整描述

2 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

我過去通過創建源可執行文件的副本解決了這個問題。在您的情況下,您可以:

  • 將“original.exe”保存在特定位置。

  • 每次需要調用它時,創建 original.exe 的副本并將其命名為“instance_xxxx.exe”,其中 xxxx 是唯一編號。

  • 根據需要執行您的新實例 exe,完成后您可以將其刪除

  • 您甚至可以通過創建實例池來重用這些實例


查看完整回答
反對 回復 2022-01-15
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

在 Dave Lucre 的回答的基礎上,我通過創建綁定到我的包裝類的可執行文件的新實例來解決它。最初,我繼承IDisposable并刪除了 Disposer 中的臨時文件,但由于某種原因導致清理會阻塞應用程序的問題,所以現在我的主程序最后執行清理。


我的構造函數現在看起來像:


public AaTrend(string[] args, ILogger logger = null)

{

    _logger = logger;

    _logger?.WriteLine("Initialising new aaTrend object...");

    if (args == null || args.Length < 1) args = new[] { "" };

    _tempFilePath = GenerateTempFileName();

    CreateTempCopy(); //Needed to bypass lazy single instance checks

    HideTempFile(); //Stops users worrying

    ProcessStartInfo info = new ProcessStartInfo(_tempFilePath, args.Aggregate((s, c) => $"{s} {c}"));

    _process = new Process { StartInfo = info };

}

使用兩種新方法:


    private void CreateTempCopy()

    {

        _logger?.WriteLine("Creating temporary file...");

        _logger?.WriteLine(_tempFilePath);

        File.Copy(AaTrendLocation, _tempFilePath);

    }


    private string GenerateTempFileName(int increment = 0)

    {

        string directory = Path.GetDirectoryName(AaTrendLocation); //Obtain pass components.

        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(AaTrendLocation);

        string extension = Path.GetExtension(AaTrendLocation);

        string tempName = $"{directory}\\{fileNameWithoutExtension}-{increment}{extension}"; //Re-assemble path with increment inserted.

        return File.Exists(tempName) ? GenerateTempFileName(++increment) : tempName; //If this name is already used, increment an recurse otherwise return new path.

    }

然后在我的主程序中:


    private static void DeleteTempFiles()

    {

        string dir = Path.GetDirectoryName(AaTrend.AaTrendLocation);

        foreach (string file in Directory.GetFiles(dir, "aaTrend-*.exe", SearchOption.TopDirectoryOnly))

        {

            File.Delete(file);

        }

    }

作為旁注,這種方法僅適用于具有(惰性)確定實例化方法的應用程序,這些方法依賴于Process.GetProcessByName(); Mutex如果使用 a 或者如果在清單中明確設置了可執行文件名稱,它將不起作用。


查看完整回答
反對 回復 2022-01-15
  • 2 回答
  • 0 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號