Anonymous
Inject use python/c++ hat ein anderes Ergebnis
Post
by Anonymous » 02 Dec 2025, 10:58
Ich verwende die Python-Inject-DLL in eine dritte Software, um einen ersetzten Text zu erhalten, aber ich bekomme einen chaotischen
Chaotischen Code
hook.cpp
Code: Select all
#include
#include
#include
#include // 用于 strncpy
HHOOK g_hHook = NULL;
// 辅助函数:窄字符串(std::string) 转 宽字符串(std::wstring)(适配系统默认编码)
std::wstring MultiByteToWideString(const std::string& str)
{
if (str.empty()) return L"";
// 获取转换所需的宽字符长度(CP_ACP=系统默认ANSI编码)
int wideLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
if (wideLen == 0) return L"";
// 分配缓冲区并执行转换
std::wstring wideStr(wideLen, 0);
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wideStr[0], wideLen);
return wideStr;
}
std::string GetMessageText(WPARAM wParam, LPARAM lParam)
{
std::string text;
if (lParam)
{
char* pText = (char*)lParam;
if (pText)
{
const size_t maxLength = 1024;
char buffer[maxLength + 1] = {0};
strncpy(buffer, pText, maxLength); // 限制最大长度,避免缓冲区溢出
text = buffer;
}
}
return text;
}
extern "C" {
__declspec(dllexport) LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
CWPSTRUCT* pMsg = (CWPSTRUCT*)lParam;
if (pMsg->message == EM_REPLACESEL)
{
std::string replacedText = GetMessageText(pMsg->wParam, pMsg->lParam);
if (!replacedText.empty())
{
// 1. 窄字符串转宽字符串(适配 MessageBoxW)
std::wstring replacedTextWide = MultiByteToWideString(replacedText);
// 2. 拼接宽字符串消息(全程用 std::wstring + L"" 宽常量)
std::wstring message = L"检测到RichEdit文本替换操作:\n\n";
message += L"窗口句柄: " + std::to_wstring((intptr_t)pMsg->hwnd) + L"\n";
message += L"替换文本: " + replacedTextWide + L"\n";
message += L"文本长度: " + std::to_wstring(replacedTextWide.length()) + L" 字符";
// 3. 调用宽字符版本 MessageBoxW(参数均为宽字符串)
MessageBoxW(
NULL,
message.c_str(), // std::wstring::c_str() 返回 LPCWSTR
L"RichEdit监控", // 宽字符串标题(无乱码)
MB_OK | MB_ICONINFORMATION
);
}
}
}
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
__declspec(dllexport) void TestFunction() {
// 空函数用于测试导出机制
}
}
BOOL APIENTRY DllMain(HINSTANCE hinstDll, DWORD dwReason, LPVOID lpvRevered)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
MessageBox(NULL, TEXT("Process Load Dll Success!"), TEXT("Tips"), MB_OK);
break;
case DLL_PROCESS_DETACH:
// MessageBox(NULL, TEXT("Process Unload Dll Success!"), TEXT("Tips"), MB_OK);
break;
case DLL_THREAD_ATTACH:
// MessageBox(NULL, TEXT("Thread load Dll Success!"), TEXT("Tips"), MB_OK);
break;
case DLL_THREAD_DETACH:
// MessageBox(NULL, TEXT("Thread Unload Dll Success!"), TEXT("Tips"), MB_OK);
break;
}
return TRUE;
}
verwenden Sie c++ inject: Erhalten Sie eine richtige Nachricht im Popup-Dialogfeld „RichEdit监控“
inject.cpp
Code: Select all
#include
#include
#include
int main(int argc, char* argv[]) {
// 检查参数数量
if (argc != 2) {
std::cout
1764669533
Anonymous
Ich verwende die Python-Inject-DLL in eine dritte Software, um einen ersetzten Text zu erhalten, aber ich bekomme einen chaotischen [b]Chaotischen Code[/b] hook.cpp [code]#include #include #include #include // 用于 strncpy HHOOK g_hHook = NULL; // 辅助函数:窄字符串(std::string) 转 宽字符串(std::wstring)(适配系统默认编码) std::wstring MultiByteToWideString(const std::string& str) { if (str.empty()) return L""; // 获取转换所需的宽字符长度(CP_ACP=系统默认ANSI编码) int wideLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); if (wideLen == 0) return L""; // 分配缓冲区并执行转换 std::wstring wideStr(wideLen, 0); MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wideStr[0], wideLen); return wideStr; } std::string GetMessageText(WPARAM wParam, LPARAM lParam) { std::string text; if (lParam) { char* pText = (char*)lParam; if (pText) { const size_t maxLength = 1024; char buffer[maxLength + 1] = {0}; strncpy(buffer, pText, maxLength); // 限制最大长度,避免缓冲区溢出 text = buffer; } } return text; } extern "C" { __declspec(dllexport) LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { CWPSTRUCT* pMsg = (CWPSTRUCT*)lParam; if (pMsg->message == EM_REPLACESEL) { std::string replacedText = GetMessageText(pMsg->wParam, pMsg->lParam); if (!replacedText.empty()) { // 1. 窄字符串转宽字符串(适配 MessageBoxW) std::wstring replacedTextWide = MultiByteToWideString(replacedText); // 2. 拼接宽字符串消息(全程用 std::wstring + L"" 宽常量) std::wstring message = L"检测到RichEdit文本替换操作:\n\n"; message += L"窗口句柄: " + std::to_wstring((intptr_t)pMsg->hwnd) + L"\n"; message += L"替换文本: " + replacedTextWide + L"\n"; message += L"文本长度: " + std::to_wstring(replacedTextWide.length()) + L" 字符"; // 3. 调用宽字符版本 MessageBoxW(参数均为宽字符串) MessageBoxW( NULL, message.c_str(), // std::wstring::c_str() 返回 LPCWSTR L"RichEdit监控", // 宽字符串标题(无乱码) MB_OK | MB_ICONINFORMATION ); } } } return CallNextHookEx(g_hHook, nCode, wParam, lParam); } __declspec(dllexport) void TestFunction() { // 空函数用于测试导出机制 } } BOOL APIENTRY DllMain(HINSTANCE hinstDll, DWORD dwReason, LPVOID lpvRevered) { switch (dwReason) { case DLL_PROCESS_ATTACH: MessageBox(NULL, TEXT("Process Load Dll Success!"), TEXT("Tips"), MB_OK); break; case DLL_PROCESS_DETACH: // MessageBox(NULL, TEXT("Process Unload Dll Success!"), TEXT("Tips"), MB_OK); break; case DLL_THREAD_ATTACH: // MessageBox(NULL, TEXT("Thread load Dll Success!"), TEXT("Tips"), MB_OK); break; case DLL_THREAD_DETACH: // MessageBox(NULL, TEXT("Thread Unload Dll Success!"), TEXT("Tips"), MB_OK); break; } return TRUE; } [/code] verwenden Sie c++ inject: Erhalten Sie eine richtige Nachricht im Popup-Dialogfeld „RichEdit监控“ inject.cpp [code]#include #include #include int main(int argc, char* argv[]) { // 检查参数数量 if (argc != 2) { std::cout