Passing string over DLL boundaries.

Ò

Ò»Ê×Ê«

Hi all,

This a problem about windows dll.

I wrote a dll export this API to learn how strings are passed over the
dll boundaries.

extern "C" PINVOKELIB_API char * TestStringAsResult()
{
STRSAFE_LPWSTR result = (STRSAFE_LPWSTR)CoTaskMemAlloc( 1000 * 1000
* 100 );
StringCchCopy( result, 1000 , (STRSAFE_LPWSTR)"This is return
value" );
return (char *) result;
}

In my opinion, as the function allocated memory but do not release
it, any call of it would leak 100M bytes memory.

But that doesn't happen! Is there some mechanism that automatically
release memory of string passed over dll boundaries?

Thanks a lot!
 
J

Juha Nieminen

一首诗 said:
extern "C" PINVOKELIB_API char * TestStringAsResult()
{
STRSAFE_LPWSTR result = (STRSAFE_LPWSTR)CoTaskMemAlloc( 1000 * 1000
* 100 );
StringCchCopy( result, 1000 , (STRSAFE_LPWSTR)"This is return
value" );
return (char *) result;
}

In my opinion, as the function allocated memory but do not release
it, any call of it would leak 100M bytes memory.

But that doesn't happen! Is there some mechanism that automatically
release memory of string passed over dll boundaries?

Without knowing what STRSAVE_LPWSTR does it's impossible to say.
However, that name itself may be telling us something.
 
I

Ian Collins

Juha said:
Without knowing what STRSAVE_LPWSTR does it's impossible to say.
However, that name itself may be telling us something.

That this isn't a C++ question?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top