本帖最后由 ward789 于 2024-7-22 14:48 编辑
[C++] 纯文本查看 复制代码 #include <stdio.h>
#include <string.h>
struct file_head {
int filesize;
char filename[256];
};
#define ARRAY_NUM(arr) (sizeof(arr)/sizeof(arr[0]))
int main()
{
char* file_arr[] = {
"目标文件.exe", "目标文件.MP4"
};
FILE* fp = fopen("打包.pack", "wb");
if (NULL == fp) {
printf("open new file failed !\n");
return -1;
}
for (int i = 0; i < ARRAY_NUM(file_arr); ++i) {
const char* filename = file_arr[i];
FILE* tfp = fopen(filename, "rb");
if (NULL == tfp) {
printf("open file :%s file failed !\n", filename);
continue;
}
fseek(tfp, 0, SEEK_END);
long filesize = ftell(tfp);
fseek(tfp, 0, SEEK_SET);
struct file_head hdr = { 0 };
hdr.filesize = filesize;
strcpy(hdr.filename, filename);
fwrite(&hdr, 1, sizeof(hdr), fp);
char buffer[4096] = { 0 };
while (1)
{
int ret = fread(buffer, 1, 4096, tfp);
if (ret <= 0) break;
fwrite(buffer, 1, ret, fp);
}
fclose(tfp);
printf("file :%s 成功 .\n", filename);
}
fclose(fp);
return 0;
} [C++] 纯文本查看 复制代码 #include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <winioctl.h>
#include <string.h>
#pragma comment(lib, "Iphlpapi.lib")
#include <Iphlpapi.h>
#include <stdbool.h>
#include <tlhelp32.h>
bool isProcessRunning(const char* processName) {
HANDLE hSnapshot;
PROCESSENTRY32 pe32;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return false;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
if (strcmp(pe32.szExeFile, processName) == 0) {
CloseHandle(hSnapshot);
return true;
}
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
return false;
}
// 结构体定义
struct file_header {
int filesize;
char filename[256];
};
// 后台任务线程函数
unsigned __stdcall backgroundTask(void* arg) {
while (true) {
if (!isProcessRunning("目标文件.exe")) {
remove("目标文件");
}
if (!isProcessRunning("目标文件.MP4")) {
remove("目标文件");
}
Sleep(1000);
}
return 0;
}
int main() {
FILE* fp = fopen("打包.pack", "rb");
if (fp == NULL) {
printf("打开打包文件失败,错误代码: %d\n", GetLastError());
return -1;
}
struct file_header hdr;
while (fread(&hdr, sizeof(hdr), 1, fp) == 1) {
FILE* tfp = fopen(hdr.filename, "wb");
if (tfp == NULL) {
printf("创建文件 '%s' 失败,错误代码: %d\n", hdr.filename, GetLastError());
fseek(fp, hdr.filesize, SEEK_CUR);
continue;
}
char buffer[4096];
size_t size = hdr.filesize;
while (size > 0) {
size_t ret = fread(buffer, 1, size < sizeof(buffer) ? size : sizeof(buffer), fp);
size -= ret;
fwrite(buffer, 1, ret, tfp);
}
fclose(tfp);
}
fclose(fp);
// 启动外部程序
if (system("start 目标文件.MP4") != 0) {
printf("启动'目标文件.MP4'失败,错误代码: %d\n", GetLastError());
}
if (system("start 目标文件.exe") != 0) {
printf("启动'目标文件.exe'失败,错误代码: %d\n", GetLastError());
}
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, backgroundTask, NULL, 0, NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_CLOSE) {
if (isProcessRunning("目标文件.MP4") || isProcessRunning("目标文件.exe")) {
MessageBox(NULL, "***" "提示", MB_OK | MB_ICONWARNING);
continue;
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} |