Is it possible to access a function for a running application dll?

I am just getting into the deeper parts of creating dll's and I am wondering if it's possible to fire a external function in a dll for a running application?

Essentially I want to trigger a function in a running application without using CLI (I can't):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Application A (Managed c#)
[DllImport(DllFilePath, EntryPoint = "ExportImage")]
private extern static void DoExport(string path);

void ExportImage()
{  
    DoExport("MyfilePath");
}
}

//Application B
extern "C" __declspec(dllexport) void __cdecl ExportImage(void* path) {
    //Use path to open a file in this application when running.
  }


Any suggestions would be welcome.
Last edited on
You need to open the DLL with LoadLibrary() and get a pointer to the function with GetProcAddress(). Then you can cast the pointer to an appropriate function pointer type and call it.
1
2
3
4
5
6
7
8
auto module = LoadLibraryW(L"foo.dll");
//check for errors
auto entry_point = GetProcAddress(module, "ExportImage");
//check for errors

typedef void (*ExportImage_f)(void *);
auto ExportImage = (ExportImage_f)entry_point;
ExportImage("MyfilePath");
Sorry, I may need a little bit more of an explanation, and I may need to give you a little bit of info.

The application loads it's plugin on start of the application through a plugin main call, and I cannot change this because it passes a message object as a suite that funny enough I need to access... What I am looking to do is somehow get the address pointer to that instance of dll (From managed C# code) and call a function so I can use that plugins calls and assignments.

I would like to return the procedures address but I might be missing something I don't see or understand how I am able to call a function from an application running on the dll using your example. I am missing a large part of the GetProcAddress().

Thanks for the reply!
Last edited on
So your code resides in a DLL that's loaded as a plugin by the program, and you want to call into another plugin? If this is the case then what I said above is correct[1]. Why do you think it would not work?




[1]The other DLL may not work properly when called by someone other than the program that loaded it. Make sure to check that doing this is safe, if this is indeed what you're trying to do.
So just so we can both understand better I may need to clear some things up, there are two different applications I want to get the DLL instance of the application and fire off it's external method so I can access the assigned values contained in that instance;

Application A is written in C#, that contains the code to fire off Export image.

Application B is written in C++ and needs to have an external function to Open a document (And the OpenDocument()) is contained in the API itself and can "Only be accessed" with a message object that was passed to the .dll on start up of the application, that I am unable to call myself (This is why I don't think LoadLibraryW() works in my case, or I somehow need to get the memory pointer to load).

(I'm literally only able to access the application suite from a pointer that is passed to the PluginMain):
1
2
DLLExport SPAPI SPErr PluginMain(const char* caller, const char* selector, void* message){
message->GetSuite()->OpenDocument("FilePath.png")


I don't see how I am able to access GetProcAddress() from the managed C# code and I can't load the dll from Application A since it's already loaded and I need it to be loaded. I may be missing something, but I don't think this is totally right (This does not mean your wrong) but I don't think it works for this case.
Last edited on
Then there is no solution. Without knowing anything else about the other plugin, using the API the parent application gave you is the only safe way of calling into it.

I don't see how I am able to access GetProcAddress() from the managed C# code
Well, you would use P/Invoke, but for C# it's not really necessary because [DllImport] calls LoadLibrary() and GetProcAddress() for you.

I can't load the dll from Application A since it's already loaded and I need it to be loaded.
If the specified module is already loaded, LoadLibrary() returns the HMODULE for it, without loading it again.
For this case, it may still not be safe for your plugin to call into the other plugin, because it may contain some internal state that's only initialized when the module is loaded.


By the way, it's terribly confusing how you keep using the word "application" to refer to DLLs. An application is a complete, self-contained piece of software that fulfills some role. A plugin by definition needs some other software to do anything useful, so it cannot be an application.
"By the way, it's terribly confusing how you keep using the word "application" to refer to DLLs" When I use the term Application I literally mean the process of the application, Aplication A is running and triggering it's own events, it has no dependencies on a linked library. *I guess I can say Application B is the plugin* But I was referring of the instance of it in the running application (The instance of that plugin running on the application).

Thanks for all your help!
Last edited on
Topic archived. No new replies allowed.