Difference between api and call back function - c++

Please help me understand the concepts of APIs and call back functions. As far as I know the following are the key points:
APIs are exposed by a 3rd party application (callee), so that the application using the 3rd party (caller), can use the exposed APIs to communicate to callee. Here IPC is used to exchange information between the two processes. They run in different process address space.
Call back functions are exposed by a 3rd party application (callee), so that the application using the 3rd party (caller) can be made aware of any event that have taken place in calee and the caller needs to take some action based on the same. This is generally achieved using function pointers. They run in the same process address space.
Please correct me if I am wrong, and also add your valuable points regarding the same.

is orange. API is a very generic term, related more to architecture, or design. You have to make a difference language specific API (i.e. C API, python API) and Web API (REST, SOAP).
is apple. Is a a SOA Pattern to allow asynchronous communication.
read more: #design-pattern-callback

An api of a 3rd party library is used by an application to perform action as described by the API and that api would mostly return a value to indicate a success or failure or some times error code of the failure to the caller.
eg: createFile()
A call back function is a mechanism by which the application would try to register a function to the 3rd party library using function pointers in order to get notified asynchronously when ever an specific event occurs.
eg: if our application has to pop up to the end user on low battery,we register a function to the os framework to call our function whenever the system battery is low.
when this happens we can implement some power saving routines or a popup to a user to warn on low battery in our callback function.

Related

c++ hooking ws2_32.dll recv

I am trying to learn hooking and want to hook only an .exe's send/recv function.
I'm building the project as a .dll and then injecting it to the .exe
Edit: solved
There are 3 ways of hooking an API call as far as I know:
Inject a DLL in the application that will rewrite the Import Address Table containing the address of the API call, so that the application calls your function instead;
Write a dummy DLL with same name of the DLL with the API call you want to hook and place it in the applications's root directory, so it will load your APIs instead of the system's;
Detour the API call by rewriting it's code with a JMP yourfunc or something with similar effect.
Method 1 is pretty popular one, it's even described in the Wikipedia page about Hooking and in various examples if you Google it, like this one, or this one.
Method 2 is a bit tricky, you have to build a DLL with the same name and exports as the one you're mimicking, and bypass all the functions you're not interested in hooking and write custom code for the one you are. I find this method very clean because you don't have to modify memory, you don't have to explicitly inject this DLL using an external program, Windows just does it for you, and with a plus, it generally fly under the radar of anti-debug and anti-hack detection. Here is an example of how to do that (32-bit).
Method 3 is Microsoft's favorite. It has a particularly good advantage: You can hook any and every function, method, or virtual calls. It doesn't depend on the function being called externally to hook it, so it's very popular to hook DirectX methods for instance. This is the method used by FRAPS, Discord Overlay, Overwolf Overlay and pretty much every other software that either places an overlay in games or records gameplay. You don't need to use Microsoft Detours specifically, there's the generic alternative aswell.

controlling an application via c++ code

