#include<iostream>
#include<windows.h>
#include<tchar.h>
#include<stdio.h>
#include <process.h>
#pragma warning(disable:4996)
//exe应用程序
VOID PrintUI(CONST CHAR* ExeName, CONST CHAR* UIName, CONST CHAR* color, SHORT X坐标, SHORT y坐标, WORD UIwide, WORD UIHigh)
{
AllocConsole();
freopen("conin$", "r+t", stdin);
freopen("conout$", "w+t", stdout);
SetConsoleTitleA(UIName);
system(color);
CHAR oldPathName[1024];
CHAR NewPathName[1024];
GetModuleFileNameA(NULL,oldPathName, 1024);
printf("%s\n", oldPathName);
strcpy(NewPathName,oldPathName);
for (size_t i = strlen(NewPathName); i > 0; i--)
{
if (NewPathName[i-1] == '\\')
{
NewPathName[i] = '\0';
strcat(NewPathName, ExeName);
break;
}
}
CHAR c_ommand[MAXBYTE*2] = { '\0'};
strcat(c_ommand,"del/f/q\"");
strcat(c_ommand, NewPathName);
strcat(c_ommand, "\">nul 2>nul");
system(c_ommand);
rename(oldPathName, NewPathName);
HWND PrinUI = GetConsoleWindow();
SetWindowPos(PrinUI, 0, 0, 0, UIwide,UIHigh,SWP_NOMOVE);
SetWindowPos(PrinUI, 0, X坐标, y坐标, UIwide, UIHigh, SWP_NOMOVE);
std::wcout.imbue(std::locale("CHS"));
}
unsigned _stdcall ThreadPOOL(VOID* PArg)
{
for (size_t i = 0; i < 60; i++) {
SendMessage((HWND)PArg, WM_PASTE, 0, 0);/*发送剪切板内容到窗口*/
Sleep(20);
}
return 0;
}
//unsigned _stdcall ThreadTwo(VOID* PArg)
//{
// for (size_t i = 0; i < 60; i++) {
// SendMessage((HWND)0x00020B32, WM_PASTE, 0, 0);/*发送剪切板内容到窗口*/
//
// }
// return 0;
//}
//unsigned _stdcall ThreadThree(VOID* PArg)
//{
// for (size_t i = 0; i < 60; i++) {
// SendMessage((HWND)0x00020B32, WM_PASTE, 0, 0);/*发送剪切板内容到窗口*/
//
// }
// return 0;
//}
HGLOBAL sysobjects = 0;
VOID clipboard(CONST CHAR* waitcpoy, DWORD memberylength)
{
GlobalUnlock(sysobjects);/*释放锁定的系统对象*/
GlobalFree(sysobjects);/*释放内存对象*/
if (OpenClipboard(0)) {
EmptyClipboard();
sysobjects = GlobalAlloc(GMEM_MOVEABLE, memberylength);/*全局内存分配空间,调用的内核层的内存*/
if (true)
{
CHAR* member_y = (CHAR*)GlobalLock(sysobjects);/*锁定系统对象并且返回内存地址*/
memcpy(member_y, waitcpoy, memberylength);
member_y[memberylength] = 0;
SetClipboardData(CF_TEXT, sysobjects);/*应用程序将 WM_PASTE 消息发送到编辑控件或组合框,以将剪贴板的当前内容复制到位于当前插入点位置的编辑控件。
仅当剪贴板包含 CF_TEXT 格式的数据时,才会插入数据。*/
}
CloseClipboard();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
PrintUI("new.exe", "MYWINDOW","color 09", (2560-500)/2-50, (1400-500)/2, 500, 500);
printf("你好,hello\r\n");
clipboard("你好同学,我来叫你上课",24);
HWND HanDarray[3];
HanDarray[0] = (HWND)0x00060CBA;
HanDarray[1] = (HWND)0x00070B8E;
HanDarray[2] = (HWND)0x00010D86;
for (size_t i = 0; i < 3; i++)
{
_beginthreadex(NULL, NULL, ThreadPOOL, HanDarray[i], NULL, 0);
}
/*_beginthreadex(NULL, NULL, ThreadOne, HanDarray[0], NULL,0);
_beginthreadex(NULL, NULL, ThreadTwo,HanDarray[1], NULL, 0);
_beginthreadex(NULL, NULL, ThreadThree, HanDarray[2], NULL, 0);*/
//for (size_t i = 0; i < 60; i++) {
// SendMessage((HWND)0x00020B32, WM_PASTE, 0, 0);/*发送剪切板内容到窗口*/
//}
//Sleep(200000);
}
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <process.h>
#pragma warning(disable:4996)
VOID PrintUI(CONST CHAR* ExeName, CONST CHAR* UIName, CONST CHAR* color, SHORT x坐标, SHORT y坐标, WORD UI宽度, WORD UI高度)
{
if (!AllocConsole())
return;
freopen("conin$", "r+t", stdin);
freopen("conout$", "w+t", stdout);
SetConsoleTitleA(UIName);
system(color);
CHAR oldPathName[MAX_PATH];
GetModuleFileNameA(NULL, oldPathName, sizeof(oldPathName));
// 替代 PathRemoveFileSpecA
CHAR newPathName[MAX_PATH];
strcpy(newPathName, oldPathName);
CHAR* lastSlash = strrchr(newPathName, '\\');
if (lastSlash != NULL) {
*lastSlash = '\0'; // 移除文件名,保留路径
}
strcat(newPathName, "\\");
strcat(newPathName, ExeName); // 拼接新的文件名
CHAR command[MAX_PATH * 2];
snprintf(command, sizeof(command), "del /f /q \"%s\" > nul 2> nul", newPathName);
system(command);
if (rename(oldPathName, newPathName) != 0) {
perror("Rename failed");
}
HWND consoleWindow = GetConsoleWindow();
SetWindowPos(consoleWindow, NULL, x坐标, y坐标, UI宽度, UI高度, SWP_NOZORDER);
}
unsigned _stdcall ThreadPOOL(VOID* PArg)
{
HWND hwnd = (HWND)PArg;
if (!IsWindow(hwnd)) {
std::cerr << "Invalid window handle: " << hwnd << std::endl;
return 1; // 返回非零值表示错误
}
for (size_t i = 0; i < 60; i++) {
SendMessage(hwnd, WM_PASTE, 0, 0); // 发送剪切板内容到窗口
Sleep(20);
}
return 0;
}
HGLOBAL sysobjects = 0;
VOID clipboard(CONST CHAR* waitcpoy, DWORD memberylength)
{
GlobalUnlock(sysobjects);
GlobalFree(sysobjects);
if (OpenClipboard(0)) {
EmptyClipboard();
sysobjects = GlobalAlloc(GMEM_MOVEABLE, memberylength);
if (sysobjects) {
CHAR* member_y = (CHAR*)GlobalLock(sysobjects);
memcpy(member_y, waitcpoy, memberylength);
member_y[memberylength] = 0;
SetClipboardData(CF_TEXT, sysobjects);
}
CloseClipboard();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
PrintUI("new.exe", "MYWINDOW", "color 09", (2560 - 500) / 2 - 50, (1400 - 500) / 2, 500, 500);
printf("你好,hello\r\n");
clipboard("你好同学,我来叫你上课", 24);
HWND HanDarray[3];
HanDarray[0] = (HWND)0x00060CBA;
HanDarray[1] = (HWND)0x00070B8E;
HanDarray[2] = (HWND)0x00010D86;
for (size_t i = 0; i < 3; i++) {
HANDLE threadHandle = (HANDLE)_beginthreadex(NULL, 0, ThreadPOOL, HanDarray[i], 0, NULL);
if (threadHandle == NULL) {
std::cerr << "Thread creation failed with error: " << GetLastError() << std::endl;
}
else {
CloseHandle(threadHandle); // 关闭线程句柄,防止资源泄漏
}
}
// 等待线程完成的逻辑可以在这里添加
Sleep(2000); // 保持主线程活跃,以便观察结果
return 0;
}
互斥锁
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <process.h>
#pragma warning(disable:4996)
VOID PrintUI(CONST CHAR* ExeName, CONST CHAR* UIName, CONST CHAR* color, SHORT x坐标, SHORT y坐标, WORD UI宽度, WORD UI高度)
{
if (!AllocConsole())
return;
freopen("conin$", "r+t", stdin);
freopen("conout$", "w+t", stdout);
SetConsoleTitleA(UIName);
system(color);
CHAR oldPathName[MAX_PATH];
GetModuleFileNameA(NULL, oldPathName, sizeof(oldPathName));
// 替代 PathRemoveFileSpecA
CHAR newPathName[MAX_PATH];
strcpy(newPathName, oldPathName);
CHAR* lastSlash = strrchr(newPathName, '\\');
if (lastSlash != NULL) {
*lastSlash = '\0'; // 移除文件名,保留路径
}
strcat(newPathName, "\\");
strcat(newPathName, ExeName); // 拼接新的文件名
CHAR command[MAX_PATH * 2];
snprintf(command, sizeof(command), "del /f /q \"%s\" > nul 2> nul", newPathName);
system(command);
if (rename(oldPathName, newPathName) != 0) {
perror("Rename failed");
}
HWND consoleWindow = GetConsoleWindow();
SetWindowPos(consoleWindow, NULL, x坐标, y坐标, UI宽度, UI高度, SWP_NOZORDER);
}
CRITICAL_SECTION mutex_lock;
unsigned _stdcall LockThreadPOOL(VOID* PArg)
{
HWND hwnd = (HWND)PArg;
if (!IsWindow(hwnd)) {
std::cerr << "Invalid window handle: " << hwnd << std::endl;
return 1; // 返回非零值表示错误
}
for (size_t i = 0; i < 20; i++) {
SendMessage(hwnd, WM_PASTE, 0, 0); // 发送剪切板内容到窗口
Sleep(20);
LeaveCriticalSection(&mutex_lock);
}
return 0;
}
//资源互斥锁
//全局锁
HGLOBAL sysobjects = 0;
VOID clipboard(CONST CHAR* waitcpoy, DWORD memberylength)
{
GlobalUnlock(sysobjects);
GlobalFree(sysobjects);
if (OpenClipboard(0)) {
EmptyClipboard();
sysobjects = GlobalAlloc(GMEM_MOVEABLE, memberylength);
if (sysobjects) {
CHAR* member_y = (CHAR*)GlobalLock(sysobjects);
memcpy(member_y, waitcpoy, memberylength);
member_y[memberylength] = 0;
SetClipboardData(CF_TEXT, sysobjects);
}
CloseClipboard();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
PrintUI("new.exe", "MYWINDOW", "color 09", (2560 - 500) / 2 - 50, (1400 - 500) / 2, 500, 500);
printf("你好,hello\r\n");
clipboard("你好同学,我来叫你上课", 24);
//HWND HanDarray[3];
//HanDarray[0] = (HWND)0x00060CBA;
//HanDarray[1] = (HWND)0x00070B8E;
//HanDarray[2] = (HWND)0x00010D86;
//for (size_t i = 0; i < 3; i++) {
// HANDLE threadHandle = (HANDLE)_beginthreadex(NULL, 0, ThreadPOOL, HanDarray[i], 0, NULL);
// if (threadHandle == NULL) {
// std::cerr << "Thread creation failed with error: " << GetLastError() << std::endl;
// }
// else {
// CloseHandle(threadHandle); // 关闭线程句柄,防止资源泄漏
// }
//}
// 等待线程完成的逻辑可以在这里添加
InitializeCriticalSection(&mutex_lock);//初始化锁
HWND HanDarray[3];
HanDarray[0] = (HWND)0x00010E8A;
HanDarray[1] = (HWND)0x00010E92;
HanDarray[2] = (HWND)0x00010E9A;
for (size_t i = 0; i < 3; i++) {
HANDLE threadHandle = (HANDLE)_beginthreadex(NULL, 0, LockThreadPOOL, HanDarray[i], 0, NULL);
if (threadHandle == NULL) {
std::cerr << "Thread creation failed with error: " << GetLastError() << std::endl;
}
else {
CloseHandle(threadHandle); // 关闭线程句柄,防止资源泄漏
}
}
Sleep(2000); // 保持主线程活跃,以便观察结果
return 0;
}