开发者

Linking to Windows API's PatchAPI

开发者 https://www.devze.com 2023-02-27 11:26 出处:网络
I would like to use Windows API\'s PatchAPI in order to apply some patches. Applying of patches is implemented in mspatcha.dll, which should be located in one\'s system32 folder.

I would like to use Windows API's PatchAPI in order to apply some patches. Applying of patches is implemented in mspatcha.dll, which should be located in one's system32 folder.

After reading in various places, such as their ref and googling, I have yet to find the right way to link to this DLL. I would like to link statically, dealing with LoadLibrary seems messy and sort of defeats the purpose of their patchapi.h header. Since I have found no .lib file I am 开发者_Go百科to link to, I created my own using the following commands:

1) dumpbin /exports C:\windows\system32\mspatcha.dll

2) Create a mspatcha.def file, write an "EXPORTS" line, followed by one line for each function name that appears in the output of dumpbin

3) lib /def:mspatcha.def /out:mspatcha.lib

Although I'm sure this is not the right way to statically link with patchapi, I have not found the right way to do so. Anyway, after following these steps and writing a simple testcase made of a single call to ApplyPatchToFileExA(), I still get a linker error on the symbol _ApplyPatchToFileA@16. Taking a look at the exported symbols of my newly created mspatcha.lib, it appears that the functions use the wrong name convention

D:\tmp\mspatcha>dumpbin /exports mspatcha.lib|find "ApplyPatchToFileExA"

             _ApplyPatchToFileExA

Unless I'm wrong, this indicates that the lib exported functions using cdecl whereas the dll is using stdcall (or at least is declaring the function as _stdcall). See: C name decoration in Microsoft Windows.

My questions are: what is the right way to use mspatcha.dll in my application and what was wrong with my process of creating a lib from a dll so I can do static linking?

The detailed output of my terminal can be found here: http://pastebin.com/q4FV4Se6


As a point of terminology you are attempting to link implicitly rather than statically which means something quite different.

If you really can't get hold of a .lib file then one easy way to generate one is to create a dummy DLL with empty stubs for each function. Call the DLL mspatcha.dll. Make sure you use a .def file and stdcall.

When you've built the DLL, throw it away but keep the .lib file!

I've done this in the past to generate .lib files for DLLs built with tool chains that don't emit .lib files.

The technique that you used only works for cdecl functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号