简体   繁体   English

C ++和Windows-如何覆盖正在运行的程序的exe文件?

[英]C++ and Windows - how to overwrite exe file of the running program?

I tried to make self-updatable program, but I can't understand, how to over-write exe file of the running program (of the current process). 我试图制作可自我更新的程序,但我不明白如何覆盖正在运行的程序(当前进程)的exe文件。 When Exe file is running, it's locked by process and can't be writable. 当Exe文件正在运行时,它已被进程锁定,并且不可写。

How to update program - I need to update file, close current process and re-run updated file... 如何更新程序-我需要更新文件,关闭当前进程并重新运行更新的文件...

Maybe my question is silly, but I haven't this problem, until I used Linux... Cheers! 也许我的问题很愚蠢,但是直到我使用Linux之前,我才没有这个问题……干杯!

I solved this question by packing needed file into the another one wrapper via Resources in MS VS. 我通过将所需文件通过MS VS中的Resources打包到另一个包装中解决了这个问题。

Here is code to extract resource into the file: 以下是将资源提取到文件中的代码:

#include "stdafx.h"
#include "resource.h"
#include "windows.h"


int main()
{
    HRSRC hrsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_BINARYTYPE1), _T("BINARYTYPE"));
        //FindResource(NULL, MAKEINTRESOURCE(IDR_BINARYTYPE1), RT_BITMAP);
    HGLOBAL hLoaded = LoadResource(NULL, hrsrc);
    LPVOID lpLock = LockResource(hLoaded);
    DWORD dwSize = SizeofResource(NULL, hrsrc);
    HANDLE hFile = CreateFile(TEXT("c:/temp/zxcv.exe"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD dwByteWritten;
    WriteFile(hFile, lpLock, dwSize, &dwByteWritten, NULL);
    CloseHandle(hFile);
    FreeResource(hLoaded);

    return 0;
}

I am not realized it by 100%, but I have plan to pack my exe into the wrapper, that will unpack my exe into the %Temp% directory and will start unpacked exe file. 我还没有100%意识到这一点,但是我已经计划将我的exe打包到包装器中,这会将我的exe打包到%Temp%目录中,并开始打包的exe文件。 Unpacked file will be deleted with DELETE_ON_CLOSE. 解压缩后的文件将通过DELETE_ON_CLOSE删除。

It's just plan, but I see possible solution :). 这只是计划,但我看到了可能的解决方案:)。

Thanks to all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM