EugeneK 23 Posted yesterday 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 22 hours ago (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 22 hours ago by Remy Lebeau 1 Share this post Link to post
Vincent Parrett 843 Posted 12 hours ago 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 1 Share this post Link to post
Artem Razin 12 Posted 11 hours ago 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 7 hours ago (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 7 hours ago by dwrbudr 1 Share this post Link to post
EugeneK 23 Posted 4 hours ago 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 4 hours ago 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