2

I found here a tutorial for using NTWain to scan documents in C# WPF.

I tried the solution in an empty projet x86, it works good.

Now I'm tried to use this solution in my x64 project. First I had trouble with twainDSM.dll, so I downloaded the x64 version.

Now the project runs but there is no scanner in my list.

In code there is :

public TwainCore()
{
    //Allow old Device DSM drives
    PlatformInfo.Current.PreferNewDSM = false;

    var appId = TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetExecutingAssembly());
    _twainSession = new TwainSession(appId);

    PlatformInfo.Current.PreferNewDSM = false;

    _twainSession.TransferReady += _twainSession_TransferReady;
    _twainSession.StateChanged += _twainSession_StateChanged;

    if (_twainSession.Open() != ReturnCode.Success)
        throw new InvalidProgramException("Erreur de l'ouverture de la session");
}

I watched both :

  • In x86 _twainSession._ownedSources.Count = 1

  • In x64 _twainSession._ownedSources.Count = 0

So why there is no scanner found in my x64 project ?

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • Reaching: could it be something to do with the scanner driver not having an x64 component? – DonBoitnott Sep 20 '18 at 12:06
  • @DonBoitnott maybe It's the first time I work with scanners, I don't know how it work. Is there a way to use x86 dll in my x64 application ? – A.Pissicat Sep 20 '18 at 12:15
  • @A.Pissicat https://stackoverflow.com/questions/2265023/load-32bit-dll-library-in-64bit-application – Blacktempel Sep 20 '18 at 12:16
  • I'm not sure that it will work. How can I know if my scanner is compatible with twainDSM.dll x64 ? – A.Pissicat Sep 20 '18 at 12:27
  • If your project is a commercial ones, make use of `NAPS2` codes in https://github.com/cyanfish/naps2 there are so many issues on working on Scanners. Microsoft has released a new technology `WIA` as a successor for TWAIN and you will have some other problem with each other. I have worked on them in about One month. NAPS2 was my last choice and it worked for us in a commercial app. the Only issue with it was to install the latest driver for the Scanner. – RezaNoei Sep 20 '18 at 16:23
  • @RezaNoei I looked at your solution but I do not know github well. I tried to find NAPS2 on nuget packages via Visual Studio but there is no package. How can I use the WIA solution on my project ? – A.Pissicat Sep 21 '18 at 08:10
  • The main problem in Both TWAIN and WIA is to work with feeder. there are some sample code for both in the internet. but also they are tricky. NAPS2 actually is consist of multiple solutions, it also has OCR, PDF creator and ... . If you want to separate Scanning module from this project, it takes some time but it is worth to spend this time. DependencyInjection using Ninject is the main part the solution, if you run this solution or you have downloaded the exe file from https://www.naps2.com/ you will see that it has Scan button at the top and it has some functions to detect Scanners and ... – RezaNoei Sep 21 '18 at 12:15
  • it Uses a Scanning profile to send Scan request, to the other layers. its actually a reverse engineering. – RezaNoei Sep 21 '18 at 12:16

1 Answers1

4

The 64-bit TWAIN DSM only works with 64-bit data sources, and most scanners only provide 32-bit data sources. Therefore you will need to use the 32-bit DSM.

You have two options:

  • Run your application in 32-bit mode
  • Use a child process that runs in 32-bit mode

For the latter, see this question. You can also look at NAPS2 (of which I am the author) that uses WCF over a named pipe to communicate with a worker process.

Note that WIA (an alternative to TWAIN) doesn't have this problem because Windows includes a 64-to-32 bit translation layer that does this work for you. However, it has other downsides (like worse support for feeders).

Cyanfish
  • 3,907
  • 1
  • 14
  • 12