相关疑难解决方法(0)

将非托管dll嵌入到托管C#dll中

我有一个使用DLLImport使用非托管C++ DLL的托管C#dll.一切都很好.但是,我想在我的托管DLL中嵌入非托管DLL,如Microsoft解释:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.dllimportattribute.aspx

所以我将非托管dll文件添加到我的托管dll项目,将属性设置为'Embedded Resource'并将DLLImport修改为:

[DllImport("Unmanaged Driver.dll, Wrapper Engine, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null",
CallingConvention = CallingConvention.Winapi)]
Run Code Online (Sandbox Code Playgroud)

其中'Wrapper Engine'是我托管DLL的程序集名称'Unmanaged Driver.dll'是非托管DLL

当我跑步时,我得到:

访问被拒绝.(HRESULT异常:0x80070005(E_ACCESSDENIED))

我从MSDN和http://blogs.msdn.com/suzcook/看到了这应该是可能的......

c# unmanaged managed dllimport

82
推荐指数
4
解决办法
6万
查看次数

我可以从 .Net Core 调用 kernel32.dll 中的 LoadLibrary() 的 Linux 等效模块和函数吗?

我正在致力于将 .Net Core 3.1 应用程序从 Windows 移植到 Linux。我在 Windows 上有一个 ourdevice.dll,以及为 Linux 构建的等效 ourdevice.so。该 dll 是我们在 Windows 上使用 pinvoke 包装器使用的本机 dll。

我们使用 kernel32.dll 中的 DllImports 从本机 dll 加载函数

    [DllImport("kernel32.dll", EntryPoint = "LoadLibrary", SetLastError = true)]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);
Run Code Online (Sandbox Code Playgroud)

我们为每个要导入的函数创建委托:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate short AdcOpen([MarshalAs(UnmanagedType.LPStr)] string adcName, [MarshalAs(UnmanagedType.LPStr)] string protocol, [MarshalAs(UnmanagedType.LPStr)] string port, ref short handle, byte performSwReset);
    private AdcOpen adcOpen;
Run Code Online (Sandbox Code Playgroud)

然后我们映射所有本机函数,如下所示: …

c# linux .net-core

2
推荐指数
1
解决办法
1958
查看次数

标签 统计

c# ×2

.net-core ×1

dllimport ×1

linux ×1

managed ×1

unmanaged ×1