Jump to content
EugeneK

Intercepting UuidCreate function

Recommended Posts

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

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 by Remy Lebeau

Share this post


Link to post

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 

 

 

Share this post


Link to post

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
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 by dwrbudr

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×