mirror of
https://github.com/yuanyuanxiang/SimpleRemoter.git
synced 2026-01-21 23:13:08 +08:00
Fix: Copy text between master and client need a delay
This commit is contained in:
@@ -134,21 +134,21 @@ static BOOL HandleServiceCommandLine()
|
||||
// -service: 作为服务运行
|
||||
if (cmdLine.Find(_T("-service")) != -1) {
|
||||
int r = ServerService_Run();
|
||||
Mprintf("[HandleServiceCommandLine] ServerService_Run %s", r ? "failed" : "succeed");
|
||||
Mprintf("[HandleServiceCommandLine] ServerService_Run %s\n", r ? "failed" : "succeed");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// -install: 安装服务
|
||||
if (cmdLine.Find(_T("-install")) != -1) {
|
||||
BOOL r = ServerService_Install();
|
||||
Mprintf("[HandleServiceCommandLine] ServerService_Install %s", !r ? "failed" : "succeed");
|
||||
Mprintf("[HandleServiceCommandLine] ServerService_Install %s\n", !r ? "failed" : "succeed");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// -uninstall: 卸载服务
|
||||
if (cmdLine.Find(_T("-uninstall")) != -1) {
|
||||
BOOL r = ServerService_Uninstall();
|
||||
Mprintf("[HandleServiceCommandLine] ServerService_Uninstall %s", !r ? "failed" : "succeed");
|
||||
Mprintf("[HandleServiceCommandLine] ServerService_Uninstall %s\n", !r ? "failed" : "succeed");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ static BOOL HandleServiceCommandLine()
|
||||
// 此模式下正常运行GUI,但使用不同的互斥量名称避免冲突
|
||||
if (cmdLine.Find(_T("-agent")) != -1) {
|
||||
// 继续正常启动GUI,但标记为代理模式
|
||||
Mprintf("[HandleServiceCommandLine] Run service agent: '%s'", cmdLine.GetString());
|
||||
Mprintf("[HandleServiceCommandLine] Run service agent: '%s'\n", cmdLine.GetString());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ static BOOL HandleServiceCommandLine()
|
||||
BOOL running = FALSE;
|
||||
char servicePath[MAX_PATH] = { 0 };
|
||||
BOOL r = ServerService_CheckStatus(®istered, &running, servicePath, MAX_PATH);
|
||||
Mprintf("[HandleServiceCommandLine] ServerService_CheckStatus %s", !r ? "failed" : "succeed");
|
||||
Mprintf("[HandleServiceCommandLine] ServerService_CheckStatus %s\n", !r ? "failed" : "succeed");
|
||||
|
||||
char curPath[MAX_PATH];
|
||||
GetModuleFileNameA(NULL, curPath, MAX_PATH);
|
||||
@@ -242,13 +242,13 @@ BOOL CMy2015RemoteApp::InitInstance()
|
||||
char curFile[MAX_PATH] = { 0 };
|
||||
GetModuleFileNameA(NULL, curFile, MAX_PATH);
|
||||
if (!IsRunningAsAdmin() && LaunchAsAdmin(curFile, "runas")) {
|
||||
Mprintf("[InitInstance] 程序没有管理员权限,用户选择以管理员身份重新运行。");
|
||||
Mprintf("[InitInstance] 程序没有管理员权限,用户选择以管理员身份重新运行。\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// 首先处理服务命令行参数
|
||||
if (HandleServiceCommandLine()) {
|
||||
Mprintf("[InitInstance] 服务命令已处理,退出。");
|
||||
Mprintf("[InitInstance] 服务命令已处理,退出。\n");
|
||||
return FALSE; // 服务命令已处理,退出
|
||||
}
|
||||
|
||||
@@ -268,7 +268,7 @@ BOOL CMy2015RemoteApp::InitInstance()
|
||||
}
|
||||
#endif
|
||||
|
||||
Mprintf("[InitInstance] 主控程序启动运行。");
|
||||
Mprintf("[InitInstance] 主控程序启动运行。\n");
|
||||
SetUnhandledExceptionFilter(&whenbuged);
|
||||
|
||||
// 创建并显示启动画面
|
||||
@@ -349,10 +349,11 @@ int CMy2015RemoteApp::ExitInstance()
|
||||
// 只有在代理模式退出时才停止服务
|
||||
if (IsAgentMode()) {
|
||||
ServerService_Stop();
|
||||
Mprintf("[InitInstance] 主控程序为代理模式,停止服务。");
|
||||
Mprintf("[InitInstance] 主控程序为代理模式,停止服务。\n");
|
||||
}
|
||||
|
||||
Mprintf("[InitInstance] 主控程序退出运行。");
|
||||
Mprintf("[InitInstance] 主控程序退出运行。\n");
|
||||
Sleep(1000);
|
||||
|
||||
return CWinApp::ExitInstance();
|
||||
}
|
||||
|
||||
@@ -3809,53 +3809,56 @@ void CMy2015RemoteDlg::OnDestroy()
|
||||
|
||||
CString GetClipboardText()
|
||||
{
|
||||
if (!OpenClipboard(nullptr)) return _T("");
|
||||
if (!OpenClipboard(nullptr)) return _T("");
|
||||
|
||||
CString strText;
|
||||
|
||||
// 优先获取 Unicode 格式
|
||||
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
|
||||
if (hData) {
|
||||
wchar_t* pszText = static_cast<wchar_t*>(GlobalLock(hData));
|
||||
if (pszText) {
|
||||
#ifdef UNICODE
|
||||
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
|
||||
strText = pszText;
|
||||
#else
|
||||
HANDLE hData = GetClipboardData(CF_TEXT);
|
||||
// 将 Unicode 转换为多字节(使用系统默认代码页)
|
||||
int len = WideCharToMultiByte(CP_ACP, 0, pszText, -1, nullptr, 0, nullptr, nullptr);
|
||||
if (len > 0) {
|
||||
char* pBuf = strText.GetBuffer(len);
|
||||
WideCharToMultiByte(CP_ACP, 0, pszText, -1, pBuf, len, nullptr, nullptr);
|
||||
strText.ReleaseBuffer();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
GlobalUnlock(hData);
|
||||
}
|
||||
|
||||
if (!hData) {
|
||||
CloseClipboard();
|
||||
return _T("");
|
||||
}
|
||||
|
||||
#ifdef UNICODE
|
||||
wchar_t* pszText = static_cast<wchar_t*>(GlobalLock(hData));
|
||||
#else
|
||||
char* pszText = static_cast<char*>(GlobalLock(hData));
|
||||
#endif
|
||||
|
||||
CString strText = pszText ? pszText : _T("");
|
||||
GlobalUnlock(hData);
|
||||
CloseClipboard();
|
||||
return strText;
|
||||
CloseClipboard();
|
||||
return strText;
|
||||
}
|
||||
|
||||
void SetClipboardText(const CString& text)
|
||||
void SetClipboardText(const char* utf8Text)
|
||||
{
|
||||
if (!OpenClipboard(nullptr)) return;
|
||||
EmptyClipboard();
|
||||
if (!OpenClipboard(nullptr)) return;
|
||||
EmptyClipboard();
|
||||
|
||||
#ifdef UNICODE
|
||||
HGLOBAL hGlob = GlobalAlloc(GMEM_MOVEABLE, (text.GetLength() + 1) * sizeof(wchar_t));
|
||||
wchar_t* p = static_cast<wchar_t*>(GlobalLock(hGlob));
|
||||
if (p) wcscpy_s(p, text.GetLength() + 1, text);
|
||||
#else
|
||||
HGLOBAL hGlob = GlobalAlloc(GMEM_MOVEABLE, (text.GetLength() + 1) * sizeof(char));
|
||||
char* p = static_cast<char*>(GlobalLock(hGlob));
|
||||
if (p) strcpy_s(p, text.GetLength() + 1, CT2A(text)); // CT2A 宏把 CString 转成 char*
|
||||
#endif
|
||||
|
||||
GlobalUnlock(hGlob);
|
||||
#ifdef UNICODE
|
||||
SetClipboardData(CF_UNICODETEXT, hGlob);
|
||||
#else
|
||||
SetClipboardData(CF_TEXT, hGlob);
|
||||
#endif
|
||||
CloseClipboard();
|
||||
// UTF-8 转 Unicode
|
||||
int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8Text, -1, nullptr, 0);
|
||||
if (wlen > 0) {
|
||||
HGLOBAL hGlob = GlobalAlloc(GMEM_MOVEABLE, wlen * sizeof(wchar_t));
|
||||
if (hGlob) {
|
||||
wchar_t* p = static_cast<wchar_t*>(GlobalLock(hGlob));
|
||||
if (p) {
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8Text, -1, p, wlen);
|
||||
GlobalUnlock(hGlob);
|
||||
SetClipboardData(CF_UNICODETEXT, hGlob);
|
||||
}
|
||||
else {
|
||||
GlobalFree(hGlob);
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseClipboard();
|
||||
}
|
||||
|
||||
CDialogBase* CMy2015RemoteDlg::GetRemoteWindow(HWND hWnd)
|
||||
@@ -3919,10 +3922,15 @@ LRESULT CALLBACK CMy2015RemoteDlg::LowLevelKeyboardProc(int nCode, WPARAM wParam
|
||||
} else {
|
||||
CString strText = GetClipboardText();
|
||||
if (!strText.IsEmpty()) {
|
||||
if (strText.GetLength() > 200*1024) {
|
||||
Mprintf("【Ctrl+V】 从本地拷贝文本到远程失败, 文本太长: %d \n", strText.GetLength());
|
||||
break;
|
||||
}
|
||||
BYTE* szBuffer = new BYTE[strText.GetLength() + 1];
|
||||
szBuffer[0] = COMMAND_SCREEN_SET_CLIPBOARD;
|
||||
memcpy(szBuffer + 1, strText.GetString(), strText.GetLength());
|
||||
dlg->m_ContextObject->Send2Client(szBuffer, strText.GetLength() + 1);
|
||||
if (dlg->m_ContextObject->Send2Client(szBuffer, strText.GetLength() + 1))
|
||||
Sleep(100);
|
||||
Mprintf("【Ctrl+V】 从本地拷贝文本到远程 \n");
|
||||
SAFE_DELETE_ARRAY(szBuffer);
|
||||
}
|
||||
@@ -3945,7 +3953,8 @@ LRESULT CALLBACK CMy2015RemoteDlg::LowLevelKeyboardProc(int nCode, WPARAM wParam
|
||||
EmptyClipboard();
|
||||
CloseClipboard();
|
||||
}
|
||||
g_2015RemoteDlg->m_pActiveSession->m_ContextObject->Send2Client(bToken, sizeof(bToken));
|
||||
if (g_2015RemoteDlg->m_pActiveSession->m_ContextObject->Send2Client(bToken, sizeof(bToken)))
|
||||
Sleep(200);
|
||||
Mprintf("【Ctrl+V】 从远程拷贝到本地 \n");
|
||||
} else {
|
||||
Mprintf("[Ctrl+V] 没有活动的远程桌面会话 \n");
|
||||
|
||||
@@ -605,7 +605,7 @@ BOOL IOCPServer::OnClientPostSending(CONTEXT_OBJECT* ContextObject,ULONG ulCompl
|
||||
int iOk = WSASend(ContextObject->sClientSocket, &ContextObject->wsaOutBuffer,1,
|
||||
NULL, ulFlags,&OverlappedPlus->m_ol, NULL);
|
||||
if ( iOk == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING ) {
|
||||
int a = GetLastError();
|
||||
int a = WSAGetLastError();
|
||||
Mprintf("!!! OnClientPostSending 投递消息失败: %d\n", a);
|
||||
RemoveStaleContext(ContextObject);
|
||||
SAFE_DELETE(OverlappedPlus);
|
||||
|
||||
@@ -797,22 +797,36 @@ BOOL CScreenSpyDlg::SaveSnapshot(void)
|
||||
}
|
||||
|
||||
|
||||
VOID CScreenSpyDlg::UpdateServerClipboard(char *szBuffer,ULONG ulLength)
|
||||
VOID CScreenSpyDlg::UpdateServerClipboard(char* szBuffer, ULONG ulLength)
|
||||
{
|
||||
if (!::OpenClipboard(NULL))
|
||||
return;
|
||||
if (!::OpenClipboard(NULL))
|
||||
return;
|
||||
|
||||
::EmptyClipboard();
|
||||
HGLOBAL hGlobal = GlobalAlloc(GPTR, ulLength);
|
||||
if (hGlobal != NULL) {
|
||||
::EmptyClipboard();
|
||||
|
||||
char* szClipboardVirtualAddress = (LPTSTR) GlobalLock(hGlobal);
|
||||
memcpy(szClipboardVirtualAddress,szBuffer,ulLength);
|
||||
GlobalUnlock(hGlobal);
|
||||
if(NULL==SetClipboardData(CF_TEXT, hGlobal))
|
||||
GlobalFree(hGlobal);
|
||||
}
|
||||
CloseClipboard();
|
||||
// UTF-8 转 Unicode
|
||||
int wlen = MultiByteToWideChar(CP_UTF8, 0, szBuffer, ulLength, nullptr, 0);
|
||||
if (wlen > 0) {
|
||||
// 分配 Unicode 缓冲区(+1 确保 null 结尾)
|
||||
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, (wlen + 1) * sizeof(wchar_t));
|
||||
if (hGlobal != NULL) {
|
||||
wchar_t* pWideStr = (wchar_t*)GlobalLock(hGlobal);
|
||||
if (pWideStr) {
|
||||
MultiByteToWideChar(CP_UTF8, 0, szBuffer, ulLength, pWideStr, wlen);
|
||||
pWideStr[wlen] = L'\0'; // 确保 null 结尾
|
||||
GlobalUnlock(hGlobal);
|
||||
|
||||
if (SetClipboardData(CF_UNICODETEXT, hGlobal) == NULL) {
|
||||
GlobalFree(hGlobal);
|
||||
}
|
||||
}
|
||||
else {
|
||||
GlobalFree(hGlobal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CloseClipboard();
|
||||
}
|
||||
|
||||
VOID CScreenSpyDlg::SendServerClipboard(void)
|
||||
|
||||
Reference in New Issue
Block a user