See the question and my original answer on StackOverflow

Well, your version is not a "string", it's indeed a pointer to raw memory. The .NET framework will try to deallocate this pointer if you tell it it's a string.

So, you have two options:

  • use an IntPtr like you do, which is fine.
  • allocate a real string that .NET can understand, using the COM allocator, so in your case, your C++ code could be replace by this:

.

__declspec(dllexport) char* WINAPI GetMyVersion() {
      char* p = (char*)CoTaskMemAlloc(7);
      CopyMemory(p, VERSION, 7);
      return p;
}

note: you can also use BSTRs.