# Sunday, 21 May 2006

As most of my readers know, there are three kinds of managed-to-native interop available to the C++/CLI programmer using Visual Studio 2005, as there were to the Managed C++ programmer using Visual Studio 2002 or 2003. Those are COM Interop, P/Invoke, and C++ Interop (formerly and more colourfully known as It Just Works Interop.) COM Interop is slow, but if you have a COM component already written, maybe in VB 5 or something, it's just the ticket. P/Invoke is good for C-style DLLs, and popular with people who want to call into the Windows API. C++ Interop is the easiest (say it with me: include the header, link to the lib) and fastest, and it's available only from C++.

So why would a C++ dev, who can just include-the-header-link-to-the-lib, ever use P/Invoke? Because it handles marshalling and translation for you. Take a look at this (reasonably old) post by Kenny Kerr. He includes this function in the post:

[DllImport("Native.dll", PreserveSig=false, CharSet=CharSet::Unicode)]
bool BrowseForComputers(IntPtr parentWindow,
                        bool multiSelect,
                        [MarshalAs(UnmanagedType::SafeArray, SafeArraySubType=VarEnum::VT_VARIANT)]
                        array<String^>^% computers);

That would be pretty grody work without P/Invoke. Let the feature help you where it can.


Sunday, 21 May 2006 07:22:49 (Eastern Daylight Time, UTC-04:00)  #    Comments [0]
Comments are closed.