将 DLL 与 PHP 结合使用(傻瓜式教程)

发布于 2024-12-22 10:50:55 字数 1412 浏览 1 评论 0原文

我有一个项目需要使用 PHP 访问 DLL。服务器是Windows机器,Apache服务器由XAMPP提供。

我在网上阅读了多个答案,例如

这是我在 HTA / Javascript:

有人有工作示例吗?

这是我迄今为止在 PHP 中尝试的内容:

$obj = new COM('pathTo.dll');

有关 DLL 的信息:

  1. 已编译使用Delphi
  2. (当然)是自制的,
  3. 当我尝试使用regsvr32注册DLL时,出现以下错误找不到DllRegister服务器入口点

它可以使用吗无需注册与 regsvr32 一起使用吗?

I have a project that needs to access a DLL with PHP. The server is a Windows machine and the Apache server is provided by XAMPP.

I read multiple answers on the web like

Here is how I call the DLL in HTA / Javascript:

<object style="display:none" id="SOME_ID" classid="clsid:SOME_CLASS_ID" codebase="./somePath.dll"></object>

Does someone have a working example?

Here is what I tried so far in PHP:

$obj = new COM('pathTo.dll');

Information on the DLL:

  1. Compiled using Delphi
  2. It is (of course) home made
  3. I get the following error the DllRegister Server entry point was not found when I try to register the DLL with regsvr32

Can it be used without registering it with regsvr32?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋千易 2024-12-29 10:50:55

创建 DLL 文件时,需要使用 模块定义文件。它将包含与此类似的内容:

;
;contains the list of functions that are being exported from this DLL
;

DESCRIPTION     "Simple COM object"

EXPORTS
                DllGetClassObject       PRIVATE
                DllCanUnloadNow         PRIVATE
                DllRegisterServer       PRIVATE
                DllUnregisterServer     PRIVATE

该定义允许 regsvr32 查找 DllRegisterServer 入口点。

您可以尝试的另一个选项是将 /n 标志传递给 regsvr32。

Regsvr32 [/u] [/n] [/i[:cmdline]] dllname

/u - 取消注册服务器

/i - 调用 DllInstall 并向其传递可选的 [cmdline];与 /u 一起使用时调用 dll 卸载

/n - 不调用 DllRegisterServer;此选项必须与 /i 一起使用

/s – 静音;不显示消息框(Windows XP 和 Windows Vista 中添加)

最终,在尝试使 DLL 与 PHP 一起工作之前,您需要确保 DLL 能够正常工作。

When you create your DLL file, you need to use a module definition file. It will contain something similar to this:

;
;contains the list of functions that are being exported from this DLL
;

DESCRIPTION     "Simple COM object"

EXPORTS
                DllGetClassObject       PRIVATE
                DllCanUnloadNow         PRIVATE
                DllRegisterServer       PRIVATE
                DllUnregisterServer     PRIVATE

That definition allows regsvr32 to find the DllRegisterServer entry point.

Another option you can try is to pass the /n flag to regsvr32.

Regsvr32 [/u] [/n] [/i[:cmdline]] dllname

/u - Unregister server

/i - Call DllInstall passing it an optional [cmdline]; when used with /u calls dll uninstall

/n - do not call DllRegisterServer; this option must be used with /i

/s – Silent; display no message boxes (added with Windows XP and Windows Vista)

Ultimately, before you try to make a DLL work with PHP, you need to be sure your DLL works in general.

独孤求败 2024-12-29 10:50:55

随着 PHP>=7.4.0 的新 FFI/Foreign Function Interface (当这个问题发布时,它还不存在),现在比以前更容易了!例如,调用 GetCurrentProcessId(); kernel32.dll 中的 函数:

<?php

declare(strict_types=1);
$ffi = FFI::cdef(
    'unsigned long GetCurrentProcessId(void);',
    "C:\\windows\\system32\\kernel32.dll"
);
var_dump($ffi->GetCurrentProcessId());

输出

C:\PHP>php test.php
int(24200)

:)

With PHP>=7.4.0's new FFI/Foreign Function Interface (which didn't exist yet when this question was posted), this is now easier than ever before! For example, to call the GetCurrentProcessId(); function from kernel32.dll:

<?php

declare(strict_types=1);
$ffi = FFI::cdef(
    'unsigned long GetCurrentProcessId(void);',
    "C:\\windows\\system32\\kernel32.dll"
);
var_dump($ffi->GetCurrentProcessId());

outputs

C:\PHP>php test.php
int(24200)

:)

╰◇生如夏花灿烂 2024-12-29 10:50:55

我遇到了同样的问题,我修复了一些步骤:

  1. 以管理员权限打开命令行(windows + r + 输入“cmd”)
    在 DLL 文件所在的路径中写入:
    C:\Windows\system32\regsvr32 xwizards.dll(示例)
    出现一个窗口,显示“DLLRegisterServer success”
  2. 如果您是com_dotnet,请检查您的 phpinfo() 扩展
  3. 现在写入您的 PHP 代码:

    <前><代码>尝试{
    $dll = new COM('.'); //NameOfDllFile 不带扩展名“.dll”
    $dll->函数();
    } catch(异常$e){
    回显'错误:'。 $e->getMessage(), "\n";}

    现在,如果您知道如何管理 dll 的类和函数,那就没问题了,但是屏幕上不应该显示任何错误消息

如果我不清楚,请告诉我,下次我会尽力而为: )

I had the same problem and i fixed some steps :

  1. open the command line in administrator right (windows + r + type 'cmd')
    write the PATH where you're your dll file:
    C:\Windows\system32\regsvr32 xwizards.dll(it's example)
    a window show up with "DLLRegisterServer success"
  2. check your phpinfo() if you're com_dotnet extension
  3. now write into your PHP code :

        try    {
      $dll = new COM('<theNameOfDllFile>.<NameOfTheClass>'); //without extension '.dll' for theNameOfDllFile
      $dll->Function(); 
      } catch(Exception $e){
        echo 'error: ' . $e->getMessage(), "\n";}
    

    Now if you know how manage the class and the function of you're dll it's going ok,however no error massage should show up on your screen

If i wasn't clear let me know and i'll do my best the next time :)

七度光 2024-12-29 10:50:55

无法从 Linux/Apache 服务器访问 DLL。因此该项目被放弃。

A DLL cannot be access from Linux/Apache server. Therefore the project was drop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文