PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Accessing Caller's routines from a DLL

I've got a DLL from which I wish to access routines residing in the process that loaded the DLL. So in other words, an application has a routine foo that I wish to be able to call from within a DLL that the application loaded. Note that I aready use pointers to functions to accomplish this. However I would like something more streamlined, like the ease of calling into DLLs normally.

It seems this should be possible because a DLL is loaded into the parent process' calling space. Also when statically linking DLLs a dummy LIB is used as a placeholder for the routines that are resolved dynamically when the application is loaded. It appears the only purpose in this is to keep the linker happy. Perhaps it would be possible to do something like that in reverse.

It should be possible to LoadLibrary and GetProcAddress the parent exe from the dll. However I'm right back at pointers-to-functions, so that is no better than my current solution.

One last thing is that I have full control over both the app and the dll, so I can make whatever modifications are needed to the application itself.

Any suggestions?

Thanks.

Dan East
You won't like it, but COM is probably the best solution. However I'm not familiar with COM/ActiveX on WinCE. Only the PC platform.

You can create your own COM system. Flipcode.com has had a few COTD submissions that have essentially done this.
COM is way overkill for this, and even if you did go through the headaches of us doing it this way it would still be calling through a function pointer. COM might be worthwhile if you're going to market your application as some reusable module as a COM server or something. Doesn't sound like Dan is doing that though.

Dan, I don't think what you want to do is possible, but if it were you'd need to use LIB to create an export file (.exp) for your .exe, that you would then use to link with when you built the DLL. I don't think you can use LIB on exes, so this might not even be worth investigating.

If you could isolate the code from the exe that would be called by the DLL, and then put that code into another DLL, then the two DLLs can call routines in each other. In order to build them though you'll have to use export files to resolve the circular references. This process is explained in the MSDN docs.

I don't know how much you can get away with in DllMain (there are strict restrictions on which APIs you can call in DllMain), but you might be able to call a few APIs to get the function address from the exe and initialize a function pointer residing in the DLL. The DLL would still have to call through a pointer, but your exe wouldn't have to do anything special - just load the DLL.

All of this is really too much trouble in my book unless you're trying to do this for some copy protection scheme. I'd just have the exe call a DLLInit() exported by the DLL, and pass over a table of function pointers to routines that the DLL will call back into.
Digby, thanks for the suggestions. There are two main reasons I'm doing this. First it prevents duplication of routines in each DLL. This is amplified when many small DLLs are used. Second and most important, if the routine resides in the EXE, and the DLLs are created by 3rd parties, then I can modify the implementation of the routine without having to try and get the authors of the DLLs to build new versions. I may just put these routines in a DLL that the app and other DLLs all can use as you suggested.

BTW, there is no way I would consider using COM for this. I would much rather write my own implementation completely from scratch.

Dan East
Easy! since the DLL is loaded into the same process you should be able to just pass function pointers. This also requires you to know the calling convention like _cdecl or _fastcall or _stdcall. If it is static stuff or compile time stuff just have the prog pass a struct of pointers to funcs and the call them off when needed.

A good example of where this hapens in everyday code is your WinProc functions you pass when you create a window.

... Oh yeah: you could also use IPC methods like clipboard or named pipes but these get about as messy as COM does so just stay simple (i also assume you are worried about it's performance)
Dan East · 06 Jan 2003, 20:34 UTC — Accessing Caller's routines from a DLL
Thanks, but take a closer look at the first paragraph of my post:

Dan East wrote:
Note that I aready use pointers to functions to accomplish this. However I would like something more streamlined, like the ease of calling into DLLs normally.


Dan East
oops... oh well for skimming.

well you could use function objects if you are using C++... they provide a nice way to store information about called functions... I agree then with the above: COM.

COM offers a high level of interop... does would this project benefit from user built functions?

If you want to get really slick you should export symbols from the exec file and use a proxy stub code... but that requires IDL and a minimal amount of speed loss.

Or... you could just stick to either function pointersw or functions objects (My choice). Well if you want to go further i suggest read up of generic programming as the methods used in generic programming are often easier and more efficient than overrated OOP (both together is pure power!!!).

a simple function object is as follows:

class YouFunctionObject : /* base function object if you want */
{
public:
/* return type */ operator( ) ( /* Argument list */ )
{
// you function goes here
}
};


the nifty thing about this is what you can pass in member vars (meta data etc...) and what you can do in the constructor. all you need to do now is build a small set of functions that transports these back and forth and decide what to do with them. a good example of this would be version checking or overloading functions w/o passing 2 pointers for each... there arre many more.

I hope you find this a little more helpfull than my 1st post.
Dan: simply create an export file (.exp) that exports "foo".

Then use GetProcAddress() on the .exe module handle as you would for a DLL.

__declspec(dllexport) might even work with the .exe, although I've never tested it.
Kzinti: Would you have to include your export file with the release? Not fully understanding what the .exp files contain that the .exes and .dlls don't. I'm guessing symbol to friendly name type info.
Oops, I meant a .def file to describe the exports.
well using export files is almost identical to func pointers... at least in my opinion.

I don't know really... I am not the expert either.

Personly win32 and winCE and Win* sucks. you should check out the cool prelinking stuff you can do on linux :D

well I think that a few shared headers and or some cool macros might help too.
Forget exporting functions from an exe. I tried it ended up with a "non executable format" error on the PPC, but compile and link works. But the PPC will not execute the stuff.

If you want to share functions use a dll, it's easy and simple.

Quantum, you telling that Linux allows exporting functions from one exe to another one (or dll) by just exporting and linking?
yes and more but that is off topic...

you can export from an exec (I am currently writing a program at my work that does just this)

you have be sure to specify correct target types and compile with the right compile flags.

Now i confess i have not yet tried this on the eVC++ compiler but if it is anywhere nere VC++ 7.0 or 6.0 then it should work (note it works for me on those 2 and will later try a comile with borland's 5.5 compiler).

and just so you know: you can also make a DLL act like and exec (a little more trickery involved).

I wouldn't be able to give an easy and short explanation to the procedure that I use to accomplish this but I recomend passing a structure to the dll and then calling either pointers of function objects (note that you could also have the function obj. passed by ref and have a member variable take care of pointers) hey you could even override entries in a v-table if you wanted with those pointers :D .
Quantum,
there is no question that you can call a function by comunicating a pointer from an exe to the dll at runtime.
The question is, incorporate a function in your dll declared extern (__declspec (dllimport) or such). Export (__declspec (dllexport) or such) that function from the exe and just link the .lib to your dll.
Again, all that works for eVC at compile/link time without any errors. But it will not run on the PPC, bad executable format. :(
i would say declare a type
just like WndProc.

you then need to do a one way call (exe->dll)

sending a struct of addresses that can be used. there is no need to export from the exec.

now to make an export work you should check the PE/COFF header to make sure eVC++ is correctly linking symbols. also check the documentation for any info.

now if you are insanely set on a dll or com like iface then you can go ahead and pass a pointer to an IUknown interface.