How to replace WinAPI function calls in MS VC ++ project with my own implementation (name and parameters are set the same)?

I need to replace all WinAPI calls

  • CreateFile,
  • ReadFile
  • SetFilePointer,
  • Closehandle

with my own implementation (which uses low-level file reading via Bluetooth). The code where the functions will be replaced is Video File Player, and it already works with regular hdd files. It is also necessary that Video Player can still play files from the hard drive if the file at the VideoPlayer input is a regular hdd file.

What is the best practice for such a task?

+1
c ++ visual-c ++ winapi
source share
6 answers

I suggest you do the following:

  • Write a set of wrapper functions, such as MyCreateFile, MyReadFile, etc., that initially simply call the corresponding API and pass the same arguments together, unmodified.
  • Use a text editor to search for all calls to the original APIs and replace them with calls to new wrapper functions.
  • Verify that the application is still functioning correctly.
  • Change the wrapper functions to suit your own purposes.

Note that CreateFile is a macro that extends to CreateFileW or CreateFileA, depending on whether UNICODE is defined. Consider using LPCTSTR and TCHAR functions so that your application can be created as ANSI or Unicode.

Please do not use #define, as suggested in other answers here, as this will simply lead to maintenance problems, and, as Maximilian correctly points out, this is not the best practice.

+5
source share

You can simply write your new functions in a custom namespace. eg.

namespace Bluetooth { void CreateFile(/*params*/); void etc... } 

Then in your code the only thing you need to change:

 if (::CreateFile(...)) { } 

to

 if (Bluetooth::CreateFile(...)) { } 

Easy!:)

+2
source share

If you are trying to intercept calls to these APIs from another application, consider Detours .

+1
source share

If you can edit the code, you just have to rewrite it to use a custom API that does what you want. Otherwise, use the Maximilian technique, but be warned that this is a horror of service.

If you cannot edit the code, you can fix the import tables to redirect calls to your own code. A description of this technique can be found in this article - find the section titled "Espionage by Modifying the Import Address Table".

This is dangerous, but if you are careful, you can make it work. Also check out Microsoft Detours , which does the same thing, but does not require you to mess with the actual fix.

+1
source share

If you really want to capture the API, check out syringe.dll (L-GPL).

+1
source share

I don't think this is best practice, but it should work if you put it in an include file that is included wherever the function you want to change is called:

 #define CreateFile MyCreateFile HRESULT MyCreateFile(whatever the params are); 

The MyCreateFile implementation looks something like this:

 #undef CreateFile HRESULT MyCreateFile(NobodyCanRememberParamListsLikeThat params) { if (InputIsNormalFile()) CreateFile(params); else // do your thing } 

Basically, you make every CreateFile call with a MyCreateFile call, where you can decide whether you want to use your own implementation or the original one.

Disclaimer: I think doing it is ugly and I would not do it. I would prefer to search and replace all occurrences or something like that.

0
source share

All Articles