Preserving method signature for CCW

I got this tip from my friend, Rama Krishna Vavilala – who’s virtually a total expert in anything Win32, from SDK to COM to .NET! Thanks Rama!

By default, the CCW will convert your managed object’s methods so that the return type is an out parameter of the COM method. For example, if your managed method was :-

bool IsTrue(String^);

…then your CCW method will be :-

HRESULT IsTrue ([in] BSTR,
    [out,retval] unsigned char*);

Pretty annoying to have to check the HRESULT and to have to pass a pointer to the expected return type. Well, you can force the method to preserve its signature. You just need to apply the MethodImplAttribute to the method with MethodImplOptions::PreserveSig. Now your CCW method will become :-

unsigned char IsTrue([in] BSTR);

There, that’s much better now! Of course, to do this, you need to have access to the managed library source code, and your scenario should permit you to change the source code this way. Sometimes it may not be all that convenient to do so. But when you can, it’s good to know that there’s a way to do so.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s