Return function int64 C++ to a C# project









up vote
0
down vote

favorite












i write a dll that is injected on game and return my localplayer and listArrayplayer on server. Ok work fine
code dll project:
C++ code:



__int64 RerturnLocalPlayer() 

__int64 player = GetLocalPlayer_EX();// __Int64 GetLocalPlayer_EX() is a function that return type __int64 value
return player;



in main.h:



extern "C" 

__declspec(dllexport) __int64 RerturnLocalPlayer();




mt function



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# Code:



[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"));


the problem is when i run my c# application my console open and after 3 seconds close and sometimes get error: **Attempt to read or write to protected memory. Usually, this is an indication that another memory is damaged.



can some one try help me?










share|improve this question























  • NativeMemory.Read<Int64> interprets the return value of your function as pointer to an int64 value and reads the value it points to. If this really the case?
    – Klaus Gütter
    Nov 10 at 17:08






  • 1




    Note that if your native library is compiled as x86 (32-bit) code, you might have a mismatching calling convention (your P/Invoke declares stdcall calling convention, but your compiled C++ function exports quite possibly use cdecl calling convention.) Suggested recommendation to fix/avoid such situations: state the calling convention for your exported C/C++ functions explicitly in main.h, and then use the same calling convention in your P/Invoke declaration.
    – elgonzo
    Nov 10 at 17:25











  • Klaus Gütter yes is a function to read int64, but i dont need this function.
    – CerraossoUC
    Nov 10 at 18:13














up vote
0
down vote

favorite












i write a dll that is injected on game and return my localplayer and listArrayplayer on server. Ok work fine
code dll project:
C++ code:



__int64 RerturnLocalPlayer() 

__int64 player = GetLocalPlayer_EX();// __Int64 GetLocalPlayer_EX() is a function that return type __int64 value
return player;



in main.h:



extern "C" 

__declspec(dllexport) __int64 RerturnLocalPlayer();




mt function



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# Code:



[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"));


the problem is when i run my c# application my console open and after 3 seconds close and sometimes get error: **Attempt to read or write to protected memory. Usually, this is an indication that another memory is damaged.



can some one try help me?










share|improve this question























  • NativeMemory.Read<Int64> interprets the return value of your function as pointer to an int64 value and reads the value it points to. If this really the case?
    – Klaus Gütter
    Nov 10 at 17:08






  • 1




    Note that if your native library is compiled as x86 (32-bit) code, you might have a mismatching calling convention (your P/Invoke declares stdcall calling convention, but your compiled C++ function exports quite possibly use cdecl calling convention.) Suggested recommendation to fix/avoid such situations: state the calling convention for your exported C/C++ functions explicitly in main.h, and then use the same calling convention in your P/Invoke declaration.
    – elgonzo
    Nov 10 at 17:25











  • Klaus Gütter yes is a function to read int64, but i dont need this function.
    – CerraossoUC
    Nov 10 at 18:13












up vote
0
down vote

favorite









up vote
0
down vote

favorite











i write a dll that is injected on game and return my localplayer and listArrayplayer on server. Ok work fine
code dll project:
C++ code:



__int64 RerturnLocalPlayer() 

__int64 player = GetLocalPlayer_EX();// __Int64 GetLocalPlayer_EX() is a function that return type __int64 value
return player;



in main.h:



extern "C" 

__declspec(dllexport) __int64 RerturnLocalPlayer();




mt function



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# Code:



[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"));


the problem is when i run my c# application my console open and after 3 seconds close and sometimes get error: **Attempt to read or write to protected memory. Usually, this is an indication that another memory is damaged.



can some one try help me?










share|improve this question















i write a dll that is injected on game and return my localplayer and listArrayplayer on server. Ok work fine
code dll project:
C++ code:



__int64 RerturnLocalPlayer() 

__int64 player = GetLocalPlayer_EX();// __Int64 GetLocalPlayer_EX() is a function that return type __int64 value
return player;



in main.h:



extern "C" 

__declspec(dllexport) __int64 RerturnLocalPlayer();




mt function



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# Code:



[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"));


the problem is when i run my c# application my console open and after 3 seconds close and sometimes get error: **Attempt to read or write to protected memory. Usually, this is an indication that another memory is damaged.



can some one try help me?







c# c++ c++11 visual-c++ c#-4.0






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:59

























asked Nov 10 at 16:56









CerraossoUC

134




134











  • NativeMemory.Read<Int64> interprets the return value of your function as pointer to an int64 value and reads the value it points to. If this really the case?
    – Klaus Gütter
    Nov 10 at 17:08






  • 1




    Note that if your native library is compiled as x86 (32-bit) code, you might have a mismatching calling convention (your P/Invoke declares stdcall calling convention, but your compiled C++ function exports quite possibly use cdecl calling convention.) Suggested recommendation to fix/avoid such situations: state the calling convention for your exported C/C++ functions explicitly in main.h, and then use the same calling convention in your P/Invoke declaration.
    – elgonzo
    Nov 10 at 17:25











  • Klaus Gütter yes is a function to read int64, but i dont need this function.
    – CerraossoUC
    Nov 10 at 18:13
















  • NativeMemory.Read<Int64> interprets the return value of your function as pointer to an int64 value and reads the value it points to. If this really the case?
    – Klaus Gütter
    Nov 10 at 17:08






  • 1




    Note that if your native library is compiled as x86 (32-bit) code, you might have a mismatching calling convention (your P/Invoke declares stdcall calling convention, but your compiled C++ function exports quite possibly use cdecl calling convention.) Suggested recommendation to fix/avoid such situations: state the calling convention for your exported C/C++ functions explicitly in main.h, and then use the same calling convention in your P/Invoke declaration.
    – elgonzo
    Nov 10 at 17:25











  • Klaus Gütter yes is a function to read int64, but i dont need this function.
    – CerraossoUC
    Nov 10 at 18:13















NativeMemory.Read<Int64> interprets the return value of your function as pointer to an int64 value and reads the value it points to. If this really the case?
– Klaus Gütter
Nov 10 at 17:08




NativeMemory.Read<Int64> interprets the return value of your function as pointer to an int64 value and reads the value it points to. If this really the case?
– Klaus Gütter
Nov 10 at 17:08




1




1




Note that if your native library is compiled as x86 (32-bit) code, you might have a mismatching calling convention (your P/Invoke declares stdcall calling convention, but your compiled C++ function exports quite possibly use cdecl calling convention.) Suggested recommendation to fix/avoid such situations: state the calling convention for your exported C/C++ functions explicitly in main.h, and then use the same calling convention in your P/Invoke declaration.
– elgonzo
Nov 10 at 17:25





Note that if your native library is compiled as x86 (32-bit) code, you might have a mismatching calling convention (your P/Invoke declares stdcall calling convention, but your compiled C++ function exports quite possibly use cdecl calling convention.) Suggested recommendation to fix/avoid such situations: state the calling convention for your exported C/C++ functions explicitly in main.h, and then use the same calling convention in your P/Invoke declaration.
– elgonzo
Nov 10 at 17:25













Klaus Gütter yes is a function to read int64, but i dont need this function.
– CerraossoUC
Nov 10 at 18:13




Klaus Gütter yes is a function to read int64, but i dont need this function.
– CerraossoUC
Nov 10 at 18:13












1 Answer
1






active

oldest

votes

















up vote
0
down vote













__int64 RerturnLocalPlayer() is a function that returns a 64-bit value through some calling convention. You seem to think it's stdcall, I think you're wrong. However I don't have your source code so you can bang your head against that wall on your own.



public static extern Int64 RerturnLocalPlayer(); is the right definition (again, calling convention aside, there's no 64-bit register on the 32-bit CPU, so it won't be stdcall).



However, Int64 localp = NativeMemory.Read<Int64> (RerturnLocalPlayer()); is just plain bonkers. You're already getting your integer straight from the marshaller as a return value, use it as such! This would be the correct way of calling your function:



Int64 localp = RerturnLocalPlayer();





share|improve this answer




















  • i update and add my functionon description
    – CerraossoUC
    Nov 11 at 19:13










  • @CerraossoUC, ok and? Is it still a problem? Have you tried my suggestion? I still see the same native memory read call in your updated code.
    – Blindy
    Nov 12 at 16:09










  • yes, i forget to remove nativememory from description, but i tried Int64 localp = RerturnLocalPlayer(); but have same problem nothing happend and close my app. and yes, i change from StdCall) to CallingConvention.Cdecl). =/
    – CerraossoUC
    Nov 13 at 11:18










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241237%2freturn-function-int64-c-to-a-c-sharp-project%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













__int64 RerturnLocalPlayer() is a function that returns a 64-bit value through some calling convention. You seem to think it's stdcall, I think you're wrong. However I don't have your source code so you can bang your head against that wall on your own.



public static extern Int64 RerturnLocalPlayer(); is the right definition (again, calling convention aside, there's no 64-bit register on the 32-bit CPU, so it won't be stdcall).



However, Int64 localp = NativeMemory.Read<Int64> (RerturnLocalPlayer()); is just plain bonkers. You're already getting your integer straight from the marshaller as a return value, use it as such! This would be the correct way of calling your function:



Int64 localp = RerturnLocalPlayer();





share|improve this answer




















  • i update and add my functionon description
    – CerraossoUC
    Nov 11 at 19:13










  • @CerraossoUC, ok and? Is it still a problem? Have you tried my suggestion? I still see the same native memory read call in your updated code.
    – Blindy
    Nov 12 at 16:09










  • yes, i forget to remove nativememory from description, but i tried Int64 localp = RerturnLocalPlayer(); but have same problem nothing happend and close my app. and yes, i change from StdCall) to CallingConvention.Cdecl). =/
    – CerraossoUC
    Nov 13 at 11:18














