C#调用非托管dll的两种方式


C#调用非托管DLL的两种方式

在开发过程中,我们经常需要调用一些非托管的DLL库,比如用Delphi编写的DLL。本文将介绍两种在C#中调用非托管DLL的方法。

示例DLL

首先,我们有一个Delphi编写的DLL,它导出了一个名为MyFunction的方法:

library testDLL;

uses
  SysUtils,
  Dialogs,
  Classes;

{$R *.res}

function MyFunction(param1: Integer): Integer; stdcall;
begin
  Result := param1 + 1;
end;

exports
  MyFunction;

begin
end.

方式一:直接调用

这是最常见的调用方式,通过DllImport属性直接导入非托管方法。

[DllImport("testDLL.dll")]
static extern int MyFunction(int a);

然后,你可以直接调用这个方法:

int i = MyFunction(123);

方式二:使用本地委托

另一种方法是将非托管方法转换为本地委托。这种方式更灵活,但需要更多的代码。

辅助方法

首先,我们需要一些辅助方法来加载和卸载DLL:

// 辅助方法:加载DLL
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LoadLibrary(string lpFileName);

// 辅助方法:卸载DLL
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeLibrary(IntPtr hModule);

// 获取方法指针
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);

定义委托

接下来,定义一个与DLL中方法参数一致的委托:

public delegate int MyFunctionDelphiDelegate(int param1);

使用委托调用方法

最后,使用委托来调用非托管方法:

void run()
{
    // 加载DLL
    IntPtr dllHandle = LoadLibrary("testDLL.dll");
    if (dllHandle == IntPtr.Zero)
    {
        var err = Marshal.GetHRForLastWin32Error();
    }

    // 获取方法指针
    IntPtr functionPointer = GetProcAddress(dllHandle, "MyFunction");

    MyFunctionDelphiDelegate my = (MyFunctionDelphiDelegate)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(MyFunctionDelphiDelegate));
    // 调用方法
    int i = my(1);

    // 释放DLL
    FreeLibrary(dllHandle);
}

注意事项

  1. 位数一致性:确保你的程序和DLL的位数(32位或64位)保持一致。
  2. 字符串参数:Delphi方法中的参数和返回值如果是字符串,不能直接使用String类型。需要使用PAnsiCharPChar,并搭配StrNew使用。
  3. 返回值:如果方法返回值为字符串,需要使用IntPtr类型,并通过Marshal.PtrToStringAnsi(strIntPtr)来获取实际的字符串值。

通过这两种方式,你可以灵活地在C#中调用非托管的DLL库,满足不同的开发需求。


相关推荐

  1. C#调用托管dll方式

    2024-07-18 01:50:03       22 阅读
  2. C#调用托管dll,并从dll中再调用C#中方法

    2024-07-18 01:50:03       23 阅读
  3. C#-托管代码

    2024-07-18 01:50:03       42 阅读
  4. Lua调用c++函数办法

    2024-07-18 01:50:03       33 阅读
  5. C++生成随机数方法

    2024-07-18 01:50:03       15 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 01:50:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 01:50:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 01:50:03       58 阅读
  4. Python语言-面向对象

    2024-07-18 01:50:03       69 阅读

热门阅读

  1. WEB渗透之相关概念(笔记)

    2024-07-18 01:50:03       22 阅读
  2. idea 运行异常 gradle 项目

    2024-07-18 01:50:03       20 阅读
  3. C++ Primer:3.6 多维数组

    2024-07-18 01:50:03       26 阅读
  4. 设计模式大白话之适配器模式

    2024-07-18 01:50:03       24 阅读
  5. C语言经典例题-19

    2024-07-18 01:50:03       19 阅读