question

yanpeng avatar image
0 Votes"
yanpeng asked yanpeng edited

Is it possible for a process to load two dll with different versions?

Let me explain it further. I am facing a tough problem for me. I developed an add-in project which is a dll that can be loaded by word and word-compatible application (in this question, I call it word-like app) (they use the same ooxml standard). My add-in uses CEF (chromium embedding framework) to present some web content. Here are some details in my add-in project.

This add-in's platform target is any cpu, loaded by this word-like app. When starting up, I check if this add-in is loaded in a 32-bit environment or 64-bit environment by the size of IntPtr. After that, I decide to use a 32-bit cef library or 64-bit cef library. it makes sense. Now I know which path I should use to load the cef library. pseudocode:

 private _cefPath;
    
 void FindLibraryPath() {
   string arch = IntPtr.Size == 4 ? "x86" : "x64";
   SetLibraryAndResourcePath(arch); // set _cefPath and other path here
 }
    
 void InitializeCef() {
   // Load cef library
   // this method use LoadLibraryEx with the flag
   // LOAD_WITH_ALTERED_SEARCH_PATH to load libcef.dll
   CefRuntime.Load(_cefPath);
     // other init statements for cef
 }
    
 private void ThisAddIn_Startup(object sender, System.EventArgs e) {
   FindLibraryPath();
   InitializeCef();
 }

But the problem is that the word-like app also use cef to load its web content, we both use the same library only with different versions. It seems that once I have loaded my libcef.dll, when I clicked the button that causes the work-like app to load its web content (it tried to load its own libcef.dll at that time, I think), the word-like app crashed. I don't know the exact cause why it crashed.

I conceive if there exsits a method that my add-in and the word-like app can load their own libcef.dll and refer to their own libcef.dll in this situation to avoid crashing the word-like app.


  • I tried to rename libcef.dll to libcef1.dll, but in vain. That word like application still crashed as usually if I use its web content.

205113-a-typical-cef-folder.png


windows-apiwindows-formsoffice-vsto-com-dev
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you try to rename your 32- and 64-bit libcef.dll?


0 Votes 0 ·

I tried, but it still crashed in this word-like appliation. don't have any problems in word.

0 Votes 0 ·

The fastest and easiest way : You need to put the dll in a different folder or save it with a different name.
According to common sense, there should be no problem, whether there is data transfer between the two calls?
You can view the crash cause through the [Event viewer].

205081-image.png


0 Votes 0 ·
image.png (43.1 KiB)
yanpeng avatar image yanpeng JunjieZhu-MSFT ·

In fact, the cef library of the word-like app have a different folder with mine. although I renamed libcef.dll to libcef1.dll and load it successfully in Microsoft Word, I cannot achieve it in the word-like app, which still crashed as usual (of course, it didn't get crashed if I didn't open its web content, in that case, it didn't load its libcef.dll). btw, the event viewer doestn't give much more useful information, full of See Tracelogging for error details in the category of Windows Logs\Application.

My last hope is switching to WebView2, if that, I have to do a lot of work.

0 Votes 0 ·

Perhaps you can adapt the solution discussed at How can I specify that my DLL should resolve a DLL dependency from the same directory that the DLL is in? to your own situation. Note the sentence that says "When you try to load B.DLL, the system will recognize the manifest, and that manifest tells the system to ignore any rogue copies... "


0 Votes 0 ·

Thanks. Tomorrow I will come back to this question. Still don't have a clue. the crash is really cuz two versions of the libcef.dll are loaded.

0 Votes 0 ·

The idea in this article is the right way to solve this problem. Regarding the method it provides, it is the c++ way. In my situation,

Libraries my addin loads
myaddin.dll(.net) -> cefglue.dll(.net) -> libcef.dll (native dll, version 67) -> other chromium native dll

Libraries word-like app loads
|-> myaddin.dll(.net) -> cefglue.dll(.net) -> libcef.dll (native dll, version 67) -> other chromium native dll
|-> (when loading its own web contents) -> ... -> libcef.dll(native dll, version 87) -> other chromium native dll (in its installation directory)

I load libcef.dll using absolute path, if I load this library first, that word-like app won't load its own libcef.dll (this is a rule designed by LoadLibraryEx?), it plans to use my libcef.dll then crash, after all it need the version 87.
If the word-like app loads libcef.dll first, my add-in won't load the version 67 libcef.dll, then I get the version mismatch exception.

all in all, I need to implement the idea isolated application that makes that word-like app doesn't perceive the dlls loaded by my add-in.

0 Votes 0 ·

it tried to load its own libcef.dll at that time, I think

Did you try removing your DLLs and loading its own libcef.dll?


0 Votes 0 ·

Did you try removing your DLLs and loading its own libcef.dll?

in fact, I don't use the original cef library, which is written by c++. I used a cef .net binding called cefglue because I need use it in the .net environment (for example, embed the browser inside a windows form control). the cefglue shipped with a special version of libcef.dll. so if the libcef.dll version used by cefglue doesn't match the version used by that word-like application, some errors arise. Here is the result I tried your idea
205512-cef-runtime.png

but your idea indeed opens a door to solving this problem, that is if the version of libcef.dll cefglue used matches the version that word-like app loaded, no error will arise(I think :-). but that is hard to develop this add in, reasons: 1. I cannot control when and how the word like app loads libcef.dll 2. If it changed the version of libcef.dll, my add-in must fix some errors in cefglue caused by upgrading the version of libcef.dll.


0 Votes 0 ·
cef-runtime.png (8.2 KiB)
cef-runtime.png (8.2 KiB)

0 Answers