up vote
0
down vote













__int64 RerturnLocalPlayer() is a function that returns a 64-bit value through some calling convention. You seem to think it's stdcall, I think you're wrong. However I don't have your source code so you can bang your head against that wall on your own.



public static extern Int64 RerturnLocalPlayer(); is the right definition (again, calling convention aside, there's no 64-bit register on the 32-bit CPU, so it won't be stdcall).



However, Int64 localp = NativeMemory.Read<Int64> (RerturnLocalPlayer()); is just plain bonkers. You're already getting your integer straight from the marshaller as a return value, use it as such! This would be the correct way of calling your function:



Int64 localp = RerturnLocalPlayer();





share|improve this answer




















  • i update and add my functionon description
    – CerraossoUC
    Nov 11 at 19:13










  • @CerraossoUC, ok and? Is it still a problem? Have you tried my suggestion? I still see the same native memory read call in your updated code.
    – Blindy
    Nov 12 at 16:09










  • yes, i forget to remove nativememory from description, but i tried Int64 localp = RerturnLocalPlayer(); but have same problem nothing happend and close my app. and yes, i change from StdCall) to CallingConvention.Cdecl). =/
    – CerraossoUC
    Nov 13 at 11:18












up vote
0
down vote










up vote
0
down vote









__int64 RerturnLocalPlayer() is a function that returns a 64-bit value through some calling convention. You seem to think it's stdcall, I think you're wrong. However I don't have your source code so you can bang your head against that wall on your own.



