soukoku / ntwain

A TWAIN lib for dotnet.
MIT License
117 stars 47 forks source link

WinUI3 Sample #41

Open F1schk0pp opened 1 year ago

F1schk0pp commented 1 year ago

Hi @soukoku,

could you try to provide a WinUi3 sample? I would code it by myself but i currently have no working scanner available.

I have tried it in the past with v3 but it was not working. Maybe it is possible in v4?

soukoku commented 1 year ago

It essentially needs to be able to get into the UI message loop to intercept messages and pass some of them to TWAIN. This does not seem possible when I investigated before. There's no extensibility point at that level like winform/wpf does.

If that's still true, one choice is to run your own message pump in another thread. Unfortunately I'm probably not experienced enough in win32 to make one correctly (maybe I can try in the future). Another option is to run a separate process based on winform/wpf that the main app talks to through some IPC mechanism. That's what I was trying to do with the signalr test :p

F1schk0pp commented 1 year ago

I managed to hook into the message loop in a WinUi3 application with this code: https://www.michaelwda.com/post/netcore_message_pump

But instead of WinMsg I used your WIN_MESSAGE.

But I don't know how to implement it in NTwain.

Maybe you will have more luck if you want to try it. You are probably more experienced than me. 😅

soukoku commented 1 year ago

While I appear to have a working self-hosted loop using winform that works in other places (like avalon ui), WinUI3 apps just don't seem to work with twain in general.

Showing the old select dialog and opening a source doesn't require hooking into the mesage loop and even those don't work right. I suspect the dynamic loading nature of sources are restricted or something though I have no proof. Your only option may be a separate exe process that you control through some IPC.

F1schk0pp commented 1 year ago

Ah ok i see.. Hmh, maybe i will try it with IPC and a extra "worker" app 🤔

If i succeed, i will create a PR with a WinUi3 sample 👍

christopher-carmichael commented 1 year ago

So I thought I would chime in here, basically you have two options. If you select your winui3 app to run unpackaged it will natively have access to win32 apis. This should allow ntwain to work. But a key new feature in Winui3 vs UWP is allowing fulltrust applications within the app package. So if you open package manifest and set uap10:TrustLevel attribute in and I believe add the following capabilities are necessary, but it has been months since I initially set up so a little foggy:

<Applications>
<Application
uap10:TrustLevel="mediumIL"
....
...
...>

<Capabilities>
        <rescap:Capability Name="runFullTrust"/>
        <rescap:Capability Name="unvirtualizedResources"/>
    </Capabilities>

I am successfully using Ntwain in this capacity in a WinUi3 application. This is actually a great improvement because before we were using a UWP app and had to have an IPC to a win32 app that did the scanning and pass back scan.

F1schk0pp commented 1 year ago

Thx, if i run it unpackaged it works. v4 however throws a NotSupportedException when calling EnableSource: The send method is not supported, use Post instead.

F1schk0pp commented 11 months ago

I have managed to get it work with WinUi3. I will submit a PR the next days.