全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2164
推到 Plurk!
推到 Facebook!

.Net如何Hook元件的Method调用?

缺席
mustapha.wang
資深會員


發表:89
回覆:409
積分:274
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-11-01 21:26:24 IP:222.68.xxx.xxx 訂閱
.Net的dll没有了export的procedure,原来在win32下可以用Hook API来做的事情,在.Net下要如何实现?
如金山词霸的及时翻译,他Hook了user.dll的DrawText函数。
在.Net,如borland.delphi.system.pas里面的这个函数,在win32是调用windows的LoadStringA这个API,现在是使用.Net的ResourceManager.
function LoadResString(ID: string): string;
var
A: System.Reflection.Assembly;
R: System.Resources.ResourceManager;
newResMgr: Hashtable;
begin
if ResourceManagers = nil then
begin
{$IFNDEF CF}
if Assigned(VCLInitLocaleOverride) then
VCLInitLocaleOverride;
{$ENDIF}
ResourceManagers := Hashtable.Create;
end;
A := System.Reflection.Assembly.GetCallingAssembly;
R := System.Resources.ResourceManager(ResourceManagers[A]);
if R = nil then
begin
R := System.Resources.ResourceManager.Create('_ResourceStrings', A);
// threadsafe add to hash table.
// If multiple threads pass through here simultaneously, one of the
// hash table clones may be orphaned and will be GC'd later.
// The resource manager created in that case will be recreated on next request.
newResMgr := HashTable(ResourceManagers.Clone);
if not newResMgr.Contains(A) then
newResMgr.Add(A, R);
ResourceManagers := newResMgr;
end;
Result := R.GetString(ID);
end;
有没有办法拦截到delphi对ResourceManager.GetString函数的调用,然后改变它的返回结果?
网上查查资料,感觉有几个可能:
1、AOP,但感觉这个是设计模式,需要两方约定,如需要ResourceManager预先开放Attribut
2、用Profile API实现Method intercept,需要在外部配置Profile拦截器,Method拦截时只能在原Method执行前后加入代码,不能修改Method的传入值和返回的结果;
3、Hook GetJIT这个API,完全自己实现IL编译,会涉及很多没有公开的接口,感觉挺复杂。
------
江上何人初见月,江月何年初照人
mustapha.wang
資深會員


發表:89
回覆:409
積分:274
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-03-02 17:48:05 IP:222.68.xxx.xxx 訂閱
var
A:Assembly;
T:System.Type;
FBorlandResourceManagers:HashTable;
FResourceManagersFieldInfo:FieldInfo;
FAcropriseResourceManagers:HashTable;
RS:ResourceManager;
begin
A:=TypeInfo(Borland.Delphi.System.TResourceKeyStringAttribute).Assembly;
T:=A.GetType('Borland.Delphi.Units.System',True);
FResourceManagersFieldInfo:=T.GetField('ResourceManagers',BindingFlags.Static or BindingFlags.NonPublic);

FBorlandResourceManagers:=HashTable(FResourceManagersFieldInfo.GetValue(nil));
FAcropriseResourceManagers:=HashTable.Create;
RS:=TAcropriseResourceManager.Create('_ResourceStrings',A);
FAcropriseResourceManagers.Add(A,RS);
FResourceManagersFieldInfo.SetValue(nil,FAcropriseResourceManagers);
end;

function TAcropriseResourceManager.GetString(AID: string): string;
begin
Result:=inherited GetString(AID);
Result:=ResourceStorage.GetDisplayValue(Result);
end;

------
江上何人初见月,江月何年初照人
系統時間:2024-04-28 8:34:19
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!