none
Issue in loading dependent dlls for outlook plug in RRS feed

  • Question

  • HI,

    I have written a outlook plug in C#. However it is using unmanaged dll as well which is further dependent on some other dlls. Now when I try to install and then run my plugin it is not able to load dependent dlls of unmanaged dll.

    I have made a msi installer. Therefore I have put all dependent dlls in our installer and I can see them in installed folder as well.

    When I add the path of our installer directory in environment variable (Path), it works fine. As per my understanding, outlook's working directory is different from installation directory. Therefore it automatically does not find the dependency.

    I have tried to set he Environment variable in my code like below :--

                    string installPath = GetInstallationPath();

                    string pathVar = System.Environment.GetEnvironmentVariable("Path", System.EnvironmentVariableTarget.Process) + ";" + installPath;

                    System.Environment.SetEnvironmentVariable("Path", pathVar, System.EnvironmentVariableTarget.Process);

    But it is still not loading dependent dlls. Can some one please suggest the solution for that ?

    Thanks

    Dev

    Tuesday, May 23, 2017 3:41 PM

Answers

  • Take a look at LoadLibraryEx function.  It provides options that give you finer control over the way that the system searches for dependencies.

    Wednesday, May 24, 2017 12:04 PM
  • Are the other dependents dlls in the same folder? Have you tried to set the current directory before calling LoadLIbrary?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, May 24, 2017 8:32 PM
  • HI RLWA32 and Dmitry,

    Thanks for your help. However it emerge as stupid issue on my end. I was using install directory path from my registry setting which has URL type like "File:///C:/". It was the reason as it was neither working in CurrentDirectory nor in Envoirnment.SetEnvoinment.

    After fixing this, LoadLiberary is also working and without it, just setting the environment variable for current process is also working. 

    So at the end, I can apologize for my stupid error, which have wasted your time.

    Thanks

    Devendra

    Monday, May 29, 2017 5:46 PM

