I am trying to log the connected scanner on a pc.
I am using NTwain.dll from https://bitbucket.org/soukoku/ntwain
.
If I run my app on a server some dependency dlls from ntwain fail to load so I will load the dll at runtime and if it will fail I just want to return an empty list. There is no reference to NTwain in the project references anymore.
Problem: If I have the NTwain.dll in the folder with the exe and I run it on a server the app crashes. It doesn't return an empty list. If I delete the dll and run the app the empty list gets returned.
Code:
public class Scanner : IDB
{
private enum DataGroups : uint
{
None = 0,
Control = 0x1,
Image = 0x2,
Audio = 0x4,
Mask = 0xffff,
}
public string Name { get; private set; }
public string ProductFamily { get; private set; }
public string Version { get; private set; }
public Scanner()
{
Name = String.Empty;
}
public static List<Scanner> getScanners()
{
List<Scanner> scanners = new List<Scanner>();
try
{
Assembly assembly = Assembly.LoadFrom(Environment.CurrentDirectory + "\\NTwain.dll");
Type tident = assembly.GetType("NTwain.Data.TWIdentity");
Type tsession = assembly.GetType("NTwain.TwainSession");
object appId = tident.GetMethod("CreateFromAssembly").Invoke(null, new object[] { DataGroups.Image, System.Reflection.Assembly.GetExecutingAssembly() });
object session = Activator.CreateInstance(tsession, appId);
tsession.GetMethod("Open", new Type[0]).Invoke(session, null);
object sources = session.GetType().GetMethod("GetSources").Invoke(session, null);
foreach (var item in (IEnumerable)sources)
{
Scanner scanner = new Scanner();
scanner.Name = (string)item.GetType().GetProperty("Name").GetValue(item, null);
scanner.ProductFamily = (string)item.GetType().GetProperty("ProductFamily").GetValue(item, null);
object version = item.GetType().GetProperty("Version").GetValue(item, null);
scanner.Version = (string)version.GetType().GetProperty("Info").GetValue(version, null);
scanners.Add(scanner);
}
return scanners;
}
catch (Exception e)
{
return new List<Scanner>();
}
}
}