C# IntPtr vs ref string

HavocXphere

Honorary Master
Joined
Oct 19, 2007
Messages
33,153
Reaction score
1,297
Location
Europe
I'm trying to compile some code off MSDN its refusing to do so.

Basically the function requires a ref string while the value being used is a IntPtr. The compiler (VS Express '10) says it can't convert it.

// Call this function to remove the key from memory after use for security
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);

[...]

GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );

[...]

ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);

As I understand it ref string and IntPtr are roughly the same thing, but I don't want to nuke the wrong memory location so a clean & correct conversion would be best.

Thanks
HX
 
I am compiling it without any problems here.

Have you modified it in any way?
 

Not sure where you copy pasted from...

Yours:

public static extern bool ZeroMemory(ref string Destination, int Length);

Theirs (from the link you pasted):

public static extern bool ZeroMemory(IntPtr Destination, int Length);


EDIT: I see where you went wrong. You were being a good programmer and followed the whole document, building your source as you went along. Be a bad programmer and jump straight to the section called "Complete code listing" and copy that code... it's what compiles for me (uses IntPtr and not ref string):P
 
Last edited:
EDIT: I see where you went wrong. You were being a good programmer and followed the whole document, building your source as you went along. Be a bad programmer and jump straight to the section called "Complete code listing" and copy that code... it's what compiles for me (uses IntPtr and not ref string):P
Ah sneaky.

Compiles perfectly now. Thanks shogun!
 
Top
Sign up to the MyBroadband newsletter
X