c# - Why does IntPtr.ToString() return a memory address rather than a string? -


refer code snippet below.

private void mymethod()  {     intptr myvar = marshal.allochglobal(1024);     string hello = "hello world";     marshal.copy(hello.tochararray(), 0, myvar, hello.length);      //i don't see hello world here. instead, see memory address. why???     debug.print(myvar.tostring());      marshal.freehglobal(myvar); } 

what doing wrong? expect see "hello world", don't.

of course you'll see memory address. copied string unmanaged memory, , printed out string representation of intptr object, memory address. pointer doesn't tell .net framework type expect - it's unmanaged data.

if want read data unmanaged memory, you'll need marshal data managed string.


Comments

Popular posts from this blog

hibernate - How to load global settings frequently used in application in Java -

python 3.x - Mapping specific letters onto a list of words -

objective c - Ownership modifiers with manual reference counting -