My first approach to consuming RAPI was quite simple: a static link to rapi.lib. This meant that all the products were tied to a particular version of ActiveSync and would not run in another. By linking to a specific version's lib file we are actually precluding the possibility of linking to other versions, both older or newer.
It so happens that util recently all applciations were statically linked to ActiveSync 3.8, meaning that you had to install this particular version in order for the applications to run. Incidentally, I got a request from two different customers, one asking for ActiveSync 4.0 support, and the other was asking for version 3.6 support. Now what?
Publishing different versions of the applications was obviously out of the question. There had to be a better way of supporting mutiple versions of RAPI. Then I recalled something I read in one of Doug Boling's books: use dynamic linking. The idea is to dynamically load the RAPI.DLL file and retrieve the entry points to the functions you need by using GetProcAddress. To make things easier, I wrote a very simple helper class, of which I'm highlighting a few snippets.
First, the class declaration:
HRESULT InitEx(RAPIINIT* pRapiInit);
HRESULT Invoke(LPCWSTR pszDLL,
I'm only including a few methods to illustrate the technique. You should be able to extend your own version with the calls you need.
Let's look at the constructor and destructor:
m_hDll = LoadLibrary(_T("RAPI.DLL"));
As you can see, we dynamically load and unload the RAPI.DLL on the constructor and destructor. You should have no problems in nesting these - you will only be incrementing or decrementing the module load count. Now, let's see how to implement one of the methods:
typedef HRESULT (__stdcall *CERAPIINITEX)(RAPIINIT*);
HRESULT CRemoteAPI::InitEx(RAPIINIT *pRapiInit)
HRESULT hr = E_NOTIMPL;
hr = pfnCeRapiInitEx(pRapiInit);
You now get the idea on how to implement all other methods. The beauty of this is that by using this approach, you can support all versions of ActiveSync (provided that the target version does implement the function call you need...).