Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++ - Linking plugin DLL against EXE?

I'm in the process of porting a large C++ application from Linux (gcc) to Windows (Visual C++ 2008) and am having linker issues with plugins. On Linux this wasn't an issue, as .so supports runtime symbol lookup, but dll does not seem to support this.

Some background information: The application (the host), which hosts a scripting environment, provides interfaces to plugins (shared libraries that are loaded at runtime by script API calls), allowing the host and the scripting API to be extended without recompiling the host application. On Linux this is just a matter of including the host application's headers in the plugin source, but on Windows I'm receiving linker errors. I'm not sure exactly what I need to link with for Visual C++ to resolve these symbols.

One of our dependencies (open source, LGPL) has preprocessor declarations that it uses to insert __declspec(dllexport) and __declspec(dllimport) into it's headers. Some prior research indicates that I may have to do this as well, but I'd like to be sure before I go modifying a whole bunch of core headers. (I was previously able to get this working on MinGW, but we've decided that supporting Visual Studio is a requirement for this sort of commercial project.)

My question, in a nutshell: How do I link runtime-loaded dlls against a host exe in Visual C++?

Edit: To clarify the problem with an example, I have a class in my host application, Object, which represents the base type of an object that may be accessed by a script. In my plugins I have a number of classes which extend Object to perform other functions, such as integrating networking support or new visual elements. This means that my dll must link with symbols in the host exe, and I am not sure how to do that.

like image 870
Sean Edwards Avatar asked Apr 20 '09 22:04

Sean Edwards


People also ask

Can a exe link to DLL and static library at the same time?

Yes, the Core and Utils code will be duplicated.

How do I link a DLL in Visual Studio?

To create a DLL project in Visual Studio 2019At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Library. From the filtered list of project types, select Dynamic-link Library (DLL), and then choose Next.

Is DllMain required?

The DllMain function is an optional method of entry into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to LoadLibrary and FreeLibrary.

How do I reference a DLL in Visual Studio C++?

In Visual C++, click the Browse tab in the Add References dialog box. Click Browse, locate the component that you want on your local drive, and then click OK. The component is added to the Selected Components field.


2 Answers

What do you mean by "runtime symbol lookup"? Do you mean dynamically loading libraries using dlopen and dlsym and so on? The equivalents in Windows are called LoadLibrary and GetProcAddress.

In windows, you don't export symbols from a executable. You should only export them from a dll. The right way to solve your problem is to rearchitect so that the exported symbols are in a dll that the executable and other plugin dlls can link against.

like image 145
1800 INFORMATION Avatar answered Oct 11 '22 01:10

1800 INFORMATION


You can't, easily. The windows loader isn't designed to export symbols out of an EXE and bind them to symbols in a DLL.

One pattern that I have seen is the DLL export a certain function that the EXE calls. It takes as a parameter a structure which contains addresses of functions in the EXE for the DLL to call.

like image 37
Michael Avatar answered Oct 11 '22 01:10

Michael