我在將字符串傳遞到外部 DLL 中的某個(gè)函數(shù)時(shí)遇到問題。我會(huì)發(fā)布一個(gè)實(shí)際的代碼片段,但它有點(diǎn)混亂并且可能難以閱讀。以下片段是我的個(gè)人代碼的概要。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);引用代碼片段,出現(xiàn)的問題是 FindProgramProcessId 將成功傳遞一個(gè)字符串,沒有問題,但 InjectIntoProcess 將const char* Dll根據(jù)nullptr我在該方法中放入的“額外”代碼顯示。請(qǐng)注意,我嘗試傳遞 anIntPtr代替string和 using Marshal.StringToHGlobalAnsi,但仍然遇到Dll == nullptr問題。它破壞了我的代碼。更多相同信息可以在我的GuidedHacking線程中找到。
為什么 C# 編組字符串不能與 C++ DLL 一起使用
慕標(biāo)琳琳
2023-06-25 14:34:19