Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可使用如下代码将控制台程序转换为窗口程序 #1

Closed
Laster-dev opened this issue Aug 2, 2024 · 2 comments
Closed

可使用如下代码将控制台程序转换为窗口程序 #1

Laster-dev opened this issue Aug 2, 2024 · 2 comments

Comments

@Laster-dev
Copy link

#include
#include <windows.h>

bool ConvertToGUI(const char* exePath) {
HANDLE hFile = CreateFileA(exePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to open file: " << GetLastError() << std::endl;
return false;
}

DWORD bytesRead;
IMAGE_DOS_HEADER dosHeader;
if (!ReadFile(hFile, &dosHeader, sizeof(dosHeader), &bytesRead, NULL) || bytesRead != sizeof(dosHeader)) {
    std::cerr << "Failed to read DOS header: " << GetLastError() << std::endl;
    CloseHandle(hFile);
    return false;
}

if (dosHeader.e_magic != IMAGE_DOS_SIGNATURE) {
    std::cerr << "Invalid DOS signature." << std::endl;
    CloseHandle(hFile);
    return false;
}

if (SetFilePointer(hFile, dosHeader.e_lfanew, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
    std::cerr << "Failed to set file pointer: " << GetLastError() << std::endl;
    CloseHandle(hFile);
    return false;
}

IMAGE_NT_HEADERS ntHeaders;
if (!ReadFile(hFile, &ntHeaders, sizeof(ntHeaders), &bytesRead, NULL) || bytesRead != sizeof(ntHeaders)) {
    std::cerr << "Failed to read NT headers: " << GetLastError() << std::endl;
    CloseHandle(hFile);
    return false;
}

if (ntHeaders.Signature != IMAGE_NT_SIGNATURE) {
    std::cerr << "Invalid NT signature." << std::endl;
    CloseHandle(hFile);
    return false;
}

ntHeaders.OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;

if (SetFilePointer(hFile, dosHeader.e_lfanew, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
    std::cerr << "Failed to set file pointer: " << GetLastError() << std::endl;
    CloseHandle(hFile);
    return false;
}

DWORD bytesWritten;
if (!WriteFile(hFile, &ntHeaders, sizeof(ntHeaders), &bytesWritten, NULL) || bytesWritten != sizeof(ntHeaders)) {
    std::cerr << "Failed to write NT headers: " << GetLastError() << std::endl;
    CloseHandle(hFile);
    return false;
}

CloseHandle(hFile);
return true;

}

int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <path_to_exe>" << std::endl;
return 1;
}

const char* exePath = argv[1];
if (ConvertToGUI(exePath)) {
    std::cout << exePath << " has been converted to a Windows GUI application." << std::endl;
    return 0;
}
else {
    std::cerr << "Failed to convert " << exePath << " to a Windows GUI application." << std::endl;
    return 1;
}

}

@yj94
Copy link
Owner

yj94 commented Aug 2, 2024

收到你的提议,我会跟进的,编译测试后会作为tools放在该项目!感谢您

@yinsel
Copy link

yinsel commented Aug 3, 2024

也可以使用LordPE:https://down.52pojie.cn/Tools/PEtools/LordPE.7z

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants