Work around with Delphi DLL

半城伤御伤魂 提交于 2020-01-03 06:34:27

问题


I have two applications in Delphi for which I don't have any code source:

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.

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.


回答1:


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.

Here is a simple example

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.

And this program that uses it

program A;
{$apptype console}
function B_FUNCTION(value:integer): integer; external 'b.dll';
var i:integer;
begin
  i:=B_FUNCTION(2010);
  writeln(i);
end.

Compile both programs and run them. The result printed is 2011.

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.

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

Execute A, now it will spit 2012.




回答2:


It's not entirely obvious to me which parts are yours and what calls what, but you should be able to create your own intermediate DLL in Delphi with an interface that uses fastcall and which forwards the call to the real DLL using another calling convention.



来源:https://stackoverflow.com/questions/9606330/work-around-with-delphi-dll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!