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

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -