-->

Call .dll functions using Java

2019-08-20 11:28发布

问题:

I need to write an application for a client that calls a method from a ".dll" file. The ".dll" file was previously executed manually from an ".exe" GUI but now they want to automate the process.

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:

The interface is an ActiveX DLL which provides two functions (GetUnitInfo and SaveResult).

In the moment I just want to run the "GetUnitInfo" method from the Winwdows command line using RUNDLL32.exe.

This is the documentation for the "GetUnitInfo" method: The interface for GetUnitInfo is as follows:

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:

But this are all the functions, normally I would expected that also "GetUnitInfo" is listed.

Can somebody help? It is not mandatory to use 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.

回答1:

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. 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. To register a DLL:

C:\Windows\SysWOW64>regsvr32.exe "path to your DLL" for 32 bit DLL

Or

C:\Windows\System32>regsvr32.exe "path to your DLL" for 64 bit DLL

Also depending on your DLL type, 32 or 64 bit, you need to use proper Eclipse/JDK.