title: Using a C++ DLL in Python
body: <p>So here I have a function in my DLL in C++ which allows to create a memory block with a string and which returns its memory address. Then from my python code I send this memory address to my 2nd function in C++ and I would like to modify the string from this address, but I can't.</p>
  <p>How can I do it?</p>
  <p>My C++ DLL</p>
  <pre><code>EXPORT_API void* memory_string(int size, int* pValues)  {
      const char text[] = &quot;hello world !&quot;;
  
      size_t malloc_allocation_size = strlen(text) + 1;
      char* text_allocation = (char*)malloc(malloc_allocation_size * sizeof(char));
  
      strcpy_s(text_allocation, malloc_allocation_size, text);
  
      //uintptr_t address = reinterpret_cast&lt;uintptr_t&gt;(text_allocation);
  
      return static_cast&lt;void*&gt;(text_allocation);
  }
  
  EXPORT_API void free_memory(unsigned long long ptr)
  {
      char* memory_string = reinterpret_cast&lt;char*&gt;(ptr);
      uintptr_t* memory_address = reinterpret_cast&lt;uintptr_t*&gt;(ptr);
  
      (*memory_string) = &quot;hello World !&quot;;
  }
  </code></pre>
  <p>My python code</p>
  <pre><code>import ctypes
  
  DLL_PATH = &quot;./RayDLL.dll&quot;
  LOAD_DLL = ctypes.cdll.LoadLibrary(DLL_PATH)
  
  INT_POINTER = ctypes.POINTER(ctypes.c_int)
  
  hex_array = ['0x72', '0x7c', '0x75', '0x65', '0x7e', '0x7f', '0x3d', '0x24', '0x25', '0x23', '0x24', '0x25', '0x23', '0x29']
  size_array = len(hex_array) + 1
  
  memory_string_function = LOAD_DLL.memory_string
  memory_string_function.argtypes = (ctypes.c_int, INT_POINTER)
  memory_string_function.restype = ctypes.c_void_p
  
  int_array = ( ctypes.c_int * size_array )(*(int(_, base=16) for _ in hex_array) )
  x = LOAD_DLL.memory_string(size_array, ctypes.cast(int_array, INT_POINTER))
  
  #print(x)
  print(ctypes.cast(x, ctypes.c_char_p).value)
  
  LOAD_DLL.free_memory(ctypes.c_ulonglong(x))
  
  print(ctypes.cast(x, ctypes.c_char_p).value)
  </code></pre>