代码之家  ›  专栏  ›  技术社区  ›  Gabriel Morin

如何使用记事本++作为7zip的编辑器,而不显示控制台窗口?

  •  1
  • Gabriel Morin  · 技术社区  · 7 年前

    这个问题可以概括为: .

    要使用记事本++作为7zip的编辑器,您需要启动 notepad++.exe -multiInst 命令行参数,否则它会立即关闭参数并将其转发到现有实例。由于7zip在被调用的程序关闭时会拾取您对其临时文件所做的更改,因此您永远都没有机会对其进行编辑。

    问题是,7zip不允许您为作为编辑器配置的任何程序输入参数。

    • 调用一个批处理文件,但在整个版本中,我一直被一个难看(并且很容易意外关闭)的控制台窗口所困扰——这是不可接受的。
    • start 调用Notepad++:控制台窗口确实会关闭,但不幸的是,Notepad++正在查看的批处理执行器进程已经消失,因此它认为您已经完成了编辑,即返回到初始问题。
    • Wait for program to complete )它使你在维护模式中依赖于旧技术,这具有恶意软件的内涵。

    注意:这个问题与 Execute Batch File without Command line visible 因为这增加了一个要求,即使用的任何启动器都必须在启动进程的整个生命周期内保持打开状态,并且不能将命令行参数传递给启动器。

    1 回复  |  直到 4 年前
        1
  •  1
  •   Gabriel Morin    4 年前

    #include <fstream>
    #include <string>
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <filesystem>
    
    HINSTANCE hInst;
    
    void launchProcess(std::wstring commandLine);
    
    int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                         _In_opt_ HINSTANCE hPrevInstance,
                         _In_ LPWSTR    lpCmdLine,
                         _In_ int       nCmdShow)
    {
        UNREFERENCED_PARAMETER(hPrevInstance);
        UNREFERENCED_PARAMETER(lpCmdLine);
        //Get current executable's location
        HMODULE hModule = GetModuleHandleW(NULL);
        WCHAR executableFolder[MAX_PATH];
        GetModuleFileNameW(hModule, executableFolder, MAX_PATH);
        std::experimental::filesystem::v1::path path(executableFolder);
        path.remove_filename();
        path.append(L"NoConsoleProgramLauncher_Arguments.txt");
        std::wifstream infile(path);
        std::wstring commandLine;
        std::getline(infile, commandLine);
        commandLine += L" ";
        commandLine += lpCmdLine;
        launchProcess(commandLine);
    }
    
    void launchProcess(std::wstring commandLine)
    {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
    
        ZeroMemory(&si, sizeof(si));
        si.cb = sizeof(si);
        ZeroMemory(&pi, sizeof(pi));
    
        // Start the child process. 
        if (!CreateProcess(NULL,   // No module name (use command line)
            &commandLine[0],        // Command line - C++ 11 guarantees that string's internal buffer is contiguous and null-terminated.
            NULL,           // Process handle not inheritable
            NULL,           // Thread handle not inheritable
            FALSE,          // Set handle inheritance to FALSE
            CREATE_NO_WINDOW,              // No creation flags
            NULL,           // Use parent's environment block
            NULL,           // Use parent's starting directory 
            &si,            // Pointer to STARTUPINFO structure
            &pi)           // Pointer to PROCESS_INFORMATION structure
            )
        {
            printf("CreateProcess failed (%d).\n", GetLastError());
            return;
        }
    
        // Wait until child process exits.
        WaitForSingleObject(pi.hProcess, INFINITE);
    
        // Close process and thread handles. 
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }
    

    Windows桌面应用程序 项目,如有需要,修复包括,并建造。

    不会显示控制台窗口,程序将在退出之前等待生成的进程终止,因此7zip一直在等待获取更改 .

    这是我放在我的 NoConsoleProgramLauncher_Arguments.txt 文件:

    "C:\Program Files (x86)\Notepad++\notepad++.exe" -multiInst -nosession