Is [ComImport] P / Invoke considered?

What does it mean Platform Challenge (P / Invoke)?


What does running P / Invoke mean? Is this an external dll call ? eg:.

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

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

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

How about [ComImport] ? eg:.

 [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 build my own ADO Recordset object with code:

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

Is it considered P / Invoke?

If we want to say that “P / Invoke is bad”, is ComImport “bad” like DllImport?

What does it mean Platform Challenge (P / Invoke)?


Update : from MSDN:

Educational application for the platform

Invocation Services for Platforms (PInvoke) allows you to manage managed code unmanaged functions implemented in DLLs.

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

I think I may have answered my question.


This is in a year and a half. Now that no one pays attention to this question, and no one has this, I can say that the answer I accepted is erroneous. P/Invoke short for Platform Invoke . This is a mechanism in which managed code running inside the CLR can invoke unmanaged native (e.g., platform) code. This is almost always done by calling code that resides in the native DLL. COM dll is native code; they simply follow a strict structure that allows many different compilers to call them.

And Platform Invoke is bad. It bypasses the entire garbage collection and is platform dependent (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 , Windows XP will not work).

+6
source share
1 answer

No, they are not the same. P / Invoke (platform invoke) always includes invoking native DLL files directly using CLR functions. "Native DLLs" means any DLL that exposes extern "C" functions. Managed DLLs do not allow you to open them; such a DLL is usually written in C / C ++. The P / Invoke signed signature in managed code is a DllImport attribute (or extern in C #). Reading the P / Invoke page does not mention COM anywhere.

Using ComImport intended to create a custom interop assembly (i.e., something created manually, not PIA, automatically generated by TlbImp ) and uses the built-in CLR functionality, which is completely independent of P / Invoke functionality and specific to COM.

The similarity is that both of these methods are used to interact with unmanaged code. Support for both is baked in the CLR, and although it is theoretically possible to use the Windows API for full COM interaction manually in managed code, it makes no sense when .NET provides the framework for you, in terms of primary or user-level assembly interaction, as well as lower-level support in System.ComponentModel .

+7
source

Source: https://habr.com/ru/post/906312/


All Articles