[英]Return function int64 C++ to a C# project
我寫了一個注入游戲的dll,並在服務器上返回了我的localplayer和listArrayplayer。 好的工作良好的代碼dll項目:C ++代碼:
__int64 RerturnLocalPlayer() {
__int64 player = GetLocalPlayer_EX();// __Int64 GetLocalPlayer_EX() is a function that return type __int64 value
return player;
}
在main.h中:
extern "C" {
__declspec(dllexport) __int64 RerturnLocalPlayer();
}
mt功能
extern "C" {
__declspec(dllexport) __int64 GetLocalPlayer_EX()
{
DWORD64 pClientGameContext = *(DWORD64*)OFFSET_CLIENTGAMECONTEXT;
if (!(pClientGameContext)) return 0;
DWORD64 pPlayerManager = *(DWORD64*)(pClientGameContext + 0x68);
if (!(pPlayerManager)) return 0;
DWORD64 pObfuscationMgr = *(DWORD64*)OFFSET_ObfuscationMgr;
if (!(pObfuscationMgr)) return 0;
DWORD64 LocalPlayerListXorValue = *(DWORD64*)((DWORD64)pPlayerManager + 0xF0);
DWORD64 LocalPlayerListKey = LocalPlayerListXorValue ^ *(DWORD64 *)(pObfuscationMgr + 0x70);
hashtable<DWORD64>* table = (hashtable<DWORD64>*)(pObfuscationMgr + 8);
hashtable_iterator<DWORD64> iterator = { 0 };
hashtable_find(table, &iterator, LocalPlayerListKey);
if (iterator.mpNode == table->mpBucketArray[table->mnBucketCount])
return 0;
DWORD64 EncryptedPlayerMgr = (DWORD64)iterator.mpNode->mValue.second;
if (!(EncryptedPlayerMgr)) return 0;
DWORD MaxPlayerCount = *(DWORD *)(EncryptedPlayerMgr + 0x18);
if (MaxPlayerCount != 1) return 0;
return EncryptedPlayerMgr__GetPlayer(EncryptedPlayerMgr, 0);
}
}
C#代碼:
[System.Runtime.InteropServices.DllImportAttribute("BFClient1.dll", EntryPoint = "RerturnLocalPlayer",
CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static extern Int64 RerturnLocalPlayer();
Int64 localp = NativeMemory.Read<Int64> (RerturnLocalPlayer());
Console.WriteLine("LocalPlayer " + localp.ToString("X"));
問題是當我運行我的C#應用程序時,我的控制台打開,並且在3秒鍾后關閉,有時會出現錯誤:**嘗試讀取或寫入受保護的內存。 通常,這表明另一個內存已損壞。
有人可以幫我嗎?
__int64 RerturnLocalPlayer()
是一個通過某些調用約定返回64位值的函數。 您似乎認為這是stdcall
,我認為您錯了。 但是,我沒有您的源代碼,因此您可以自己動腦子。
public static extern Int64 RerturnLocalPlayer();
是正確的定義(再次,調用約定,在32位CPU上沒有64位寄存器,因此不會是stdcall
)。
但是, Int64 localp = NativeMemory.Read<Int64> (RerturnLocalPlayer());
簡直就是傻瓜。 您已經從marshaller中直接獲取了整數作為返回值,就這樣使用它! 這將是調用函數的正確方法:
Int64 localp = RerturnLocalPlayer();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.