使用Environment.Is64BitProcess从c#应用程序动态调用32位或64位DLL [英] Dynamically calling 32-bit or 64-bit DLL from c# application using Environment.Is64BitProcess

查看:683
本文介绍了使用Environment.Is64BitProcess从c#应用程序动态调用32位或64位DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为C.NET 4.0(通过Visual Studio 2010)编写用C#编写的项目。有一个第三方工具需要使用C / C ++ DLL,并且在C#中有32位应用程序和64位应用程序的示例。

I am working on a project written in C# for .NET 4.0 (via Visual Studio 2010). There is a 3rd party tool that requires the use of a C/C++ DLL and there are examples for 32-bit applications and 64-bit applications in C#.

问题是32位演示静态链接到32位DLL,而64位演示静态链接到64位DLL。作为.NET应用程序,它可以在客户端PC上作为32位或64位进程运行。

The problem is that the 32-bit demo statically links to the 32-bit DLL and the 64-bit demo statically links to the 64-bit DLL. Being a .NET application it could run as either a 32-bit or 64-bit process on the client PCs.

.NET 4.0框架提供了Environment.Is64BitProcess属性如果应用程序以64位进程运行,则返回true。

The .NET 4.0 framework provides the Environment.Is64BitProcess property that returns true if the application is running as a 64-bit process.

我想做的是在检查Is64BitProcess属性后动态加载正确的DLL。但是,当我研究动态加载库时,总是会提出以下内容:

What I would like to do is to dynamically load the correct DLL after checking the Is64BitProcess property. However, when I research dynamically loading libraries I always come up with the following:

[DllImport("kernel32.dll")]
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);

这些方法似乎专门用于32位操作系统。是否存在64位等效项?

It would appear that these methods are specifically for the 32-bit operating system. Are there 64-bit equivalents?

只要基于调用适当的方法,就会导致静态链接32位和64位库的问题。是64BitProcess检查吗?

Would it cause problems to statically link both the 32-bit and 64-bit libraries as long as the appropriate methods are called based on the Is64BitProcess check?

public class key32
{
    [DllImport("KEYDLL32.DLL", CharSet = CharSet.Auto)]
    private static extern uint KFUNC(int arg1, int arg2, int arg3, int arg4);

    public static bool IsValid()
    {
       ... calls KFUNC() ...
    }
}

public class key64
{
    [DllImport("KEYDLL64.DLL", CharSet = CharSet.Auto)]
    private static extern uint KFUNC(int arg1, int arg2, int arg3, int arg4);

    public static bool IsValid()
    {
       ... calls KFUNC() ...
    }
}

...

if (Environment.Is64BitProcess)
{
    Key64.IsValid();
}
else
{
    Key32.IsValid();
}

谢谢!

推荐答案

很多方法可以做到:


  • 这是一个部署问题,只需获取安装程序复制的正确DLL,并给它们起相同的名字

  • this is a deployment problem, just get the right DLL copied by the installer, give them the same name

很少有程序实际上需要大量的地址空间由64位代码提供。只需使用[DllImport]属性的EntryPoint字段将Platform目标设置为x86

very few programs actually need the massive address space provided by 64-bit code. Just set the Platform target to x86

。将其设置为 KFUNC。并为方法指定不同的名称。现在,您可以根据IntPtr.Size的值调用一个或另一个$ p $ b

use the EntryPoint field of the [DllImport] attribute. Set it to "KFUNC". And give the methods different names. Now you can call one or the other, based on the value of IntPtr.Size

演示最后一个解决方案:

Demonstrating the last solution:

[DllImport("KEYDLL32.DLL", EntryPoint = "KFUNC")]
private static extern uint KFUNC32(int arg1, int arg2, int arg3, int arg4);

[DllImport("KEYDLL64.DLL", EntryPoint = "KFUNC")]
private static extern uint KFUNC64(int arg1, int arg2, int arg3, int arg4);

...

if (IntPtr.Size == 8) KFUNC64(1, 2, 3, 4);
else                  KFUNC32(1, 2, 3, 4);

这篇关于使用Environment.Is64BitProcess从c#应用程序动态调用32位或64位DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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