[Interop from managed code to unmanaged code]Platform Invoke
2010-04-01 17:45
411 查看
PInvoke
Platform Invocation Services (PInvoke) allows managed code to call unmanaged functions that are implemented in a DLL.
This tutorial shows you what you need to do to be able to call unmanaged DLL functions from C#. The attributes discussed in the tutorial allow you to call these functions and have data types be marshaled correctly.
Visit MSDN article link to get whole story about how to use unmanaged dll in C#.
Marshal Class
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types, as well as other miscellaneous methods used when interacting with unmanaged code.
MarshalAs
There is default marshaling from .Net to handle the interop from managed code (c#) to unmanaged code. Using MarshalAs gives you a chance to change the unmanaged type to marshal with.
from MSDN:
For every .NET Framework type there is a default unmanaged type, which the common language runtime will use to marshal data across a managed to unmanaged function call. For example, the default marshaling for C# string values is to the type LPTSTR (pointer to TCHAR char buffer). You can override the default marshaling using the MarshalAs attribute in the C# declaration of the unmanaged function.
The MarshalAs attribute can be placed on method parameters, method return values, and fields of structs and classes. "
Platform Invocation Services (PInvoke) allows managed code to call unmanaged functions that are implemented in a DLL.
This tutorial shows you what you need to do to be able to call unmanaged DLL functions from C#. The attributes discussed in the tutorial allow you to call these functions and have data types be marshaled correctly.
Visit MSDN article link to get whole story about how to use unmanaged dll in C#.
Marshal Class
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types, as well as other miscellaneous methods used when interacting with unmanaged code.
MarshalAs
There is default marshaling from .Net to handle the interop from managed code (c#) to unmanaged code. Using MarshalAs gives you a chance to change the unmanaged type to marshal with.
from MSDN:
"Default Marshaling and Specifying Custom Marshaling for Parameters to Unmanaged Methods
When calling an unmanaged function from C# code, the common language runtime must marshal the parameters and return values.For every .NET Framework type there is a default unmanaged type, which the common language runtime will use to marshal data across a managed to unmanaged function call. For example, the default marshaling for C# string values is to the type LPTSTR (pointer to TCHAR char buffer). You can override the default marshaling using the MarshalAs attribute in the C# declaration of the unmanaged function.
The MarshalAs attribute can be placed on method parameters, method return values, and fields of structs and classes. "
相关文章推荐
- Sinking events from managed code in unmanaged C++
- Passing Managed Structures With Arrays To Unmanaged Code Part 1
- Passing Managed Structures With Arrays To Unmanaged Code Part 2
- Passing Managed Structures With Arrays To Unmanaged Code Part 3
- How to handle exception in managed code and unmanaged code
- Sinking events from managed code in unmanaged C++
- Calling Managed .NET C# COM Objects from Unmanaged C++ Code
- How to handle exception in managed code and unmanaged code
- How to release an unmanaged library loaded into managed .NET code
- How to make a callback to C# from C/C++ code
- Managed/Unmanaged Code Interoperability
- From Program to Product: Turning Your Code into a Saleable Product
- Beginning C# Objects:From Concepts to Code
- Configuring Visual Studio to Debug .NET Framework Source Code【from Shawn Burke's Blog】
- Get Up to 20% off lucky code for 07 rs gold buying from Rsorder facebook 5.20-5.30
- To consume a managed type from COM 之一 Register assemblies with COM
- Beginning C# Objects: From Concepts to Code (Note)
- An Overview of Managed/Unmanaged Code Interoperability
- How to use gcov to get code coverage from static library in iphone?
- 安装Adobe Flash CS5出错的解决办法(Exit Code: 7 ERROR: Unable to get root from inChildPath)