EugeneK 23 Posted Thursday at 08:26 PM I'm using DDetours to intercept Windows functions for unit testing purposes, but don't know how to intercept UuidCreate function because its declaration is hidden in implementation section of System.SysUtils unit. Is there a way to do it? Share this post Link to post
Remy Lebeau 1602 Posted Thursday at 10:05 PM (edited) UuidCreate() is a Win32 API function, not a Delphi function. If you can't reach Delphi's declaration, then just make your own declaration in your own code. There is only one physical function to detour (residing in rpcrt4.dll), it doesn't matter how many declarations there are to reach it. Edited Thursday at 10:07 PM by Remy Lebeau 1 Share this post Link to post
Vincent Parrett 844 Posted Friday at 08:12 AM Intercepting functions for unit testing is a terrible idea. A better option would be to create abstractions and a concrete implementation (ie actually calls UuidCreate), that abstraction can be easily mocked using Delphi Mocks or Spring4D for uinit tests. The same applies to code that relies on things like Now or NowUTC - e.g - https://github.com/VSoftTechnologies/VSoft.System.TimeProvider 2 Share this post Link to post
Artem Razin 12 Posted Friday at 09:40 AM UuidCreate is exported from rpcrt4.dll: function UuidCreate(out guid: TGUID): Longint; stdcall; external 'rpcrt4.dll' name 'UuidCreate' delayed; However, for testing purposes, I would use a custom implementation that returns a predefined GUID. Share this post Link to post
dwrbudr 9 Posted Friday at 12:59 PM (edited) function Detour_UuidCreate(out guid: TGUID): Longint; stdcall; begin guid := Default(TGUID); Result := 0; end; procedure TForm68.Button1Click(Sender: TObject); var myguid: TGUID; begin InterceptCreate('rpcrt4.dll', 'UuidCreate', @Detour_UuidCreate); CreateGUID(myguid); end; So the above code does not work? On my side it works if I put a breakpoint in Detour_UuidCreate Edited Friday at 01:05 PM by dwrbudr 1 Share this post Link to post
EugeneK 23 Posted Friday at 04:10 PM 7 hours ago, Vincent Parrett said: Intercepting functions for unit testing is a terrible idea. A better option would be to create abstractions and a concrete implementation (ie actually calls UuidCreate), that abstraction can be easily mocked using Delphi Mocks or Spring4D for uinit tests. The same applies to code that relies on things like Now or NowUTC - e.g - https://github.com/VSoftTechnologies/VSoft.System.TimeProvider I mostly use abstractions, but using abstractions for Now feels like an overkill, that's the only one I use DDetours for so far. Also until Spring4d uses namespaces I'm not using it. Share this post Link to post
EugeneK 23 Posted Friday at 04:14 PM 3 hours ago, dwrbudr said: function Detour_UuidCreate(out guid: TGUID): Longint; stdcall; begin guid := Default(TGUID); Result := 0; end; procedure TForm68.Button1Click(Sender: TObject); var myguid: TGUID; begin InterceptCreate('rpcrt4.dll', 'UuidCreate', @Detour_UuidCreate); CreateGUID(myguid); end; So the above code does not work? On my side it works if I put a breakpoint in Detour_UuidCreate Thanks! I did not know that you can use this form of InterceptCreate Share this post Link to post