public static extern Int64 RerturnLocalPlayer(); is the right definition (again, calling convention aside, there's no 64-bit register on the 32-bit CPU, so it won't be stdcall).



However, Int64 localp = NativeMemory.Read<Int64> (RerturnLocalPlayer()); is just plain bonkers. You're already getting your integer straight from the marshaller as a return value, use it as such! This would be the correct way of calling your function:



Int64 localp = RerturnLocalPlayer();





share|improve this answer












__int64 RerturnLocalPlayer() is a function that returns a 64-bit value through some calling convention. You seem to think it's stdcall, I think you're wrong. However I don't have your source code so you can bang your head against that wall on your own.



public static extern Int64 RerturnLocalPlayer(); is the right definition (again, calling convention aside, there's no 64-bit register on the 32-bit CPU, so it won't be stdcall).



However, Int64 localp = NativeMemory.Read<Int64> (RerturnLocalPlayer()); is just plain bonkers. You're already getting your integer straight from the marshaller as a return value, use it as such! This would be the correct way of calling your function:



Int64 localp = RerturnLocalPlayer();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 20:14









Blindy

43.2k765107




43.2k765107











  • i update and add my functionon description
    – CerraossoUC
    Nov 11 at 19:13










  • @CerraossoUC, ok and? Is it still a problem? Have you tried my suggestion? I still see the same native memory read call in your updated code.
    – Blindy
    Nov 12 at 16:09










  • yes, i forget to remove nativememory from description, but i tried Int64 localp = RerturnLocalPlayer(); but have same problem nothing happend and close my app. and yes, i change from StdCall) to CallingConvention.Cdecl). =/
    – CerraossoUC
    Nov 13 at 11:18
















  • i update and add my functionon description
    – CerraossoUC
    Nov 11 at 19:13










  • @CerraossoUC, ok and? Is it still a problem? Have you tried my suggestion? I still see the same native memory read call in your updated code.
    – Blindy
    Nov 12 at 16:09










  • yes, i forget to remove nativememory from description, but i tried Int64 localp = RerturnLocalPlayer(); but have same problem nothing happend and close my app. and yes, i change from StdCall) to CallingConvention.Cdecl). =/
    – CerraossoUC
    Nov 13 at 11:18















i update and add my functionon description
– CerraossoUC
Nov 11 at 19:13




i update and add my functionon description
– CerraossoUC
Nov 11 at 19:13












@CerraossoUC, ok and? Is it still a problem? Have you tried my suggestion? I still see the same native memory read call in your updated code.
– Blindy
Nov 12 at 16:09




@CerraossoUC, ok and? Is it still a problem? Have you tried my suggestion? I still see the same native memory read call in your updated code.
– Blindy
Nov 12 at 16:09












yes, i forget to remove nativememory from description, but i tried Int64 localp = RerturnLocalPlayer(); but have same problem nothing happend and close my app. and yes, i change from StdCall) to CallingConvention.Cdecl). =/
– CerraossoUC
Nov 13 at 11:18




yes, i forget to remove nativememory from description, but i tried Int64 localp = RerturnLocalPlayer(); but have same problem nothing happend and close my app. and yes, i change from StdCall) to CallingConvention.Cdecl). =/
– CerraossoUC
Nov 13 at 11:18

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241237%2freturn-function-int64-c-to-a-c-sharp-project%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3