为了账号安全,请及时绑定邮箱和手机立即绑定

为什么 C# 编组字符串不能与 C++ DLL 一起使用

为什么 C# 编组字符串不能与 C++ DLL 一起使用

C#
慕标琳琳 2023-06-25 14:34:19
我在将字符串传递到外部 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的参数。InjectIntoProcessuint


查看完整回答
反对 回复 2023-06-25
  • 1 回答
  • 0 关注
  • 137 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信