问题描述
我在Delphi中有两个应用程序,但是我没有任何代码源:
I have two applications in Delphi for which I don't have any code source:
我使用应用程序A的接口从应用程序B调用DLL文件例如,我通常从接口A传递服务号200011来调用DLL文件B以返回值。但是,最近应用程序A更改了变量。我必须添加P00200011才能调用DLL文件B。
I use an interface from application A to call an DLL file from application B. Example, I usually pass a service number, 200011, from interface A to call DLL file B for a value return. But, recently the application A have changed the variable. I have to add P00200011 to call DLL file B.
我尝试创建DLL C#,但是B中的DLL是使用约定,我无法更改此DLL文件。
I have tried to create an DLL C#, but the DLL in B is created with the fastcall convention and I cannot change this DLL file.
我还有其他方法可以做到吗?我没主意。
What are others ways I can do it? I'm out of ideas.
推荐答案
您需要编写包装DLL。使用要拦截的函数构建DLL,然后在代码中简单地加载并调用原始DLL。然后,将包装器放置在应用程序的同一目录中。来自应用程序的所有调用都将转到包装DLL,再到原始DLL。
You need to write a wrapper DLL. You build your DLL with the functions you want to intercept, and in your code you simply load and call the original DLL. Then you place your wrapper in the same directory of your application. All calls from the application will go to your wrapper DLL and from there to the original DLL.
这里是一个简单的示例
假设您拥有此库(B.DLL)
supose you have this library (B.DLL)
library B;
function B_FUNCTION(value:integer): integer; export;
begin
result:=value+1;
end;
exports B_FUNCTION;
end.
使用该程序的程序
program A;
{$apptype console}
function B_FUNCTION(value:integer): integer; external 'b.dll';
var i:integer;
begin
i:=B_FUNCTION(2010);
writeln(i);
end.
编译两个程序并运行它们。打印的结果是2011年。
Compile both programs and run them. The result printed is 2011.
现在,对包装DLL进行编码
Now, code your wrapper DLL
library w;
uses windows;
function B_FUNCTION(value:integer): integer; export;
var
adll: Thandle;
afunc: function(v:integer):integer;
begin
adll:=LoadLibrary('TRUE_B.DLL');
afunc:= GetProcAddress(adll,'B_FUNCTION');
result:=afunc(value+1);
FreeLibrary(adll);
end;
exports B_FUNCTION;
end.
构建它,您将拥有A.EXE,B.DLL和W.DLL。替换它们
Build it, you'll have A.EXE, B.DLL and W.DLL. Replace them
REN B.DLL TRUE_B.DLL
REN W.DLL B.DLL
执行A,现在它将吐出2012。
Execute A, now it will spit 2012.
这篇关于使用Delphi DLL解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!