我在将字符串传递给外部 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)
#endif
extern "C" DllExport DWORD FindProgramProcessId(const char* procName);
extern "C" DllExport bool InjectIntoProcess(DWORD procId, const char* Dll);
引用片段,所产生的问题是,FindProgramProcessId将成功地传递一个字符串,没有problemo,但InjectIntoProcess将显示const char* Dll
为nullptr
根据“额外”的代码,我把这种方法。
请注意,我已尝试通过IntPtr
代替string
并使用Marshal.StringToHGlobalAnsi
,但我仍然遇到Dll == nullptr
问题。它破坏了我的代码。更多相同的信息可以在我的GuidedHacking线程中找到。
Win32DWORD
是一个 32 位整数,而 C#ulong
是一个 64 位整数。混淆源于它DWORD
是 的别名unsigned long
,但 C++long
不一定是 64 位(实际上,在 MSVC 中,它是 32 位;unsigned long long
是 64 位无符号整数)。
由于您使用的是 cdecl 调用约定,调用者负责清理堆栈(因此不会发生崩溃),并且参数从右到左传递(因此Dll
最终指向传递给 的值中间的某个位置procId
,即可能包含零)。或者至少这是我的猜测,因为我们在这里处于未定义的行为领域。
您应该声明as的返回值FindProgramProcessId
和procId
参数。InjectIntoProcess
uint
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句