问题
Scenario:
We're busy wrapping up a 3rd party's C++ SDK as a DLL to make it simpler for other developers in our organisation to integrate this functionality into their own apps (be it, .net, delphi, etc)
The underlying system sends Windows messages to signal events that occur in the system. These events need to be dealt with, as they could potentially signal the state of the system and what can be done next.
Question:
What would the best way be to handle these messages within the context of the approach we are taking (i.e a DLL that wraps up the 3rd party SDK)? Some ideas that come to mind:
- Let the application using the DLL trap the message, and then pass it back to the DLL via a function call for processing -- is it even possible for the calling application to trap these messages?
- Spawn a thread from within the DLL that implements a message pump that handles these messages from the underlying system and bubbles up our own custom messages?
All sample code given for the SDK uses a single Win32 app that implements a message pump and handles the messages within the context of the application.
Its been ages since I have done Windows development using native Win32 and would appreciate some advice.
回答1:
The easiest way is to make a proxy DLL. The basic idea is to replace the API DLL with your own, replace the functions you want to and forward the rest to the original API DLL. You rename the original DLL and put in your with the original name of the API DLL. To do this you need to export the functions. Microsoft has made this fairly simple using Visual Studio.
Here's how to export the functions: http://msdn.microsoft.com/en-us/library/z4zxe9k8%28v=VS.100%29.aspx
If you dont know the function calls of the API they are using, there is a utility called PE Explorer that can tell you all about the functions. It costs $229 for a single business license but it looks like it's worth it's money: http://pe-explorer.com/peexplorer-tour-function-view.htm
There are ways to replace the API calls in memory but it's complex, requires the API to already be active (unless you are closely monitoring the system) and may set off an antivirus programs.
来源:https://stackoverflow.com/questions/4517214/windows-messaging-trapping-calls-originating-from-another-api