在一个程序中结合 32 位和 64 位 DLL [英] Combine 32- and 64bit DLLs in one program

查看:33
本文介绍了在一个程序中结合 32 位和 64 位 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加载 .dll 文件中提供的不同硬件驱动程序.问题似乎是一个设备的驱动程序在 64 位 dll 中给出,另一个设备(相当旧)显然依赖于 32 位 dll 中给出的驱动程序.我想通过一个用 C# 编写的程序来控制它们,该程序将通过 python 包装器运行.

I need to load different hardware drivers that are provided in .dll files. The problem appears to be that the drivers for one device are given in a 64bit dll, the other device (rather old) apparently relies on drivers given in a 32bit dll. I want to control them through a program written in C# which will be run through a python wrapper.

显然我不能直接从一个程序运行两个设备,但我需要一种方法来根据彼此寻址它们 - 例如:设备 1 等待设备 2 完成某些工作.有什么办法可以绕过这个问题,还是我需要在两个单独的程序中运行它们并通过 python 包装器管理彼此依赖的操作?

Obviously I cant run both devices from one program directly but I need a way to address them depending on each other - for example: device 1 waiting for device 2 to finish some job. Is there any way to circumvent this issue or will I need to run them in two separate programs and manage actions depending on each other through the python wrapper?

推荐答案

在 64 位 Windows 上,64 位进程不能使用 32 位 DLL,32 位进程不能使用 64 位 DLL.Microsoft 已记录:

On 64-bit Windows 64-bit processes can not use 32-bit DLLs and 32-bit processes can't use 64-bit DLLs. Microsoft has documented this:

在 64 位 Windows 上,64 位进程无法加载 32 位动态链接库 (DLL).此外,32 位进程无法加载 64 位 DLL.

On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link library (DLL). Additionally, a 32-bit process cannot load a 64-bit DLL.

您需要一个与 32 位 DLL 通信的 32 位进程和一个 64 位进程与 64 位 DLL 通信.微软是这样说的:

You would need a 32-bit process that communicates with the 32-bit DLL and a 64-bit process to communicate with the 64-bit DLL. Microsoft says this:

但是,64 位 Windows 支持 64 位和 32 位进程(在同一台计算机上和跨计算机上)之间的远程过程调用 (RPC).

However, 64-bit Windows supports remote procedure calls (RPC) between 64-bit and 32-bit processes (both on the same computer and across computers).

问题就变成了如何让 Python 与这些进程通信的问题之一.需要某种形式的进程间通信 (IPC).微软在几十年前创造了一项可以做到这一点的技术 - 使用进程外 COM 服务器(进程外)的 COM 接口.

The problem then becomes one of how to have Python communicate with these processes. Some form of Interprocess Communication (IPC) would be needed. Microsoft created a technology decades ago that can do just that - COM interfaces using Out of Process COM servers (out-of-proc).

总体思路是:

  • 创建一个 64 位进程外 COM 服务器,用于包装(并公开)64 位 DLL 所需的方法和数据.
  • 创建一个 32 位进程外 COM 服务器,用于包装(并公开)32 位 DLL 所需的方法和数据.
  • 编写 32 位或 64 位客户端代码来实例化 COM 对象并调用它们的接口.Python 可以通过 win32com<用作 COM 客户端/a>

COM 在底层提供了一种 IPC 机制,允许 64 位客户端访问 64 位进程外 COM 服务器,并允许 64 位客户端访问 32 位进程外服务器.您甚至可以让 32 位客户端与 32 位和 64 位进程外 COM 服务器进行通信.

COM provides an IPC mechanism under the hood that allows a 64-bit client to access a 64-bit out-of-proc COM server and for a 64-bit client to access a 32-bit out-of-proc server. You can even have 32-bit clients communicate with 32-bit and 64-bit out-of-proc COM servers as well.

我没有使用较新的 MS 语言完成低级别的 Windows 工作.当我不得不按照你的问题做你需要的事情时,使编写 COM 服务器和 COM 接口变得容易的两种主要技术是:

I haven't done low level Windows work using the newer MS languages. When I had to do what you needed in your question the two main technologies that made it easy to write COM servers and COM interfaces were:

  • MSVC/C++ using Microsoft Foundation Classes (MFC)
  • MSVC/C++ using Active Template Library (ATL).

我有一个 偏好 用于 ATL,因为它不需要 MFC 库并且开销较小.

I had a preference for ATL since it didn't require the MFC library and had less overhead.

这篇关于在一个程序中结合 32 位和 64 位 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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