简体   繁体   English

使用Java调用.dll函数

[英]Call .dll functions using Java

I need to write an application for a client that calls a method from a ".dll" file. 我需要为从“ .dll”文件调用方法的客户端编写应用程序。 The ".dll" file was previously executed manually from an ".exe" GUI but now they want to automate the process. 以前是通过“ .exe” GUI手动执行“ .dll”文件的,但现在他们希望使该过程自动化。

I never worked with .dll files so everything that I found until now is the result of a complete day of research, I also received a small documentation with this tool: 我从未使用过.dll文件,因此直到现在为止发现的所有内容都是经过一整天研究的结果,我还收到了有关此工具的小型文档:

The interface is an ActiveX DLL which provides two functions (GetUnitInfo and SaveResult). 该接口是ActiveX DLL,它提供两个功能(GetUnitInfo和SaveResult)。

In the moment I just want to run the "GetUnitInfo" method from the Winwdows command line using RUNDLL32.exe. 目前,我只想使用RUNDLL32.exe从Winwdows命令行运行“ GetUnitInfo”方法。

This is the documentation for the "GetUnitInfo" method: The interface for GetUnitInfo is as follows: 这是“ GetUnitInfo”方法的文档: GetUnitInfo的接口如下:

Public Function GetUnitInfo( _
ByVal strRequest As String, _
ByRef strUnitInfo As String,
Optional ByVal strStationName As String = "") As Long

Sample calling code can be: 示例调用代码可以是:

Dim lRet As Long
    Dim strXML as String
    lRet = GetUnitInfo( _“<?xml version=""1.0"" ?><GetUnitInfo 
    xmlns=""urn:GetUnitInfo-schema"" SerialNumber=""BD3ZZTC8MA"" />",  strXML)

So I tried to run this method with some dummy parameters because the method returns an error if the parameters are not OK. 因此,我尝试使用一些虚拟参数运行此方法,因为如果参数不正确,该方法将返回错误。 The command: 命令:

RUNDLL32.EXE FFTester.dll, GetUnitInfo test1, test2

But I receive this error: 但我收到此错误:

在此处输入图片说明

I used "Dependency Walker" to list the functions from the dll file: 我使用“ Dependency Walker”列出了dll文件中的功能: 在此处输入图片说明

But this are all the functions, normally I would expected that also "GetUnitInfo" is listed. 但这是所有功能,通常我希望还会列出“ GetUnitInfo”。

Can somebody help? 有人可以帮忙吗? It is not mandatory to use RUNDLL32. 使用RUNDLL32不是强制性的。

Later edit: I want to call this DLL from a tool that is written in JAVA, I tried to use JNA but I failed so I was thinking to call the dll functions from the command line because if this works I can use a process builder to execute the command. 以后的编辑:我想从用JAVA编写的工具中调用此DLL,我尝试使用JNA,但失败了,所以我想从命令行调用dll函数,因为如果这可行,我可以使用进程生成器来执行命令。

I fixed my problem and I will provide a solution, maybe it will help someone else. 我已解决问题,将提供解决方案,也许会帮助其他人。

I used com4j library to generate the interfaces for my dll. 我使用com4j库为我的dll生成接口。 After this you need to register your DLL otherwise most problely your code will throw an "ComException", you can read more in my second question . 之后,您需要注册DLL,否则最有可能您的代码将抛出“ ComException”,您可以在第二个问题中阅读更多内容。 To register a DLL: 要注册DLL:

C:\\Windows\\SysWOW64>regsvr32.exe "path to your DLL" for 32 bit DLL C:\\ Windows \\ SysWOW64> regsvr32.exe“ DLL的路径”(用于32位DLL)

Or 要么

C:\\Windows\\System32>regsvr32.exe "path to your DLL" for 64 bit DLL C:\\ Windows \\ System32> regsvr32.exe 64位DLL的“ DLL路径”

Also depending on your DLL type, 32 or 64 bit, you need to use proper Eclipse/JDK. 另外,根据您的DLL类型(32或64位),您需要使用适当的Eclipse / JDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM