如何从 C# 获取非托管全局变量?

发布于 2024-09-17 11:47:13 字数 412 浏览 7 评论 0原文

我正在为非托管 DLL 编写 .NET 包装器。原始 DLL 是带有 C 垫片的 C++,基本上只是以 C 形式复制 API,因此语言绑定并不是那么痛苦。我已经为它编写了一个 Python 绑定,所以我知道我想要做的事情应该可行。

DLL 有许多作为全局成员导出的回调,即:

__declspec(dllexport) extern int (*some_callback)(int32_t*, uint32_t);

我需要将托管函数附加到此指针,但我不知道如何获取非托管库中的非函数资源。除非我是瞎子,否则 DllImport 只导入函数。

有没有一种 C# 方法可以做到这一点,或者我需要用 C 编写一个提供注册函数的小垫片 DLL?我讨厌这种方法,因为它感觉不优雅,但如果我必须这样做,我就必须这样做。

I'm writing a .NET wrapper for an unmanaged DLL. The original DLL is C++ with a C shim that basically just replicates the API in C form, so that language bindings aren't such a pain. I've already written a Python binding for it, so I know what I'm trying to do should work.

The DLL has a number of callbacks exported as global members, viz.:

__declspec(dllexport) extern int (*some_callback)(int32_t*, uint32_t);

I need to attach a managed function to this pointer, but I can't figure out how to get at non-function resources in an unmanaged library. Unless I'm just blind, DllImport only imports functions.

Is there a C# way to do this, or do I need to write a little shim DLL in C that provides registration functions? I hate that approach, because it just feels inelegant, but if I have to, I have to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2024-09-24 11:47:13

你是对的,P/Invoke 无法处理从 DLL 导出的数据。然而,通过直接获得出口在技术上是可能的。这非常丑陋,有一天您可能会后悔,但这有效:

示例 C/C++ DLL:

#include "stdafx.h"

typedef int (__stdcall * pfnCallback)(int*, unsigned*);

extern "C" __declspec(dllexport) 
pfnCallback some_callback;

pfnCallback some_callback;

static int theCallback(int*, unsigned*) {
    return 42;
}

BOOL APIENTRY DllMain( HMODULE hModule, DWORD reason, LPVOID reserved) {
    if (reason == DLL_PROCESS_ATTACH) {
        some_callback = theCallback;
    }
    return TRUE;
}

测试 C# 代码:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

class Program {
    unsafe static void Main(string[] args) {
        IntPtr hMod = LoadLibrary("cpptemp10.dll");
        if (hMod == IntPtr.Zero) throw new Win32Exception();
        IntPtr export = GetProcAddress(hMod, "some_callback");
        if (export == IntPtr.Zero) throw new Win32Exception();
        IntPtr callback = Marshal.ReadIntPtr(export);
        some_callback dlg = (some_callback)Marshal.GetDelegateForFunctionPointer(callback, typeof(some_callback));
        int retval = dlg(null, null);
        Console.WriteLine(retval);
        Console.ReadLine();
    }

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    unsafe delegate int some_callback(int* arg1, uint* arg2);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr LoadLibrary(string path);
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern IntPtr GetProcAddress(IntPtr hMod, string name);

}

You're right, P/Invoke cannot handle data exports from a DLL. It is however technically possible by obtaining the export directly. This is very ugly and you'll probably regret it some day, but this worked:

Sample C/C++ DLL:

#include "stdafx.h"

typedef int (__stdcall * pfnCallback)(int*, unsigned*);

extern "C" __declspec(dllexport) 
pfnCallback some_callback;

pfnCallback some_callback;

static int theCallback(int*, unsigned*) {
    return 42;
}

BOOL APIENTRY DllMain( HMODULE hModule, DWORD reason, LPVOID reserved) {
    if (reason == DLL_PROCESS_ATTACH) {
        some_callback = theCallback;
    }
    return TRUE;
}

Test C# code:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

class Program {
    unsafe static void Main(string[] args) {
        IntPtr hMod = LoadLibrary("cpptemp10.dll");
        if (hMod == IntPtr.Zero) throw new Win32Exception();
        IntPtr export = GetProcAddress(hMod, "some_callback");
        if (export == IntPtr.Zero) throw new Win32Exception();
        IntPtr callback = Marshal.ReadIntPtr(export);
        some_callback dlg = (some_callback)Marshal.GetDelegateForFunctionPointer(callback, typeof(some_callback));
        int retval = dlg(null, null);
        Console.WriteLine(retval);
        Console.ReadLine();
    }

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    unsafe delegate int some_callback(int* arg1, uint* arg2);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr LoadLibrary(string path);
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern IntPtr GetProcAddress(IntPtr hMod, string name);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文