Stump the Chump: Resolving Unable to load DLL wldp.dll exception

After starting with my current team, I started a monthly challenge where one of us challenges other team members to solve a technical problem. Once a solution is proposed and the problem owner is satisfied with the outcome, I will buy the team lunch and ask the solution owner(s) to discuss the following:

  1. Outline the steps taken to reach this solution
  2. Are there any alternatives? If yes, what are they and why didn’t choose one of them?
  3. What resources (if any) did you use to solve this problem?

Last Friday, I realized that I was chosen to be victim for the next challenge when I received this email from one of the leads:

Subject: Solution is due Monday
Sammy, Attempting to use PowerShell commands from a C# project in windows 8.1 and VS 2013 will result in the following exception “Unable to load DLL 'wldp.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)”. You can see the exception in Visual Studio’s IntelliTrace window.
PowerShell commands will execute properly if you choose to ignore the exception. However, your challenge is to provide a solution which runs without any exceptions.
Disclaimer/Credit: Once of Jason’s friends at ___ encountered this when adding power shell capabilities to one of their projects and they gave up on finding a solution after three days and many failed attempts.
Have fun this weekend :0)

Initially I thought this was a joke or a trick question by the guys. On Saturday I started with a simple console application, added a reference to System.Management.Automation using Nuget and wrote the following code:

static void Main(string[] args)
    var initial = InitialSessionState.CreateDefault();
    initial.ThrowOnRunspaceOpenError = true;
    using (var rs = RunspaceFactory.CreateRunspace(initial))

The exception “Unable to load DLL 'wldp.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)” gets thrown after the rs.Open(); line gets executed (the exception can be seen on the InteliTrace window).

A quick google search returned a couple of posts ( here and here) related to the same problem with no solution. A quick search on my machine clearly shows this assembly is present in these two locations (C:\windows\WinSxS and C:\windows\system32. Maybe it’s just a binding issue? Opening Fusion log viewer and enabling logs didn’t show any binding problems, in fact there is no mention of this assembly in fusion logs.

My next step is to debug the System.Management.Automation assembly. Using reflector, I generated pdp files, enabled assembly debugging and started stepping into System.Management.Automation source code. After hitting F11 a few times, the debugger stopped at this method’s catch clause:

public static SystemEnforcementMode GetLockdownPolicy(string path, SafeHandle handle)
                if (hadMissingWldpAssembly)
                    return GetDebugLockdownPolicy(path);
                WLDP_HOST_INFORMATION pHostInformation = new WLDP_HOST_INFORMATION {
                    dwRevision = 1,
                    dwHostId = WLDP_HOST_ID.WLDP_HOST_ID_POWERSHELL
                if (!string.IsNullOrEmpty(path))
                    pHostInformation.szSource = path;
                    if (handle != null)
                        IntPtr zero = IntPtr.Zero;
                        zero = handle.DangerousGetHandle();
                        pHostInformation.hSource = zero;
                uint pdwLockdownState = 0;
                WldpNativeMethods.WldpGetLockdownPolicy(ref pHostInformation, ref pdwLockdownState, 0);
                return GetLockdownPolicyForResult(pdwLockdownState);
            catch (DllNotFoundException)
                hadMissingWldpAssembly = true;
                return GetDebugLockdownPolicy(path);

Restarting the debug session multiple time led me to conclude that prior calls are used to determine if the host machine is Windows machine with an ARM architecture. When I unchecked prefer 32-bit checkbox platform target from the project build properties  and started the project in debug mode without seeing the ‘not found’ exception I was more excited than a teenage girl at a Justin Bieber concert.

If you are not familiar with these platform targets, all you need to know is that if you compile a .Net application using prefer 32-bit and any CPU flags it will run on any windows environment including the ones with an ARM Architecture, however, application compiled without the prefer 32-bit flag will fail to run on machines with ARM Architecture (Tables and Windows Phones 8+)

The next exception was "Could not load file or assembly 'Microsoft.PowerShell.Security' or one of its dependencies. The system cannot find the file specified.", resolving this was much easier than the wldp.dll one . Fusion logs showed that the Microsoft.PowerShell.Security is missing from the project, adding a reference to the assembly and set it's Copy Local property to true resolved exception and gave me an exception free PowerShell enabled app.

As usual, hit the comment box and let me know what you think.

Comments (6) -

holy shit, we had 2 devs working on this for 3 days without any solutions, gave up on using System.Management.Automation then spent one extra week writing a custom solution after MS told us just to ignore the exception.

I am glad the post was of help to you.

Michelle Marks 6/1/2014 6:19:16 PM

I just wanted to thank you as well.
I spent my weekend scratching my head because of this.

Wagar Hussain 6/4/2014 2:26:21 PM

such an easy solution Smile

thank you

Thanks for the post, however I continue to get this issue ('wldp.dll' in IntelliTrace exceptions), whether 'Prefer 32-bit' is checked or not. Further, I cannot locate Microsoft.Powershell.* references on my system.

Is there any other causes you could think of, or would perhaps adding Microsoft.Powershell.Security.dll as a reference fix this? Do I need to download it (I don't see it available in VS2013)?

Thanks for this, but I'm still getting the issue. I downloaded wldp.dll as Windows 7 doesn't have it. Now I'm getting:

A first chance exception of type 'System.BadImageFormatException' occurred in System.Management.Automation.dll

Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

I give up!

Thanks anyway.

Comments are closed