`

c# - P/Invoke, DllImport, Marshal Structures and Type conversions

    博客分类:
  • C#
c# 
阅读更多

P/Invoke as in the following quote provides basically the way to calls into the unmanaged DLLs. the quote is as follow. 

MSDN 写道
P/Invoke is short for Platform Invoke and provides the functionality to access functions, structs, and callbacks in unmanaged DLLs. P/Invoke provides a translation layer to assist developers by allowing them to extend the library of available functionality beyond the managed library of the BCL. To easiest way to understand how to use P/Invoke is to look at some sample applications that use P/Invoke to accomplish a task not possible with managed code.

 

DllImport

 

DllImport has many a attribute, but fortunately you can visit all those attribute from the following page: Using P/Invoke to call Unmanaged APIs from your Managed Classes, attribute include 

  • Name
  • EntryPoint
  • SetLastError
  • CharSet
  • ExactSpelling
  • BestFitMapping
  • Calling Convention
  • Perserve Signature
  • ThrowOnUnmappedChar

Marshalling

 

As for marshalling, 

as it is quoted:

MSDN 写道
Marshalling is the act of converting data such that it can be passed and correctly analyzed between managed and unmanaged program spaces. This marshaling is performed at runtime using the CLR's marshalling service. When using P/Invoke you typically need to marshal classes and structs and control marshalling details using a number of attributes from the System.Runtime.InteropServices namespace.

 

As this is maybe very important for the common types to marshal across platform..

 

Windows Data Type .NET Data Type
BOOL, BOOLEAN Boolean or Int32
BSTR String
BYTE Byte
CHAR Char
DOUBLE Double
DWORD/LPDWORD Int32 or UInt32
FLOAT Single
HANDLE (and all other handle types, such as HFONT and HMENU) IntPtr, UintPtr, or HandleRef
HRESULT Int32 or UInt32
INT Int32
LANGID Int16 or UInt16
LCID Int32 or UInt32
LONG Int32
LPARAM IntPtr, UintPtr, or Object
LPCSTR String
LPCTSTR String
LPCWSTR String
LPSTR String or StringBuilder*
LPTSTR String or StringBuilder
LPWSTR String or StringBuilder
LPVOID IntPtr, UintPtr, or Object
LRESULT IntPtr
SAFEARRAY .NET array type
SHORT Int16
TCHAR Char
UCHAR SByte
UINT Int32 or UInt32
ULONG Int32 or UInt32
VARIANT Object
VARIANT_BOOL Boolean
WCHAR Char
WORD Int16 or UInt16
WPARAM IntPtr, UintPtr, or Object

 

 

StructLayout Attribute

Because we have to marshal types between native and managed code, we have to take care to handle the StructLayout which controls how they end up in the memory (imagine a native function which pass a struct out with special layout from what is expected from managed side or the reverse/inverse.)

 

So what is structLayout is about?

MSDN 写道
In addition to the simple types listed in the type conversion table, many unmanaged functions use structs to pass information more efficiently. The StructLayout attribute allows you to specify how to lay out the data members in managed portions of memory so that they can successfully be referenced in unmanaged portions of memory.

 The following will control the how a StructLayout is 

  • Layout - one of the following kinds
    •    LayoutKind.Sequential
    •    LayoutKind.Union
    •    LayoutKind.Explicit
  • Pack
  • Charset
  • Size

Besisde the explicit PInvoke, there is some implicit invoke - a more accurate way of saying is "C++ Interop (Implicit PInvoke) - whch is said to have better support in 
managed and unmanaged code to exist in the same application/even same file - from unmanaged to managed ones
Better type safety, less tedious to implement, more forgiving if the unmanaged API is modified, makes performance enhancement possible which is not possible with explicit PInvoke.


 

 

References:

Using P/Invoke to call Unmanaged APIs from your Managed Classes

How to : Marshal Structures Using PInvoke

分享到:
评论

相关推荐

    P/Invoke Interop Assistant交互助手

    P/Invoke Interop Assistant是一个非常实用的工具,它专门用于帮助C#或VB.NET开发者将C或C++的函数和数据类型转换为.NET Framework兼容的形式。这个工具的主要目的是解决跨语言互操作性问题,即所谓的“平台调用”...

    P/Invoke Interop 助手

    P/Invoke Interop Assistant,它支持托管代码和非托管代码之间的方法签名的转换,而且直接生成相关的C#或者是VB的方法调用代码。这个签名的转换,不只是适用于Windows的方法签名,只要给定一个C头文件,就可以转换...

    P/Invoke Interop Assistant

    3. 生成P/Invoke代码:根据解析结果,自动生成相应的C#或VB.NET代码,包括必需的DllImport特性,函数签名,以及可能的Marshaling代码。 4. 复制并粘贴到项目中:开发者将生成的代码复制到.NET项目中,即可直接调用...

    《精通.NET互操作:p/invoke, c++ interop和COM interop》源代码

    《精通.NET互操作:p/invoke, c++ interop和COM interop》这本书深入探讨了.NET框架中的三种主要互操作技术,即平台调用(P/Invoke)、C++互操作(C++ Interop,也称为StructLayoutAttribute)以及组件对象模型...

    p/Invoke.net工具

    《P/Invoke.NET工具详解:实现托管代码与非托管代码的无缝交互》 在软件开发过程中,有时我们需要在.NET Framework的托管代码环境中调用非托管的C/C++库或者WinAPI函数,这时就需要借助P/Invoke(Platform Invoke)...

    使用P/Invoke来开发用于与串行设备通讯的.NET基类

    标题中的“使用P/Invoke来开发用于与串行设备通讯的.NET基类”指的是在.NET环境中,使用C#编程语言,通过平台调用服务(P/Invoke)来直接调用Win32 API,以实现与RS232串行设备的通信。这种方式避免了依赖过时的...

    C#串口操作小例子,P/Invoke

    2. **P/Invoke声明**:在C#代码中,我们需要使用`DllImport`特性来声明并加载非托管库。例如: ```csharp [DllImport("kernel32.dll", SetLastError = true)] static extern SafeFileHandle CreateFile( string ...

    《CLR via C#》非托管代码互操作性提到的P/Invoke Interop Assistant

    《CLR via C#》非托管代码互操作性提到的P/Invoke Interop Assistant开发工具,包含源代码,是从网站:http://clrinterop.codeplex.com/ 上下载的,作为备份

    PInvoke Interop Assistant

    PInvoke Interop Assistant是一款强大的工具,它为程序员提供了一个便捷的方式,将C/C++的原生代码转换为C#或VB.NET的平台调用(P/Invoke)代码。在.NET框架中,P/Invoke是允许托管代码(如C#或VB.NET)与非托管代码...

    P/Invoke获取CultureInfo

    然而,有些特定的操作可能需要访问操作系统底层的功能,例如获取或设置系统当前的CultureInfo,这时就需要用到P/Invoke(Platform Invoke)技术。 P/Invoke是.NET Framework提供的一种机制,允许托管代码调用非托管...

    Invoke-WCMDump结合powershell进行密码获取

    Ivoke-WCMDump 什么是Credential Manager ...从Credential Manager导出Windows凭据的Powershell脚本 https://github.com/peewpw/Invoke-WCMDump PS>Import-Module .\Invoke-WCMDump.ps1 ...Invoke-WCMDump

    《精通.NET互操作:pinvoke, c++ interop和COM interop》全部源代码

    P/Invoke是.NET Framework提供的一种机制,允许C#和其他.NET语言调用本机Windows API或其他非托管代码。在C#中,通常通过`[DllImport]`特性来声明外部函数,并指定DLL库和函数签名。P/Invoke是.NET与C/C++代码交互...

    连接COM与.NET的桥梁(二)——COM服务器的P/Invoke方式

    在IT行业中,互操作性是至关重要的,尤其是在不同的技术栈之间,如经典的COM(Component Object Model)和现代的.NET框架。...理解并熟练掌握P/Invoke技术,对于开发跨平台、跨技术栈的应用程序至关重要。

    PInvoke Interop Assistant.7z

    Microsoft 推出的 P/Invoke Interop Assistant#C/C++ API 转 C# 工具 找资源不易,限定5个积分

    generic-webhook-trigger-plugin:可以接收任何HTTP请求,从JSON或XML中提取任何值,并使用这些值作为变量来触发作业。 与GitHub,GitLab,Bitbucket,Jira等一起使用

    接收任何HTTP请求, JENKINS_URL/generic-webhook-trigger/invoke 提取值 从带有或POST内容 从query参数 从headers 使用这些值作为变量触发构建 仅当提供的正则表达式与提取的变量匹配时,才有可选功能来触发作业...

    PInvoke Interop Assistant.rar

    标题 "PInvoke Interop Assistant.rar" 暗示了这是一个关于C#与C++之间互操作(Interop)的工具包,特别关注P/Invoke技术。P/Invoke是.NET框架提供的一种机制,允许托管代码(C#)调用非托管代码(通常为C或C++编写...

    J/Invoke 2010 jinvoke jni 天涯浪子

    J/Invoke enables Java developers to easily invoke native methods (such as the Win32 API or C-based Windows DLLs and Unix dynamic libraries) with pure Java code. Unlike error-prone JNI programming ...

Global site tag (gtag.js) - Google Analytics