Skip to content

Commit

Permalink
Fallback to pnputil if Dism is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
lostindark committed Jun 11, 2019
1 parent 4a124e1 commit a3d1f38
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Rapr/DSEForm.cs
Expand Up @@ -70,7 +70,7 @@ public DSEForm()
Trace.TraceInformation("---------------------------------------------------------------");
Trace.TraceInformation($"{Application.ProductName} started");

this.UpdateDriverStore(new DismUtil());
this.UpdateDriverStore(DriverStoreFactory.CreateOnlineDriverStore());
}

private void UpdateDriverStore(IDriverStore driverStore)
Expand Down Expand Up @@ -860,11 +860,11 @@ private void ChooseDriverStoreToolStripMenuItem_Click(object sender, EventArgs e
switch (chooseDriverStore.StoreType)
{
case DriverStoreType.Online:
this.UpdateDriverStore(new DismUtil());
this.UpdateDriverStore(DriverStoreFactory.CreateOnlineDriverStore());
break;

case DriverStoreType.Offline:
this.UpdateDriverStore(new DismUtil(chooseDriverStore.OfflineStoreLocation));
this.UpdateDriverStore(DriverStoreFactory.CreateOfflineDriverStore(chooseDriverStore.OfflineStoreLocation));
break;
}

Expand Down
1 change: 1 addition & 0 deletions Rapr/Rapr.csproj
Expand Up @@ -99,6 +99,7 @@
<Compile Include="Utils\DeviceDriverInfo.cs" />
<Compile Include="Utils\DeviceHelper.cs" />
<Compile Include="Utils\DriverStoreEntry.cs" />
<Compile Include="Utils\DriverStoreFactory.cs" />
<Compile Include="Utils\DriverStoreRepository.cs" />
<Compile Include="Utils\IDriverStore.cs" />
<Compile Include="Utils\DismUtil.cs" />
Expand Down
23 changes: 22 additions & 1 deletion Rapr/Utils/DismUtil.cs
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

using System.Runtime.InteropServices;
using Microsoft.Dism;

namespace Rapr.Utils
Expand All @@ -24,6 +24,20 @@ public DismUtil(string imagePath)
this.OfflineStoreLocation = imagePath;
}

public static bool IsDismAvailable { get; } = new Func<bool>(() =>
{
try
{
Marshal.PrelinkAll(typeof(NativeMethods));
}
catch (DllNotFoundException)
{
return false;
}
return true;
})();

#region IDriverStore Members
public List<DriverStoreEntry> EnumeratePackages()
{
Expand Down Expand Up @@ -155,5 +169,12 @@ public bool AddDriver(string infFullPath, bool install)
}

#endregion

internal static class NativeMethods
{
[DllImport("DismApi", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Error)]
public static extern int DismInitialize(DismLogLevel logLevel, string logFilePath, string scratchDirectory);
}
}
}
22 changes: 22 additions & 0 deletions Rapr/Utils/DriverStoreFactory.cs
@@ -0,0 +1,22 @@
namespace Rapr.Utils
{
public static class DriverStoreFactory
{
public static IDriverStore CreateOnlineDriverStore()
{
if (DismUtil.IsDismAvailable)
{
return new DismUtil();
}
else
{
return new PnpUtil();
}
}

public static IDriverStore CreateOfflineDriverStore(string imagePath)
{
return new DismUtil(imagePath);
}
}
}

0 comments on commit a3d1f38

Please sign in to comment.