Search code examples
c++winapihippomocks

Call original/mocked function, from within mock


I'm trying to mock some WinAPI functions via hippomocks. How can I call the original/mocked WinAPI, from within the mock?

For example, if I mock CreateFile, the logic I would like to implement inside the mock which I register via:

MockRepository mocks;
mocks.OnCallFunc(::CreateFile).Do(MyCreateFileMock);

is to call the original API, and then log its output, before returning the result.

I tried to retrieve CreateFile address before calling the mock, and storing it in a global. When I call that global from within the mock I end up calling the mock again.


Solution

  • It seems that retrieving the WinAPI address via kernelbase.dll, as opposed to retrieving it from kernel32.dll gets around hippomocks hook -- kernelbase is where the actual implementation is.

    So retrieving the address by calling GetProcAddress(GetModuleHandleA("kerelbase.dll"), "<API_NAME>") allows me to invoke the API from within the mock without a problem.