Take the 2-minute tour ×

David's answer to another question shows a Delphi DLL function returning a WideString. I never thought that was possible without the use of ShareMem.

My test DLL:

function SomeFunction1: Widestring; stdcall;
  Result := 'Hello';

function SomeFunction2(var OutVar: Widestring): BOOL; stdcall;
  OutVar := 'Hello';
  Result := True;

My caller program:

function SomeFunction1: WideString; stdcall; external 'Test.dll';
function SomeFunction2(var OutVar: Widestring): BOOL; stdcall; external 'Test.dll';

procedure TForm1.Button1Click(Sender: TObject);
  W: WideString;

It works, and I don't understand how. The convention I know of is the one used by the Windows API, for example Windows GetClassNameW:

function GetClassNameW(hWnd: HWND; lpClassName: PWideChar; nMaxCount: Integer): Integer; stdcall;

Meaning the caller provides the buffer, and the maximum length. The Windows DLL writes to that buffer with the length limitation. The caller is allocates and deallocates the memory.

Another option is that the DLL allocate the memory for example by using LocalAlloc, and the Caller deallocates the memory by calling LocalFree.

How does the memory allocation and deallocation work with my DLL example? Does the "magic" happen because the result is WideString(BSTR)? And why aren't Windows APIs declared with such convenient convention? (Are there any known Win32 APIs that uses such convention?)


I Tested the DLL with C#.
Calling SomeFunction1 causes an AV (Attempted to read or write protected memory).
SomeFunction2 works fine.

[return: MarshalAs(UnmanagedType.BStr)]
static extern string SomeFunction1();

[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SomeFunction2([MarshalAs(UnmanagedType.BStr)] out string res);


string s;
SomeFunction2(out s);
MessageBox.Show(s); // works ok
MessageBox.Show(SomeFunction1()); // fails with AV!

Here is a followup.

share|improve this question
add comment

1 Answer

up vote 18 down vote accepted

A WideString is the same as a BSTR, it's just the Delphi name for it. The memory allocation is handled by the shared COM allocator, CoTaskMemAlloc. Because all parties use the same allocator you can safely allocate in one module and deallocate in another.

So, the reason you don't need to use Sharemem is that the Delphi heap is not being used. Instead the COM heap is used. And that is shared between all modules in a process.

If you look at the Delphi implementation of WideString you will see calls to the following APIs: SysAllocStringLen, SysFreeString and SysReAllocStringLen. These are the system provided BSTR API functions.

Many of the Windows APIs you refer to pre-date the invention of COM. What's more, there are performance benefits to using a fixed length buffer, allocated by the caller. Namely that it can be allocated on the stack rather than a heap. I also can imagine that the Windows designers don't want to force every process to have to link to OleAut32.dll and pay the price of maintaining the COM heap. Remember that when most of the Windows API was designed, the performance characteristics of the typical hardware was very different from now.

Another possible reason for not using BSTR more widely is that the Windows API is targeted at C. And managing the lifetime of BSTR from C is very much more tricky than from higher level languages like C++, C#, Delphi etc.

share|improve this answer
Thanks for answering. Look here. my understanding is that the DLL should is call CoTaskMemAlloc. does Delphi call it when allocation WideString? also, when does the CoTaskMemFree called? –  kobik Feb 17 '12 at 16:22
The SysXXX functions make the necessary calls to the COM allocator. –  David Heffernan Feb 17 '12 at 16:23
The link in your comment merely states the fact that the p/invoke marshaller assumes that a return value of type string was allocated with CoTaskMemAlloc and so it calls CoTaskMemFree when it has finished marshalling. –  David Heffernan Feb 17 '12 at 16:25
@kobik, David, that's an interesting question/discussion... Thanks! –  François Feb 17 '12 at 19:01
@kobik: most Win32 API functions DO NOT use CoTaskMem...() to manage memory. They use GlobalAlloc() or LocalAlloc() instead, both of which are local to the calling process and do not need sharing across process boundaries. Documentation for functions usually states how memory is alloated and how it must be freed. –  Remy Lebeau Feb 17 '12 at 22:38
show 19 more comments

Your Answer


