This project has moved and is read-only. For the latest updates, please go here.

Native mode error

Apr 18, 2016 at 4:10 PM
Saraff.Twain.TwainException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Saraff.Twain.DibToImage.WithStream(IntPtr dibPtr)
at Saraff.Twain.Twain32._NativeTransferPictures()
at Saraff.Twain.Twain32._TwCallbackProcCore(TwMSG msg, Action`1 endAction)
at Saraff.Twain.Twain32._TwCallbackProc(TwIdentity appId, TwIdentity srcId, TwDG dg, TwDAT dat, TwMSG msg, IntPtr data)
I have followed the CSharp Sample3 (console project) to the letter and I get that error in my application, the sample project works fine. THe scanner is a mini-desk brick thing Brother DS820W.
Apr 18, 2016 at 6:11 PM
Hello, Ravenheart.
Try set STAThreadAttribute on a Main method.
Marked as answer by SARAFF on 4/27/2016 at 10:20 AM
Apr 19, 2016 at 6:55 AM
Is there no way to workaround that? I've created a simple scan class that does the job well. It works fine when the transfer tech is File.
Apr 19, 2016 at 7:07 PM
Hello, Ravenheart.
I do not understand the question. What exactly do you need to workaround? Were you solve the problem by setting the STAThreadAttribute attribute?
Apr 20, 2016 at 7:12 AM
Where am I supposed to place it? Over the method that does all the TWAIN creation and .Acquire() or on my class?
Apr 20, 2016 at 7:41 PM
Hello, Ravenheart.
See STAThreadAttribute Class.
internal sealed class Program {

    private static void Main(string[] args) {

        // ...

Apr 20, 2016 at 8:48 PM
The code where I use the Twain32 class is not in the entry point of the app (where you would normally find the STAThread attribute). I Have create a helper class Scanner that has basically a public Image ScanImages() method that does all the work of creating the Twain32 class instance and setting a bunch of capabilities and handles the actual Xfer of the scanned images, hence me asking if its possible to put the attribute above the ScanImages() method.
Apr 20, 2016 at 9:05 PM
Apply this attribute to the entry point method (the Main() method in C# and Visual Basic). It has no effect on other methods. To set the apartment state of threads you start in your code, use the Thread.SetApartmentState or Thread.TrySetApartmentState method before starting the thread.