<p>I need to replace all WinAPI calls of the</p>

<ul>
<li>CreateFile, </li>
<li>ReadFile, </li>
<li>SetFilePointer,</li>
<li>CloseHandle </li>
</ul>

<p>with my own implementation (which use low-level file reading via Bluetooth).
The code, where functions will be replaced, is Video File Player and it already works with  the regular hdd files.
It is also needed, that Video Player still can play files from HDD, if the file in the VideoPlayer input is a regular hdd file.</p>

<p>What is the best practice for such task?</p>

[{'answer_id': 60651, 'author': 'Maximilian', 'author_id': 1733, 'author_profile': 'https://Stackoverflow.com/users/1733', 'pm_score': 0, 'selected': False, 'text': "<p>I don't think this is best practice but it should work if you put it in an include file that's included everywhere the function you want to change is called:</p>\n\n<pre><code>#define CreateFile MyCreateFile\n\nHRESULT MyCreateFile(whatever the params are);\n</code></pre>\n\n<p>Implementation of MyCreateFile looks something like this:</p>\n\n<pre><code>#undef CreateFile\nHRESULT MyCreateFile(NobodyCanRememberParamListsLikeThat params)\n{\n    if (InputIsNormalFile())\n        CreateFile(params);\n    else\n        // do your thing\n}\n</code></pre>\n\n<p>You basically make every CreateFile call a MyCreateFile call where you can decide if you want need to use your own implementation or the orginal one.</p>\n\n<p>Disclaimer: I think doing this is ugly and I wouldn't do it. I'd rather search and replace all occurences or something.</p>\n"}, {'answer_id': 60663, 'author': 'denis phillips', 'author_id': 748, 'author_profile': 'https://Stackoverflow.com/users/748', 'pm_score': 1, 'selected': False, 'text': '<p>If you\'re trying to intercept calls to these APIs from another application, consider <a href="http://research.microsoft.com/sn/detours/" rel="nofollow noreferrer">Detours</a>.</p>\n'}, {'answer_id': 60671, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 1, 'selected': False, 'text': '<p>If you can edit the code, you should just re-write it to use a custom API that does what you want. Failing that, use Maximilian\'s technique, but be warned that it is a maintenance horror.</p>\n\n<p>If you cannot edit the code, you can patch the import tables to redirect calls to your own code. A description of this technique can be found in <a href="http://www.codeproject.com/KB/system/hooksys.aspx" rel="nofollow noreferrer">this article</a> - search for the section titled "Spying by altering of the Import Address Table".</p>\n\n<p>This is dangerous, but if you\'re careful you can make it work. Also check out <a href="http://research.microsoft.com/sn/detours/" rel="nofollow noreferrer">Microsoft Detours</a>, which does the same sort of thing but doesn\'t require you to mess around with the actual patching.</p>\n'}, {'answer_id': 60691, 'author': 'ChrisN', 'author_id': 3853, 'author_profile': 'https://Stackoverflow.com/users/3853', 'pm_score': 4, 'selected': True, 'text': '<p>I suggest that you follow these steps:</p>\n\n<ol>\n<li>Write a set of wrapper functions, e.g MyCreateFile, MyReadFile, etc, that initially just call the corresponding API and pass the same arguments along, unmodified.</li>\n<li>Use your text editor to search for all calls to the original APIs, and replace these with calls to your new wrapper functions.</li>\n<li>Test that the application still functions correctly.</li>\n<li>Modify the wrapper functions to suit your own purposes.</li>\n</ol>\n\n<p>Note that CreateFile is a macro which expands to either CreateFileW or CreateFileA, depending on whether UNICODE is defined. Consider using LPCTSTR and the <a href="http://msdn.microsoft.com/en-us/library/c426s321%28VS.80%29.aspx" rel="nofollow noreferrer">TCHAR functions</a> so that your application can be built as either ANSI or Unicode.</p>\n\n<p>Please don\'t use #define, as suggested in other responses here, as this will just lead to maintenance problems, and as Maximilian correctly points out, it\'s not a best-practice.</p>\n'}, {'answer_id': 62005, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 2, 'selected': False, 'text': '<p>You could just write your new functions in a custom namespace. e.g.</p>\n\n<pre><code>namespace Bluetooth\n{\n  void CreateFile(/*params*/);\n  void etc...\n}\n</code></pre>\n\n<p>Then in your code, the only thing you would have to change is:</p>\n\n<pre><code>if (::CreateFile(...))\n{\n}\n</code></pre>\n\n<p>to</p>\n\n<pre><code>if (Bluetooth::CreateFile(...))\n{\n}\n</code></pre>\n\n<p>Easy! :)</p>\n'}, {'answer_id': 92171, 'author': 'titanae', 'author_id': 2387, 'author_profile': 'https://Stackoverflow.com/users/2387', 'pm_score': 1, 'selected': False, 'text': '<p>If you really want to hijack the API, look at <a href="http://www.geocities.com/SiliconValley/1741/downloads/" rel="nofollow noreferrer">syringe.dll</a> (L-GPL).</p>\n'}]
['https://Stackoverflow.com/questions/60641', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2279145/']