Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert c# forums application to a windows service api so that I can interact from browser.

.NET TWAIN image scanner[^]
I am complete beginner in c# and twain technology so I want to first rewrite the library in c# console then make it api/ windows service.

But I faced major roadblock and not sure how to convert this into c# console or api app
In the source code
public void Init( IntPtr hwndp )
		{
		Finish();
		TwRC rc = DSMparent( appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.OpenDSM, ref hwndp );
		if( rc == TwRC.Success )
			{
			rc = DSMident( appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.GetDefault, srcds );
			if( rc == TwRC.Success )
				hwnd = hwndp;
			else
				rc = DSMparent( appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.CloseDSM, ref hwndp );
			}
		}


Init (this.handle) is used in windows forums but how can I give the input to twain dll if its a console or an api app.

I need to get the dll handle in order to perform any further functions but I am stuck in this step.

I tried giving intptr hwndp= new intptr(x) where 3 is int x=3. Null pointer is resulting in failure. But I believe this is not how you do it. I need to give some variable type so that dll will give me back or populate its value or handle instead of setting it myself.

Anyone plz help I am stuck at this step from past 5 days and not sure how to proceed

What I have tried:

Giving it fixed value into the function
Posted
Updated 29-Nov-20 16:30pm
Comments
Dave Kreskowiak 30-Nov-20 10:51am    
It's "Windows Forms", not "forums".

1 solution

I don't think you can use TWAIN without a window. You're going to run into that problem in the console and the service. You won't be able to use windows forms but you could P/invoke CreateWindow/CreateWindowEx from User32.dll and perhaps use that.

... inside service or program class:
C#
IntPtr32 _handle;


... inside start up function somewhere
C#
if (string.IsNullOrEmpty(className))
    className = "TwainWindow";
_wndProc = WndProc;


// Create WNDCLASS
var wndclass = new WNDCLASS();
wndclass.lpszClassName = className;
wndclass.lpfnWndProc = _wndProc;

var classAtom = RegisterClassW(ref wndclass);

var lastError = Marshal.GetLastWin32Error();

if (classAtom == 0 && lastError != ERROR_CLASS_ALREADY_EXISTS)
{
    throw new Exception("Could not register window class");
}

// Create window
_handle = CreateWindowExW(
    0,
    wndclass.lpszClassName,
    String.Empty,
    0,
    0,
    0,
    0,
    0,
    IntPtr.Zero,
    IntPtr.Zero,
    IntPtr.Zero,
    IntPtr.Zero
);
}


That should create an invisible window

I didn't post the complete code here because there's quite a bit to it, but you can try using my MessageWindow class to create an invisible hwnd for you.

It's in this code.
Understanding Windows Message Queues for the C# Developer[^]

You'll have to modify the WindowProc (i think it's called) window procedure in that code. I'll bet you a dollar TWAIN will send special messages to that routine which you'll treat as TWAIN "events" and process them. If it doesn't do that then the TWAIN libraries themselves create a UI and you are out of luck.

I haven't tested the idea within the service: Usually to create *visible* windows you must allow your service to interact with the desktop. I *think* invisible windows are fine.
 
Share this answer
 
v4
Comments
htanmsa id 30-Nov-20 20:19pm    
Sorry for being a beginner(no clue about handle nature) but could you please help me understand why we even need to create a window since the dll will have its own window created. I believe I need that handle to send commands when user clicks
According to this article it will pop up user interface.

https://www.codeproject.com/Articles/991207/TWAINComm-A-Csharp-TWAIN-Communications-Library

So I want to hook up into Twain dll user interface which will pop up after sending the msgopendsm in select function.
What exactly the input to init(this.handle) function should be (shouldn’t it be some empty variable or something which will get populated by dll handle) since I need the interface handler for further functioning which will be popped up at later point

DSMparent( appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.OpenDSM, ref hwndp );

Also to use the dll interface window does it need the system.window.forums to be declared in your program. I believe we can’t use it in console or api / windows service. For some reason when I’m trying to include windows forums assembly reference in .net core console app my terminal output window vanishes (no clue why again begineer). Is it because console app is only static unlike window dynamic nature so we can’t use
honey the codewitch 30-Nov-20 20:36pm    
You can't use windows forms in core 3.0. Either way you're going to need to create a window from the looks of it. It might not need to be visible. That, and the code I offered is what I have. If you want to know about TWAIN you'll just have to study it as I don't know anything about it.
htanmsa id 11-Dec-20 19:57pm    
Thanks to your post. I was able to graduate from c# noob to beginner(used to have so many misconceptions before on windows handle and message que system ). I have understood a lot of basics which I was lacking . Still haven’t made a complete solution but least now I know how to proceed in my journey. Just want to express my thanks again
honey the codewitch 11-Dec-20 19:59pm    
Wonderful! I'm so glad when my stuff can help someone. We all start somewhere. Good luck with your project!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900