How to let a DLL know which application uses it

in

This question has been raised several times in the forum and as I had to implement such feature in one of our development here is a small tip that retrieves the name of the application that calls a DLL:

// Note: link against cone.lib, apparc.lib and eikcore.lib
#include <apparc.h>
#include <eikapp.h>
#include <eikappui.h>
#include <coemain.h>

/**
* Get the full path of the callin app
* (ex: "c:\system\myapp\myapp.app")
* @param aPath On exit, contains the full path to the application.
* The descriptor should be big enough to hold a full file name (i.e. 255 characters)
*/
EXPORT_C void MyDll::GetCallingAppPath(TDes& aPath)
{     aPath=((CEikAppUi*)CCoeEnv::Static()->AppUi())->Application()->DllName();
}

You can also use the function above to get the drive on which the DLL is installed (provided that the application and your DLL are part of the same package and installed on the same drive):

/**
* Get the drive on which the DLL is installed
* (It is assumed that the app and the DLL are part of the same
* SIS file and that the PKG specifies same drive for both)
* @param aDrive On exit, contains the full path to the application.
* The descriptor should be big enough to hold a full file name (i.e. 255 characters)
* Note: take care that this code is designed as this for simplicity
* but it uses 1KB of stack (512 bytes for the TFileName
* and a little bit more for the TParse object)
*/
EXPORT_C void MyDll::GetInstallationDrive(TDes& aDrive)
{    
 // Get the path of the calling application
 TFileName appPath;
 GetCallingAppPath(appPath);

 // Parse the app name to get the drive
 TParse parser;
 parser.Set(appPath,NULL,NULL);
 aDrive=parser.Drive();
}


> How to let a DLL know which application uses it

Nice trick, Eric! :)

But don't forget about that this trick won't work in a server, where CCoeAppUi() returns a lovely NULL.

> How to let a DLL know which application uses it

And I've just noticed that it won't work on SDK 3.0 either, where applications are not DLLs, but EXEs. Anyhow, one of the C...Application classes should have a relevant function that can be called for servers (i.e. EXEs), too.

> How to let a DLL know which application uses it

So I guess the basic idea is that if you tell the DLL who's calling, then the DLL can have different kind of services depending on who the caller is? That kind of solution sounds a bit awkward, doesn't it? :)

> How to let a DLL know which application uses it

Expressed like that, yes it is.... but this also allow a DLL to store different configurations for different applications that may use it (this is the case we currently have).