title: Python - ctypes.c_char.from_buffer vs ctypes.c_void_p(string)
body: <p>What would be the difference between:</p>
  
  <pre><code>pointer = ctypes.c_char_p('abc')
  </code></pre>
  
  <p>and </p>
  
  <pre><code>string = 'abc'
  buffer = (ctypes.c_char * len(string)).from_buffer(string)
  </code></pre>
  
  <p>Technically both are pointers when passed in their respective function calls like such: (cross platform different function calls)</p>
  
  <pre><code>if os.name == 'posix':
      string = 'abc'
      libc = ctypes.CDLL('libc.so.6')
  
      # creating a pointer pointing at our string
      s_ptr = ctypes.c_char_p(string)
  
      # allocating free space
      free_space_ptr = ctypes.c_void_p(libc.valloc(ctypes.c_int(len(string))))
  
      # copying memory from one loc to another
      ctypes.memmove(free_space_ptr, s_ptr, ctypes.c_int(len(string)))
  
  else:
      string = 'abc'
  
      # allocating free space
      free_space_ptr = ctypes.windll.kernel32.VirtualAlloc(...)
  
      # creating a pointer pointing at our string?
      buffer = (cytpes.c_char_p * len(string))).from_buffer(string)
  
      # copying memory from one loc to another
      ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_void_p(free_space_ptr), buffer, ctypes.c_int(len(shellcode)))
  </code></pre>
  
  <p><strong>My Question is:</strong> </p>
  
  <p>The two function calls (both memmove, and RtlMoveMemory) take in two pointers ==> destination, source, and then the last param is the length to copy. </p>
  
  <p><strong><em>What is the difference in the two ways of getting a pointer that points to our string?</em></strong>:</p>
  
  <ul>
  <li>Using ctypes.c_char_p(string)</li>
  </ul>
  
  <p>vs</p>
  
  <ul>
  <li>Using ctypes.c_char.from_buffer(string)</li>
  </ul>