msg loop tweaks & intptr cleanup.

This commit is contained in:
soukoku
2014-09-05 06:56:57 -04:00
parent 19f65642b2
commit 669de30345
8 changed files with 26 additions and 9 deletions

View File

@@ -45,6 +45,7 @@ namespace NTwain.Internals
{ {
_hook.Dispose(); _hook.Dispose();
_hook = null; _hook = null;
Handle = IntPtr.Zero;
} }
})); }));
loopThread.IsBackground = true; loopThread.IsBackground = true;

View File

@@ -24,4 +24,11 @@ namespace NTwain.Internals
#endregion #endregion
} }
static class NativeMethods
{
[DllImport("user32.dll")]
public static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
}
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Interop; using System.Windows.Interop;
@@ -18,13 +19,13 @@ namespace NTwain.Internals
{ {
_filter = filter; _filter = filter;
// hook into windows msg loop for old twain to post msgs.
// the style values are purely guesses here with
// CS_NOCLOSE, WS_DISABLED, and WS_EX_NOACTIVATE
HwndSource win = null; HwndSource win = null;
try try
{ {
win = new HwndSource(0x0200, 0x8000000, 0x8000000, 0, 0, "NTWAIN_LOOPER", IntPtr.Zero); // hook into windows msg loop for old twain to post msgs.
// the style values are purely guesses here with
// CS_NOCLOSE, WS_TILEDWINDOW, and WS_EX_APPWINDOW
win = new HwndSource(0x0200, 0xCF0000, 0x40000, 0, 0, "NTWAIN_LOOPER", IntPtr.Zero);
Handle = win.Handle; Handle = win.Handle;
win.AddHook(WndProc); win.AddHook(WndProc);
_win = win; _win = win;
@@ -43,6 +44,13 @@ namespace NTwain.Internals
{ {
handled = _filter.IsTwainMessage(hwnd, msg, wParam, lParam); handled = _filter.IsTwainMessage(hwnd, msg, wParam, lParam);
} }
if (!handled)
{
Debug.WriteLine("Hwnd=" + hwnd);
handled = true;
// unnecessary to do default wndproc?
return NativeMethods.DefWindowProc(hwnd, (uint)msg, wParam, lParam);
}
return IntPtr.Zero; return IntPtr.Zero;
} }
@@ -58,6 +66,7 @@ namespace NTwain.Internals
((HwndSource)_win).RemoveHook(WndProc); ((HwndSource)_win).RemoveHook(WndProc);
_win.Dispose(); _win.Dispose();
_win = null; _win = null;
Handle = IntPtr.Zero;
} }
} }

View File

@@ -113,6 +113,7 @@ namespace NTwain
//{ //{
System.Windows.Forms.Application.RemoveMessageFilter(this); System.Windows.Forms.Application.RemoveMessageFilter(this);
_filter = null; _filter = null;
Handle = IntPtr.Zero;
//}); //});
} }
@@ -192,6 +193,7 @@ namespace NTwain
_hooker.RemoveHook(FilterMessage); _hooker.RemoveHook(FilterMessage);
_hooker.Dispose(); _hooker.Dispose();
_hooker = null; _hooker = null;
Handle = IntPtr.Zero;
} }
} }
} }

View File

@@ -14,6 +14,6 @@ namespace NTwain
// keep this same in majors releases // keep this same in majors releases
public const string Release = "2.0.0.0"; public const string Release = "2.0.0.0";
// change this for each nuget release // change this for each nuget release
public const string Build = "2.0.6"; public const string Build = "2.0.7";
} }
} }

View File

@@ -74,8 +74,6 @@ namespace Tester.WPF
// use this for internal msg loop // use this for internal msg loop
//var rc = _twainVM.Open(); //var rc = _twainVM.Open();
// use this to hook into current app loop // use this to hook into current app loop
var rc = _twainVM.Open(new WpfMessageLoopHook(new WindowInteropHelper(this).Handle)); var rc = _twainVM.Open(new WpfMessageLoopHook(new WindowInteropHelper(this).Handle));
if (rc == ReturnCode.Success) if (rc == ReturnCode.Success)

View File

@@ -56,7 +56,7 @@
</Reference> </Reference>
<Reference Include="ModernWPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c99d0cfbea7491ef, processorArchitecture=MSIL"> <Reference Include="ModernWPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c99d0cfbea7491ef, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\ModernWPF.1.1.45.2\lib\net40-Client\ModernWPF.dll</HintPath> <HintPath>..\..\packages\ModernWPF.1.1.45.3\lib\net40-Client\ModernWPF.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />

View File

@@ -2,6 +2,6 @@
<packages> <packages>
<package id="CommonServiceLocator" version="1.3" targetFramework="net40-Client" /> <package id="CommonServiceLocator" version="1.3" targetFramework="net40-Client" />
<package id="CommonWin32" version="2.0.5.4" targetFramework="net40-Client" /> <package id="CommonWin32" version="2.0.5.4" targetFramework="net40-Client" />
<package id="ModernWPF" version="1.1.45.2" targetFramework="net40-Client" /> <package id="ModernWPF" version="1.1.45.3" targetFramework="net40-Client" />
<package id="MvvmLightLibs" version="4.4.32.1" targetFramework="net40-Client" /> <package id="MvvmLightLibs" version="4.4.32.1" targetFramework="net40-Client" />
</packages> </packages>