I am writing an application in C++ in windows, that has a UI (WxWidgets) and user normally use the application via its UI.
Now I have a new requirement, the application needs to start and controlled by another application.
I can not develop a DLL or similar solutions.
I have access to my code (apparently!) and the other applications is developed by other users, but I can give them details on how to control my application.
My question is: How can I allow other applications to control my application via a defined interface?
For simplicity assume that I developed a calculator (has UI) and I want to give other application to do math on my application (for example they may ask my application to add two numbers and so on, As the math is very time consuming, I need to inform them about progress and any error that generate during processing.
Can I open a pipe to communicate?
Any other way to achieve this?
You can use pipes or tcp/sockets with a custom protocol, but probably it's better if you split your application in two parts:
One part that does the computation
The user interface
and publish the first one as an http server responding to JSON requests.
Using a standard protocol can ease up testing and increases interoperability (you can also probably leverage already existing libraries for both implementing the server and the JSON marshalling).
Note that in addition to accepting commands, any error message you are going to show for example in a message box or any other nested event loop like dialog boxes need to be rewired properly; this can be very problematic if message or dialog box come up as the result of calls to external code that you didn't write yourself.
This is the typical change that would have costed 10 if done early and that will cost 1000 now.

intercept messages between VBA applications

I have an application developed in VB 6.0. I don't have access to its code. This application also exposes its functionality through certain API provided in its dlls. Is there a way for me to check what methods of the API the consumers of this application's API are calling across anywhere the API is deployed. I want a C# program to just sit in that target environment and intercept the calls made to that API and report it back to my service via a service. I wont be modifying the API or the code calling the API. Is this possible in C# or would I need to go with C++?
Update
Lets say for sake of simplicity, that its a simple VB application developed in VB 6 called SimpleAPP, and it has a button that displays records in a grid. It does this by calling a component CMPA.dll with a public method GetRecords(string ID) which returns an Array of records. I have another few applications called CustomerApp.exe and AnotherCustomerApp.exe which also have a reference to CMPA.dll and they both calls this same method to get the records. Now, I want to develop a program called Interceptor.exe that will actually sit in the environment where CustomerApp and AnotherCustomerApp is deployed and will log internally which of these two applications called that CMPA dll's public method GetRecords and also log what parameter it sent in and what results were retrieved.
I had to google to find the library that was on the tip of my tongue.
That googling turned up some interesting articles: a new to me 1999 Microsoft Research article called “Intercepting and Instrumenting COM Applications” and an Microsoft Systems Journal article from january 1999 that I do remember, “Building a Lightweight COM Interception Framework”.
The library you want is probably Microsoft Detours. I have only used it from C++, not from C#, and I have only used it for intercepting calls to Windows API functions, not COM methods, so I can’t guarantee that it’s well suited. But it's not exactly rocket science to interface these two languages, if needed.
If Detours doesn’t turn out to fill your needs, then look at the articles cited. Quite possibly they resulted in some framework you can use. And otherwise they have the information you need to build your own. You might then also check out if ParkPlace ever made what you want. There was once great interest in “cross concern“ functionality, and ParcPlace did some of the most interesting research, as I recall.

How to get my IHttpNegotiate implementation called by IWebBrowser?

I use cwebpage_src code and I need to update some HTTP request headers while clicking on links. As I understand it can be done with self implementation of IHttpNegotiate->BeginTransaction. But how to get my IHttpNegotiate implementation called??
Thanks!
Although I have no experience of writing one, I believe that you need to write an asynchronous pluggable protocol, as recommended in this thread.
Details of how and why to do this are scattered around the web in various places, but the best exposition that I've read is in this post by Igor Tandetnik (abridged here for brevity):
There are several technology layers
that support the download and
navigation in Internet Explorer and
WebBrowser control. At the top, there
is WebBrowser itself and MSHTML object
that provides HTML parsing and
rendering. The client uses such
interfaces as IWebBrowser2 and
IHTMLDocument2 to communicate with
these high-level objects.
WebBrowser and MSHTML use URL Monikers
library to perform actual downloads.
URLMon exposes its services via
IMoniker and IBinding interfaces, and
the client (say MSHTML) implements
IBindStatusCallback and a number of
associated interfaces, e.g.
IHttpNegotiate or IAuthenticate.
Next down is an Asynchronous Pluggable
Protocol handler. An APP encapsulates
the details of a particular protocol,
such as http, file or res.
...
Most of the time, an application
hosting a WebBrowser control (or a BHO
running inside IE) uses high-level
services provided by WebBrowser and
MSHTML objects. However, sometimes
these services are insufficient, and a
lower-level hook is required.
...
It would be nice to be able to hook
into the communication sequence
between WebBrowser/MSHTML and URL
Monikers. Unfortunately, there does
not seem to be any way to do that - at
least, none that I know of. So, we
look at the next level - a
communication between a URL moniker
and an APP.
...
Now, it is rarely necessary to
implement a full-blown APP from
scratch - after all, how often do new
protocols actually get defined? But
for our purposes, it is useful to
implement a so-called passthrough APP
(pAPP). A pApp is an object that
implements both sides of URL
moniker-to-APP communication, that is,
it implements both IInternetProtocol
and IInternetProtocolSink /
IInternetBindInfo. We register it as a
temporary handler for a standard
protocol, such as HTTP. Now whenever
an HTTP request needs to be sent, URL
moniker will create an instance of our
pAPP and ask it to do the job. The
pAPP then creates an instance of a
standard APP for the protocol in
question (I call it a target APP, or
tAPP, but be aware that I've invented
the terminology myself, it's not
widely accepted, suggestions for a
better naming convention are welcome)
and acts as its client. At this point,
our pAPP becomes a proverbial
man-in-the-middle. In the simplest
case, any method call made by URL
Moniker on pAPP is forwarded to tAPP,
and any method call made by tAPP on
pAPP is forwarded back to URL Moniker.
The pAPP gets to observe and, if
desired, modify every bit of
information relevant to this request
passing back and forth between the
moniker and the tAPP.
Igor has a couple of sample projects that should help in writing your own pAPP:
PassthruApp.zip
PassthruAppBeta.zip

How can I communicate between two C++ MFC plugins?

I have a plugin for a c++ MFC app. I'm working with the developer of another plugin for the same app, that's trying to get notifications of events in my code. Both plugins are in the form of c++ dlls.
How can I pass messages from my plugin to his plugin? The solution needs to be robust to mismatched versions of our two plugins, as well as the host app. The notifications are during control point movement, so several times a second.
I could set up a callback mechanism, where upon load his plugin calls a function in my plugin with a function pointer. We're not guaranteed any loading order, but we could probably just check periodically.
I know Win32 has a messaging system, but I'm not sure how it works, really. We could add a hook, and I could send messages, but I'm a bit fuzzy on how we'd synchronize what the message id is, or any details other than what I said, really.
Any other ideas on how to do this?
I'm a bit fuzzy on how we'd synchronize what the message id
Use the RegisterWindowMessage API.
Take a look at this article here, it shows the available IPC mechanisms in windows. I might try COM, Mailslots, Pipes or Shared Memory (file mapping) in your case, in addition to windows messages which you already mentioned.