All replies

  • Try not load link to those dlls statically - use LoadLibrary.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.14 is now available!


    Tuesday, May 23, 2017 3:48 PM
  • HI Dmitry,

    Thanks for quick reply. But I have one stupid question. If suppose I load unmanaged dll dynamically even then if that library is using some other dlls statically, then will it pick all those dlls from that path as well (ideally it should but I am not sure).

    Second point is that is it a right solution ? Can not we use unmanaged static lib in out plug in ? we should have some common solution for it ? 

    Thanks

    Devendra

    Tuesday, May 23, 2017 3:55 PM
  • If that dll depends on other dlls, Windows will search in the folder when the first dll is located.

    Or you can set the active directory before calling LoadLibrary.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, May 23, 2017 4:02 PM
  • HI Dmitry,

    Thanks for your reply. Do you have idea why setting a path variable for my process is not working ? ideally it should work ? Any advice ?

    Thanks

    Devendra

    Tuesday, May 23, 2017 4:08 PM
  • Don't mess with PATH - you are using a sledgehammer where you need small tweezers.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, May 23, 2017 4:22 PM
  • See Dynamic-Link Library Search Order for a detailed discussion about how Windows searches for DLLs. 

    In the case of static linkage, the system has already attempted to load DLLs before your code to manipulate the path has executed.  That's why it doesn't work.

    If you use dynamic linking you can exercise control over how and where the system looks for your unmanaged DLLs.

    Tuesday, May 23, 2017 5:21 PM
  • Hi RLWA32,

    Thanks for response. I will try tomorrow and will update accordingly.

    Cheers

    Dev

    Tuesday, May 23, 2017 6:28 PM
  • HI RLWA,

    I am facing a strange behavior. When I link this unmanaged library statically (basically I am using clr / cli lib to wrap it and then link this library statically), it loaded successfully. But when I try to load this one dynamically it gives error unable to load one of its dependencies. 

    What can be the reason for it ? Does system not look in to common directories to load it ? Do we have any other way around ? It supposed to be obvious issue for outlook plug in ?

    Thanks

    Devendra

    Wednesday, May 24, 2017 11:38 AM
  • Take a look at LoadLibraryEx function.  It provides options that give you finer control over the way that the system searches for dependencies.

    Wednesday, May 24, 2017 12:04 PM
  • I probably should have asked this right from the beginnig -- are you specifying a path in your call to LoadLibrary?
    Wednesday, May 24, 2017 3:40 PM
  • HI RLWA32,

    Yes, I am doing it since my first attempt.


    Wednesday, May 24, 2017 8:00 PM
  • Are the other dependents dlls in the same folder? Have you tried to set the current directory before calling LoadLIbrary?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, May 24, 2017 8:32 PM
  • Use LoadLibraryEx to control the search path for dependent Dlls as follows -

    HMODULE hLib = LoadLibraryEx(L"C:\\Program Files\\Outlook Extensions\\MyUnmanagedLib.dll", NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);

    This assumes that the unmanaged DLL and all of it's dependencies are in the same folder.
    • Edited by RLWA32 Wednesday, May 24, 2017 9:22 PM
    Wednesday, May 24, 2017 9:20 PM
  • HI Dmitry,

    I have not set the current directory and that can be the reason. But my question is should we set the current directory ? Outlook load our vsto plugin. IN that case, if we change the current directory, will it not impact any outlook's process ?  

    Thanks

    Dev

    Thursday, May 25, 2017 4:25 AM
  • No, once the dll is loaded, you are fine - you can read the current directory, set the current directory to where the dll and its dependencies are located, call LoadLibrary, reset the current directory to the value saved in the first step.

    Or, as RLWA32 mentioned, use LoadLibraryEx with the LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR flag.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, May 25, 2017 4:37 AM
  • HI Dmitry and RLWA32,

    CurrentDirectory is also not working for me. I will dig more in to it with process monitor and come back to you. However I have one question. What is the role of manifest file ? I feel that outlook read the dependencies from this file only.

    In my plugin structure, I have one vsto dll which is further dependent on one clr / cli dll (which I had statically linked). This clr / cli dll further uses some unmanaged dlls. Now by default, I think, to generate the manifest, visual studio look in to references only. Therefore my ManagedC++ (lets suppose it is name of my clr / cli dll) dll appears in manifest and then in C:\users\appdata\Local\Assembly\... path from where exactly outlook executes the process. But dependencies of ManagedC++ dll does not appear it manifest and therefore Outlook ignores it from Install directory. 

    Please correct me if I am wrong ? And if it is then can I use any manifest generator tool which will generate manifest for all dlls which is present in my output folder ? 

    Thanks

    Dev

    Friday, May 26, 2017 2:15 PM
  • It seems you have not tried to use LoadLibraryEx as suggested above -

    HMODULE hLib = LoadLibraryEx(L"C:\\Program Files\\Outlook Extensions\\MyUnmanagedLib.dll", NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);


    I suspect the problem resides in code that we have not seen.
    • Edited by RLWA32 Friday, May 26, 2017 2:25 PM
    Friday, May 26, 2017 2:22 PM
  • Hello,

    If you have resolved this issue, we would appreciate you could share your solution here and i would suggest you mark helpful posts or your solution as answer to close this thread.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 29, 2017 6:05 AM
    Moderator
  • HI RLWA32 and Dmitry,

    Thanks for your help. However it emerge as stupid issue on my end. I was using install directory path from my registry setting which has URL type like "File:///C:/". It was the reason as it was neither working in CurrentDirectory nor in Envoirnment.SetEnvoinment.

    After fixing this, LoadLiberary is also working and without it, just setting the environment variable for current process is also working. 

    So at the end, I can apologize for my stupid error, which have wasted your time.

    Thanks

    Devendra

    Monday, May 29, 2017 5:46 PM