0

I have C++ code in Visual Studio that calls some functions from the Windows API. This code is shared and I don't want to change it much, but I want to provide error logging if some Windows function returns an error. My plan was to define alternatives to all Windows functions in a separate source file in some namespace WinLog, and force the shared code to call my functions instead of the Windows API functions. All of the shared code includes the stdafx.h header, and if I add these lines in that header:

#include "WinLog.h"
using namespace WinLog;

then the shared code would call my functions. I don't care about using namespace in header files. But the problem is that, now the compiler reports compilation errors, for instance:

C2668: 'SCardReleaseContext': ambiguous call to overloaded function

How can I make the shared code call my functions if I only change something in stdafx.h?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • 1
    Rather than *wrapping* the API functions, requiring callers to call your functions directly instead of the original functions, have you considered *hooking* the API functions instead? Most Win32 API functions can be detoured, such as with the [MSDetours library](https://github.com/microsoft/Detours). That way, when callers call the original API functions, your functions will be called instead. – Remy Lebeau Feb 22 '23 at 18:48
  • 1
    *"How can I make the shared code call my functions"* - Compile your shim functions into a library and have the shared code link against your library rather than the exports from the Windows API. – IInspectable Feb 22 '23 at 18:48
  • Background information: [Understanding the classical model for linking: You can override an LIB with another LIB, and a LIB with an OBJ, but you can’t override an OBJ](https://devblogs.microsoft.com/oldnewthing/20130109-00/?p=5613). – IInspectable Feb 22 '23 at 21:15
  • https://stackoverflow.com/questions/2825960/wanted-winapi-calls-logger – Hans Passant Feb 22 '23 at 22:17
  • [(Windows) Monitoring API calls in C](https://stackoverflow.com/questions/720317/) – Remy Lebeau Feb 24 '23 at 00:31
  • Thanks all. I'll try Rohitab's API Monitor. Seems old, but functional and easy to use. And I don't have to recompile anything. – Dialecticus Feb 24 '23 at 08:36
  • If you want to observe the behavior on your *local* system, you can use [Event Tracing for Windows](https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/event-tracing-for-windows--etw-). If you need to debug an issue, but don't know exactly how/when/why it happens, [Time Travel Debugging](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/time-travel-debugging-overview) is a nice option. – IInspectable Feb 24 '23 at 13:22

0 Answers0