Case 1:
int i;
int* pi = &i;
Case 2:
int i;
IntPtr pi = &i;
Are both cases identical?
My purpose is that:-
On VB6 app:-
More Code:
string aString = text;
byte[] theBytes = System.Text.Encoding.Default.GetBytes(aString);
// Marshal the managed struct to a native block of memory.
int myStructSize = theBytes.Length;
IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize); //int* or IntPtr is good?
try
{
Marshal.Copy(theBytes, 0, pMyStruct, myStructSize);
...............
According to the msdn page:-
The IntPtr type can be used by languages that support pointers, and as a common means of referring to data between languages that do and do not support pointers.
IntPtr objects can also be used to hold handles. For example, instances of IntPtr are used extensively in the System.IO.FileStream class to hold file handles.
Are both cases indentical?
No, they are not identical,
The difference is in the underlying implementation of IntPtr
forex
int i = 10 ;
int *pi = i ;
int c = *pi ; // would work
but to do the same thing , you have to cast IntPtr
to int*
int i =10 ;
IntPtr pi = &i ;
int *tempPtr = (int*)pi ;
int c = *tempPtr ;
The internal representation of IntPtr
is like void*
but it is exposed like an integer. You can use it whenever you need to store an unmanaged pointer and don't want to use unsafe code.
It has two limitations:
void*
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With