简体   繁体   English

是否可以使用来自托管代码的 C# 反射来调用非托管代码?

[英]Is it possible to call unmanaged code using C# reflection from managed code?

Is it possible using reflection and C# .NET to call dynamically different function (with arguments) written in C or C++ before .NET came (unmanaged code) ?在 .NET 出现(非托管代码)之前,是否可以使用反射和 C# .NET 来动态调用用 C 或 C++ 编写的不同函数(带参数)?

A small C# example if possible would be appreciated!如果可能的话,一个小的 C# 示例将不胜感激!

Yes, dynamic P/Invoke is possible in .NET in different ways.是的,在 .NET 中可以通过不同的方式实现动态 P/Invoke。

LoadLibrary and Marshal.GetDelegateForFunctionPointer LoadLibrary 和 Marshal.GetDelegateForFunctionPointer

Here's an example using Marshal.GetDelegateForFunctionPointer taken from the section Delegates and unmanaged function pointers from the article Writing C# 2.0 Unsafe Code by Patrick Smacchia, a very similar sample is also available in this old blog post by Junfeng Zhang这是一个使用Marshal.GetDelegateForFunctionPointer的示例,该示例取自 Patrick Smacchia 的文章Writing C# 2.0 Unsafe Code 中Delegates 和非托管函数指针部分 Junfeng Zhang 的这篇旧博文中也提供了一个非常相似的示例

using System;
using System.Runtime.InteropServices;
class Program
{
     internal delegate bool DelegBeep(uint iFreq, uint iDuration);
     [DllImport("kernel32.dll")]
     internal static extern IntPtr LoadLibrary(String dllname);
     [DllImport("kernel32.dll")]
     internal static extern IntPtr GetProcAddress(IntPtr hModule,String procName);
     static void Main()
     {
          IntPtr kernel32 = LoadLibrary( "Kernel32.dll" );
          IntPtr procBeep = GetProcAddress( kernel32, "Beep" );
          DelegBeep delegBeep = Marshal.GetDelegateForFunctionPointer(procBeep , typeof( DelegBeep ) ) as DelegBeep;
          delegBeep(100,100);
     }
}

Reflection.Emit反射.发射

This method work in all versions of .NET.此方法适用于所有版本的 .NET。 It is described with an example in the documentation of System.Reflection.Emit.ModuleBuilder.DefinePInvokeMethod它在System.Reflection.Emit.ModuleBuilder.DefinePInvokeMethod的文档中用一个例子来描述

Reflection only works with managed code.反射仅适用于托管代码。

Depending on what the unmanaged code actually is you could use COM interop (for com components) or PInvoke (for old-style dll's) to invoke the unmanaged code.根据非托管代码的实际内容,您可以使用 COM 互操作(对于 com 组件)或 PInvoke(对于旧式 dll)来调用非托管代码。 Maybe you can write a wrapper around the unmanaged code to make this possible.也许您可以围绕非托管代码编写一个包装器来实现这一点。

不,反射仅适用于托管代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM