diff --git a/samples/WinConsole32/Program.cs b/samples/WinConsole32/Program.cs index d985ab8..f68746f 100644 --- a/samples/WinConsole32/Program.cs +++ b/samples/WinConsole32/Program.cs @@ -11,7 +11,7 @@ namespace WinConsole32 var libVer = FileVersionInfo.GetVersionInfo(typeof(TwainAppSession).Assembly.Location).ProductVersion; Console.WriteLine($"Console sample {(TWPlatform.Is32bit ? " 32bit" : " 64bit")} on NTwain {libVer}"); - TwainAppSession session = new TwainAppSession(Environment.ProcessPath!); + TwainAppSession session = new TwainAppSession(new TW_IDENTITY_LEGACY(Environment.ProcessPath!)); session.StateChanged += Session_StateChanged; session.SourceDisabled += Session_SourceDisabled1; diff --git a/samples/WinForm32/Form1.cs b/samples/WinForm32/Form1.cs index dd78657..4aaa564 100644 --- a/samples/WinForm32/Form1.cs +++ b/samples/WinForm32/Form1.cs @@ -37,7 +37,7 @@ namespace WinFormSample TWPlatform.PreferLegacyDSM = false; - twain = new TwainAppSession(Assembly.GetExecutingAssembly().Location); + twain = new TwainAppSession(new TW_IDENTITY_LEGACY(Assembly.GetExecutingAssembly().Location)); twain.StateChanged += Twain_StateChanged; twain.DefaultSourceChanged += Twain_DefaultSourceChanged; twain.CurrentSourceChanged += Twain_CurrentSourceChanged; diff --git a/src/NTwain/Data/TWAINH_EXTRAS.cs b/src/NTwain/Data/TWAINH_EXTRAS.cs index ddc7304..ed1c80c 100644 --- a/src/NTwain/Data/TWAINH_EXTRAS.cs +++ b/src/NTwain/Data/TWAINH_EXTRAS.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Globalization; using System.Runtime.InteropServices; +using System.Text; namespace NTwain.Data { @@ -32,6 +34,7 @@ namespace NTwain.Data } #else + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { IsWindows = true; @@ -657,6 +660,68 @@ namespace NTwain.Data } partial struct TW_IDENTITY_LEGACY { + /// + /// Creates app info derived an executable file. + /// + /// + /// + /// + public TW_IDENTITY_LEGACY( + string exeFilePath, + TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) : + this(FileVersionInfo.GetVersionInfo(exeFilePath), appLanguage, appCountry) + { } + /// + /// Creates app info derived from a object. + /// + /// + /// + /// + public TW_IDENTITY_LEGACY( + FileVersionInfo appInfo, + TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) : + this( + appInfo.CompanyName ?? "", + appInfo.ProductName ?? "", + appInfo.ProductName ?? "", + new Version(appInfo.FileVersion ?? "1.0"), + appInfo.FileDescription ?? "", appLanguage, appCountry) + { } + /// + /// Creates id with explicit app info. + /// + /// + /// + /// + /// + /// + /// + /// + /// + public TW_IDENTITY_LEGACY( + string companyName, string productFamily, string productName, + Version productVersion, string productDescription = "", + TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA, + DG supportedTypes = DG.IMAGE) + { + Manufacturer = companyName; + ProductFamily = productFamily; + ProductName = productName; + ProtocolMajor = (ushort)TWON_PROTOCOL.MAJOR; + ProtocolMinor = (ushort)TWON_PROTOCOL.MINOR; + SupportedGroups = (uint)(supportedTypes | DG.CONTROL | DG.APP2); + Version = new TW_VERSION + { + Country = appCountry, + Info = productDescription, + Language = appLanguage, + MajorNum = (ushort)productVersion.Major, + MinorNum = (ushort)productVersion.Minor, + }; + } + + + /// /// A simplified check on whether this has valid data from DSM. /// diff --git a/src/NTwain/TwainAppSession.cs b/src/NTwain/TwainAppSession.cs index 1eeeaeb..82c50af 100644 --- a/src/NTwain/TwainAppSession.cs +++ b/src/NTwain/TwainAppSession.cs @@ -1,13 +1,10 @@ using NTwain.Data; using NTwain.Triplets; using System; -using System.Collections.Concurrent; -using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; -using System.Xml; namespace NTwain { @@ -15,81 +12,16 @@ namespace NTwain public partial class TwainAppSession : IDisposable { - static bool __encodingRegistered; - - /// - /// Creates TWAIN session with app info derived an executable file. - /// - /// - /// - /// - public TwainAppSession( - string exeFilePath, - TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) : - this(FileVersionInfo.GetVersionInfo(exeFilePath), appLanguage, appCountry) - { } - /// - /// Creates TWAIN session with app info derived from a object. - /// - /// - /// - /// - public TwainAppSession( - FileVersionInfo appInfo, - TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) : - this( - appInfo.CompanyName ?? "", - appInfo.ProductName ?? "", - appInfo.ProductName ?? "", - new Version(appInfo.FileVersion ?? "1.0"), - appInfo.FileDescription ?? "", appLanguage, appCountry) - { } /// /// Creates TWAIN session with explicit app info. /// - /// - /// - /// - /// - /// - /// - /// - /// - public TwainAppSession( - string companyName, string productFamily, string productName, - Version productVersion, string productDescription = "", - TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA, - DG supportedTypes = DG.IMAGE) + /// + public TwainAppSession(TW_IDENTITY_LEGACY appId) { #if WINDOWS || NETFRAMEWORK DSM.DsmLoader.TryLoadCustomDSM(); #endif - // todo: find a better place for this - if (!__encodingRegistered) - { -#if !NETFRAMEWORK - Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); -#endif - __encodingRegistered = true; - } - - _appIdentity = new() - { - Manufacturer = companyName, - ProductFamily = productFamily, - ProductName = productName, - ProtocolMajor = (ushort)TWON_PROTOCOL.MAJOR, - ProtocolMinor = (ushort)TWON_PROTOCOL.MINOR, - SupportedGroups = (uint)(supportedTypes | DG.CONTROL | DG.APP2), - Version = new TW_VERSION - { - Country = appCountry, - Info = productDescription, - Language = appLanguage, - MajorNum = (ushort)productVersion.Major, - MinorNum = (ushort)productVersion.Minor, - } - }; + _appIdentity = appId; _legacyCallbackDelegate = LegacyCallbackHandler; _osxCallbackDelegate = OSXCallbackHandler; @@ -97,6 +29,7 @@ namespace NTwain StartTransferThread(); } + internal IntPtr _hwnd; internal TW_USERINTERFACE _userInterface; // kept around for disable to use #if WINDOWS || NETFRAMEWORK