如何根据主机系统arch选择性地加载非托管delphi .dll(32位或64位)并从C#项目调用其函数? [英] How to selectively load an unmanaged delphi .dll (32bit or 64bit), according to the host system arch) and call its functions from a C# project?

查看:59
本文介绍了如何根据主机系统arch选择性地加载非托管delphi .dll(32位或64位)并从C#项目调用其函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我是C#的新手,我目前正在努力解决如何选择性地将非托管.dll加载到我的程序中的问题。问题如下:



。 C#程序需要能够识别正在运行的系统的位宽,加载正确的.dll(32或64位版本)



MyDLL32bit。 dll

MyDLL64bit.dll



dll有不同的名称来反映位宽。



。程序需要在任何加载的.dll版本中调用函数(一些采用参数,另一些只是返回值)。我无法访问dll。但我确实知道函数的名称,它们所采用的参数以及它们返回的值。



我搜索了这个,有些人建议使用



[DllImport(MyDLL32bit.dll)]

public static extern int doSomething();



但是我可以编写程序来选择正确的dll(32位或64位)并在正确的dll版本上调用函数吗?



最后,代码需要尽可能独立。我的意思是,如果所有这些都可以在C#中完成而无需使用Windows API,那将更为可取。



任何帮助都将非常感激。< br $> b $ b

我尝试过的事情:



我在网上查了一下像这样的论坛,尝试使用Dllimport解决方案。可用信息非常矛盾,根本不容易掌握。

Hello,

I am new to C# and I am currently struggling with how I can selectively load an unmanaged .dll into my program. The problem is the following:

. The C# program needs to have the ability to identify the bit width of the system it is running on, loading the correct .dll (32 or 64bit version)

MyDLL32bit.dll
MyDLL64bit.dll

The dll have different names to reflect the bit width.

.The program needs to call the functions in whatever version of the .dll that is loaded (some take arguments and others simply return values). I have no access to the dll. I do however know the names of the functions, the parameters they take and values they return.

I've searched for this and some people suggest the use of the

[DllImport("MyDLL32bit.dll")]
public static extern int doSomething();

But could I code the program to select the right dll (32 bit or 64bit) and to call the functions on the correct dll version?

Lastly, the code needs to be as independent as possible. What I mean is, if all this can be done in C# without the need for using Windows API, that would be preferable.

Any help would be much appreciated.

What I have tried:

I've looked in different online forums such as this one and tried using the Dllimport solution. The information available is very conflicting and not easy to grasp at all.

推荐答案

此StackOverflow主题 [ ^ ]。例如:

There are several suggestions in this StackOverflow thread[^]. For example:
internal static class SafeNativeMethods
{
    [DllImport("MyDLL32bit.dll", EntryPoint = "doSomething")]
    private static extern int doSomething32();
    
    [DllImport("MyDLL64bit.dll", EntryPoint = "doSomething")]
    private static extern int doSomething64();
    
    public static int doSomething()
    {
        return Environment.Is64BitProcess
            ? doSomething64();
            : doSomething32();
    }
}



在第一次调用 extern 之前,两个库都不会被加载从该库导入的函数。只要您的代码永远不会尝试从错误的库中调用函数,一切都应该有效。


Neither library will be loaded until the first call to an extern function imported from that library. So long as your code never tries to call a function from the wrong library, everything should work.


这篇关于如何根据主机系统arch选择性地加载非托管delphi .dll(32位或64位)并从C#项目调用其函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