Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is [ComImport] considered P/Invoke?

Tags:

c#

pinvoke

com

What is Platform Invoke (P/Invoke)?


What does it mean to be performing a P/Invoke? Is it calling an external dll? e.g.:

[DllImport("coredll.dll", SetLastError=true)]
private static extern bool SHGetSpecialFolderPath(
      int hwndOwner, 
      string lpszPath,
      ceFolders nFolder,
      bool fCreate);

Is that what P/Invoke means: to use the [DllImport] attribute?

Is there anything else that can be considered a P/Invoke?

What about [ComImport]? e.g.:

[System.Runtime.InteropServices.ComImport]
[Guid("F8383852-FCD3-11d1-A6B9-006097DF5BD4")]
public class ProgressDialog
{
}

Note: This COM class (F8383852-FCD3-11d1-A6B9-006097DF5BD4) can be found in

HKEY_CLASSES_ROOT\CLSID\{F8383852-FCD3-11d1-A6B9-006097DF5BD4}
      (default)       %SystemRoot%\system32\shell32.dll
      ThreadingModel  Both

i could also construct a native ADO Recordset object with the code:

[System.Runtime.InteropServices.ComImport]
[Guid("00000535-0000-0010-8000-00AA006D2EA4")]
public class Recordset
{
}

Object rs= new Recordset();

Is that considered P/Invoke?

If we choose to say "P/Invoke is bad", is ComImport as "bad" as DllImport?

What is Platform Invoke (P/Invoke)?


Update: From MSDN:

Platform Invoke Tutorial

Platform Invocation Services (PInvoke) allows managed code to call unmanaged functions that are implemented in a DLL.

There are two ways that C# code can directly call unmanaged code:

  • Directly call a function exported from a DLL.
  • Call an interface method on a COM object (for more information, see COM Interop Part 1: C# Client Tutorial).

i think i may have answered my own question.


It's a year and a half later. Now that nobody is paying attention to the question, and nobody has it favorited, i can say that the answer i accepted is wrong. P/Invoke is short for Platform Invoke. It is a mechanism where managed code operating inside the CLR can call unmanaged native (i.e. platform) code. This is nearly always done by calling code that resides in a native dll. COM dlls are native code; they just follow a strict structure to allow many different compilers to call them.

And Platform Invoke is bad. It bypasses all garbage collection, and depends on the platform (i.e. my 32-bit CLR process cannot load a 64-bit dll, my application written for Android cannot work on Windows, my application written for functionality on Windows 8 won't work on Windows XP).

like image 678
Ian Boyd Avatar asked Jan 18 '12 14:01

Ian Boyd


1 Answers

No, they are not the same. P/Invoke (platform invoke) always involves calling native DLLs directly using the CLR functionality. "Native DLLs" means any DLL that expoes extern "C" functions. Managed DLLs don't let you expose them; such a DLL is typically written in C/C++. The give-away signature of P/Invoke in managed code is a DllImport attribute (or extern function in C#). Reading the P/Invoke page gives no mention of COM anywhere.

Using ComImport is for the purpose of creating a custom interop assembly (i.e. something hand-crafted rather than a PIA automatically generated by TlbImp), and uses inherent CLR functionality that is quite independent from the P/Invoke functionality, and specific to COM.

The similarities lie in that both these methods are used for interoperating with unmanaged code. Support for both is baked into the CLR, and although in theory one could use the Windows API to do COM interop completely manually in managed code, it makes no sense when .NET provides a structure means for you, in terms of primary or custom interop assemblies as well as lower-level support in System.ComponentModel.

like image 177
Noldorin Avatar answered Nov 08 '22 09:11

Noldorin