how to convert char[] to System.IntPtr
-
Hi, I want to convert a char array to System.IntPtr. How can I do this? I written the following code wich gives error as cannot copy to null value string metadatapath2 = "C:\\BigMetadata.txt"; StreamReader myStreamReader1 = new StreamReader(metadatapath2); string bigdata = myStreamReader1.ReadToEnd(); char [] dataarray = bigdata.ToCharArray(); System.IntPtr myDataptr = new System.IntPtr(); Marshal.Copy(dataarray,0,myDataptr,dataarray.Length*2); Thanks in advance Regards, Hemant.
-
Hi, I want to convert a char array to System.IntPtr. How can I do this? I written the following code wich gives error as cannot copy to null value string metadatapath2 = "C:\\BigMetadata.txt"; StreamReader myStreamReader1 = new StreamReader(metadatapath2); string bigdata = myStreamReader1.ReadToEnd(); char [] dataarray = bigdata.ToCharArray(); System.IntPtr myDataptr = new System.IntPtr(); Marshal.Copy(dataarray,0,myDataptr,dataarray.Length*2); Thanks in advance Regards, Hemant.
You can't convert a char array to an IntPtr. An IntPtr is just a pointer in a form that can be handled by managed code. What your code is currently doing is trying to copy the data from the array to the memory area that the pointer is pointing to. As you haven't allocated any memory area and made the IntPtr point to it, that is impossible. What you have to do is to get a pinned memory area, either by allocating unmanaged memory and copy the data to it, or by pinning the dataarray, and make the IntPtr point to the memory area.
--- b { font-weight: normal; }
-
You can't convert a char array to an IntPtr. An IntPtr is just a pointer in a form that can be handled by managed code. What your code is currently doing is trying to copy the data from the array to the memory area that the pointer is pointing to. As you haven't allocated any memory area and made the IntPtr point to it, that is impossible. What you have to do is to get a pinned memory area, either by allocating unmanaged memory and copy the data to it, or by pinning the dataarray, and make the IntPtr point to the memory area.
--- b { font-weight: normal; }
Hi, Thanks for urgent reply. How can I allocate a memry and get some data copy to that memory ad get its pointer back? Regards, Hemant.
-
Hi, Thanks for urgent reply. How can I allocate a memry and get some data copy to that memory ad get its pointer back? Regards, Hemant.