Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to convert char[] to System.IntPtr

how to convert char[] to System.IntPtr

Scheduled Pinned Locked Moved C#
questiondata-structureshelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hemant kulkarni
    wrote on last edited by
    #1

    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.

    G 1 Reply Last reply
    0
    • H Hemant kulkarni

      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.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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; }

      H 1 Reply Last reply
      0
      • G Guffa

        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; }

        H Offline
        H Offline
        Hemant kulkarni
        wrote on last edited by
        #3

        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.

        G 1 Reply Last reply
        0
        • H Hemant kulkarni

          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.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Look at the AllocCoTaskMem and AllocHGlobal methods in the Marshal class. In the documentation you will find some information on when to use which.

          --- b { font-weight: normal; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups