#include <windows.h>
#include <fstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <sstream>
#include <iostream>
#include <thread>
#include <chrono>
// 全局变量
HWND hEdit, hButton, hOutput, hUsername, hPassword, hLoginButton, hRegisterButton, hActivateButton, hActivationCode;
HWND hActivationLabel, hUsernameLabel, hPasswordLabel, hInputBox, hInputButton;
HANDLE hOutputReadTmp, hOutputRead, hOutputWrite;
HANDLE hInputWriteTmp, hInputRead, hInputWrite;
std::unordered_map<std::string, std::string> users;
bool activated = false;
// 加载用户数据
void loadUserData() {
std::ifstream file("users.txt");
std::string username, password;
while (file >> username >> password) {
users[username] = password;
}
file.close();
}
// 保存用户数据
void saveUserData() {
std::ofstream file("users.txt");
for (const auto& pair : users) {
file << pair.first << " " << pair.second << "\n";
}
file.close();
}
// 运行命令并获取输出
std::string runCommand(const std::string& command) {
std::string result;
char buffer[128];
FILE* pipe = _popen(command.c_str(), "r");
if (!pipe) return "popen failed!";
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
result += buffer;
}
_pclose(pipe);
return result;
}
// 编译和运行代码
void compileAndRun(const std::string &sourceCode) {
std::ofstream file("test.cpp");
file << sourceCode;
file.close();
std::string compileResult = runCommand("g++ test.cpp -O2 -g -Wall -o test.exe 2>&1");
if (!compileResult.empty()) {
SetWindowTextA(hOutput, compileResult.c_str());
return;
}
// 创建管道
// HANDLE hOutputReadTmp, hOutputRead, hOutputWrite;
// HANDLE hInputWriteTmp, hInputRead, hInputWrite;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hOutputReadTmp, &hOutputWrite, &sa, 0)) return;
if (!DuplicateHandle(GetCurrentProcess(), hOutputReadTmp, GetCurrentProcess(), &hOutputRead, 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
CloseHandle(hOutputReadTmp);
if (!CreatePipe(&hInputRead, &hInputWriteTmp, &sa, 0)) return;
if (!DuplicateHandle(GetCurrentProcess(), hInputWriteTmp, GetCurrentProcess(), &hInputWrite, 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
CloseHandle(hInputWriteTmp);
// 设置启动信息
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hOutputWrite;
si.hStdError = hOutputWrite;
si.hStdInput = hInputRead;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
// 启动子进程
if (!CreateProcess(NULL, (LPSTR)"test.exe", NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) return;
CloseHandle(hOutputWrite);
CloseHandle(hInputRead);
// 读取子进程的输出
std::thread outputThread([&]() {
DWORD read;
CHAR chBuf[4096];
BOOL success;
std::string output;
while (true) {
success = ReadFile(hOutputRead, chBuf, 4096, &read, NULL);
if (!success || read == 0) break;
output.append(chBuf, read);
SetWindowTextA(hOutput, output.c_str());
}
});
// 等待子进程结束
std::thread waitThread([&]() {
WaitForSingleObject(pi.hProcess, INFINITE);
if (outputThread.joinable()) {
outputThread.join();
}
CloseHandle(hOutputRead);
CloseHandle(hInputWrite);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
});
waitThread.detach();
}
// 窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: {
loadUserData();
// 创建激活界面
hActivationLabel = CreateWindowExW(0, L"STATIC", L"Key",
WS_CHILD | WS_VISIBLE,
120, 100, 80, 25,
hwnd, NULL, NULL, NULL);
hActivationCode = CreateWindowExW(0, L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT,
200, 100, 200, 25,
hwnd, (HMENU)5, NULL, NULL);
hActivateButton = CreateWindowExW(0, L"BUTTON", L"Activate",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
200, 140, 80, 30,
hwnd, (HMENU)6, NULL, NULL);
return 0;
}
case WM_COMMAND: {
if (LOWORD(wParam) == 6) { // 激活按钮
wchar_t codeBuffer[100];
GetWindowTextW(hActivationCode, codeBuffer, 100);
std::wstring wsCode(codeBuffer);
std::string code(wsCode.begin(), wsCode.end());
if (code == "123456") {
activated = true;
DestroyWindow(hActivationCode);
DestroyWindow(hActivateButton);
DestroyWindow(hActivationLabel);
// 创建登录界面
hUsernameLabel = CreateWindowExW(0, L"STATIC", L"Username",
WS_CHILD | WS_VISIBLE,
120, 100, 80, 25,
hwnd, NULL, NULL, NULL);
hUsername = CreateWindowExW(0, L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT,
200, 100, 200, 25,
hwnd, (HMENU)1, NULL, NULL);
hPasswordLabel = CreateWindowExW(0, L"STATIC", L"Password",
WS_CHILD | WS_VISIBLE,
120, 140, 80, 25,
hwnd, NULL, NULL, NULL);
hPassword = CreateWindowExW(0, L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_PASSWORD,
200, 140, 200, 25,
hwnd, (HMENU)2, NULL, NULL);
hLoginButton = CreateWindowExW(0, L"BUTTON", L"Login",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
200, 180, 80, 30,
hwnd, (HMENU)3, NULL, NULL);
hRegisterButton = CreateWindowExW(0, L"BUTTON", L"Register",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
320, 180, 80, 30,
hwnd, (HMENU)4, NULL, NULL);
} else {
MessageBoxW(hwnd, L"Activation failed!", L"Error", MB_OK);
}
} else if (LOWORD(wParam) == 3) { // 登录按钮
wchar_t usernameBuffer[100], passwordBuffer[100];
GetWindowTextW(hUsername, usernameBuffer, 100);
GetWindowTextW(hPassword, passwordBuffer, 100);
std::wstring wsUsername(usernameBuffer), wsPassword(passwordBuffer);
std::string username(wsUsername.begin(), wsUsername.end()), password(wsPassword.begin(), wsPassword.end());
if (users.find(username) != users.end() && users[username] == password) {
// 登录成功,进入代码编辑和运行界面
DestroyWindow(hUsername);
DestroyWindow(hPassword);
DestroyWindow(hLoginButton);
DestroyWindow(hRegisterButton);
DestroyWindow(hUsernameLabel);
DestroyWindow(hPasswordLabel);
// 创建代码编辑和运行界面
hEdit = CreateWindowExW(0, L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
10, 10, 800, 400,
hwnd, (HMENU)7, NULL, NULL);
hButton = CreateWindowExW(0, L"BUTTON", L"Compile & Run",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
820, 10, 100, 30,
hwnd, (HMENU)8, NULL, NULL);
hOutput = CreateWindowExW(0, L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY,
10, 420, 800, 200,
hwnd, (HMENU)9, NULL, NULL);
hInputBox = CreateWindowExW(0, L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
820, 50, 100, 100,
hwnd, (HMENU)10, NULL, NULL);
hInputButton = CreateWindowExW(0, L"BUTTON", L"Send Input",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
820, 160, 100, 30,
hwnd, (HMENU)11, NULL, NULL);
} else {
MessageBoxW(hwnd, L"Login failed!", L"Error", MB_OK);
}
} else if (LOWORD(wParam) == 4) { // 注册按钮
wchar_t usernameBuffer[100], passwordBuffer[100];
GetWindowTextW(hUsername, usernameBuffer, 100);
GetWindowTextW(hPassword, passwordBuffer, 100);
std::wstring wsUsername(usernameBuffer), wsPassword(passwordBuffer);
std::string username(wsUsername.begin(), wsUsername.end()), password(wsPassword.begin(), wsPassword.end());
if (users.find(username) == users.end()) {
users[username] = password;
saveUserData();
MessageBoxW(hwnd, L"Registration successful!", L"Info", MB_OK);
} else {
MessageBoxW(hwnd, L"Username already exists!", L"Error", MB_OK);
}
} else if (LOWORD(wParam) == 8) { // 编译和运行按钮
wchar_t codeBuffer[65536];
GetWindowTextW(hEdit, codeBuffer, 65536);
std::wstring wsCode(codeBuffer);
std::string code(wsCode.begin(), wsCode.end());
compileAndRun(code);
} else if (LOWORD(wParam) == 11) { // 发送输入按钮
// 发送用户输入到子进程
wchar_t inputBuffer[65536];
GetWindowTextW(hInputBox, inputBuffer, 65536);
std::wstring wsInput(inputBuffer);
std::string input(wsInput.begin(), wsInput.end());
input += "\n"; // 添加换行符作为输入结束标志
DWORD written;
WriteFile(hInputWrite, input.c_str(), input.size(), &written, NULL);
}
return 0;
}
case WM_DESTROY: {
PostQuitMessage(0);
return 0;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
// 主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
const wchar_t CLASS_NAME[] = L"SwiftCompile Window Class";
WNDCLASSW wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClassW(&wc);
HWND hwnd = CreateWindowExW(
0,
CLASS_NAME,
L"SwiftCompile",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 1000, 700,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, iCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
求实现正常编译功能的方法。