简体   繁体   中英

How to override a windows system function in C

We are developing a test simulator (in windows environment, using C language) for a software component that will be running on an automotive HW. Actually the test simulator is a windows application that include, as core sources, the sw componentm and allow to test the I/O interfaces of the core and in this way its correct behavior. Unfortunately, within the core is defined the function GetSystemTime that cause a conflict with a windows function of the same name:

[core] returnType GetSystemsTime(UInt32* time)
[windows] WINBASEAPI VOID WINAPI GetSystemTime (LPSYSTEMTIME lpSystemTime)

It is not possible to modify the core function, being part of the I/O interface of the component. I have read of the #define trick, that i have applied introducing a new core function GetSystemsTime:

#define GetSystemTime(x) GetSystemsTime(x)

But this trick is not sufficent, it allow to redirect all the function calls of the project towards the new function, but if i do not comment out the windows prototype i have the conflict. How i can do? Thx!!

Actually the test simulator is a windows application that include, as core sources, the sw components...

This may not be the perfect solution and I'm not even sure it will work, but:

  1. Do not include the source code into the test application. Just link compiled modules (but assume that is the case).

  2. Do not include windows headers in the components.

  3. Include only the component headers in your test application that you need, or even make a separate include file for testing with only the things (prototypes) you need.

  4. In the linker script, define the order of libraries.

The first three points should ensure there is no windows prototype in your component source code and so there can be no conflict.

With the last point you can ensure your functions are found first before the Windows functions.

If you use GetSystemsTime in your components and in your test application, you have a conflict and you must adapt the sources of the modules to be tested. Best if you would coordinate that with the designers.

Windows GetSystemTime is a macro that nowadays typically expands to GetSystemTimeW .

Can you #undefine GetSystemTime after including Windows headers, but before including the component's header files?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM