我在将字符串传递到外部 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 将成功传递一个字符串,没有问题,但 InjectIntoProcess 将const char* Dll根据nullptr我在该方法中放入的“额外”代码显示。
请注意,我尝试传递 anIntPtr代替string和 using Marshal.StringToHGlobalAnsi,但仍然遇到Dll == nullptr问题。它破坏了我的代码。更多相同信息可以在我的GuidedHacking线程中找到。
慕田峪4524236
相关分类