我在將字符串傳遞到外部 DLL 中的某個函數時遇到問題。我會發布一個實際的代碼片段,但它有點混亂并且可能難以閱讀。以下片段是我的個人代碼的概要。C# 文件(UNICODE)[DllImport("InjectDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]private static extern ulong FindProgramProcessId(string procName);[DllImport("InjectDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]private static extern bool InjectIntoProcess(ulong procId, string Dll);string ProcName = "random_test_game.exe";string DllPath = @"C:\ProgramData\Hack\File.dll";ulong procId = FindProgramProcessId(ProcName);bool Injected = InjectIntoProcess(procId, DllPath);C++ 文件 (ANSI)DllExport DWORD FindProgramProcessId(const char* procName){ ...}DllExport bool InjectIntoProcess(DWORD procId, const char* Dll){ if (Dll == nullptr) { MessageBox(NULL, "DLL", "EMPTY", MB_OK); return false; } ...}C++ 頭文件#pragma once#include <Windows.h>#include <TlHelp32.h>#include <string>#ifdef EXPORT#define DllExport __declspec(dllexport)#else#define DllExport __declspec(dllimport)#endifextern "C" DllExport DWORD FindProgramProcessId(const char* procName);extern "C" DllExport bool InjectIntoProcess(DWORD procId, const char* Dll);引用代碼片段,出現的問題是 FindProgramProcessId 將成功傳遞一個字符串,沒有問題,但 InjectIntoProcess 將const char* Dll根據nullptr我在該方法中放入的“額外”代碼顯示。請注意,我嘗試傳遞 anIntPtr代替string和 using Marshal.StringToHGlobalAnsi,但仍然遇到Dll == nullptr問題。它破壞了我的代碼。更多相同信息可以在我的GuidedHacking線程中找到。
1 回答

慕田峪4524236
TA貢獻1875條經驗 獲得超5個贊
Win32DWORD
是32位整數,而C#ulong
是64位整數。混亂源于以下事實:DWORD
是 的別名unsigned long
,但 C++long
不一定是 64 位(事實上,在 MSVC 中,它是 32 位;unsigned long long
是 64 位無符號整數)。
由于您使用的是 cdecl 調用約定,調用者負責清理堆棧(因此不會發生崩潰),并且參數從右到左傳遞(因此最終指向傳遞給 的Dll
值中間的某個位置procId
,這可能包含零)?;蛘咧辽龠@是我的猜測,因為我們處于未定義的行為領域。
您應該聲明 as 的返回值FindProgramProcessId
和asprocId
的參數。InjectIntoProcess
uint
- 1 回答
- 0 關注
- 157 瀏覽
添加回答
舉報
0/150
提交
取消