Solved WPF API hook to class library

May 3, 2014 at 17:58:06
Specs: Windows 7, 2.4ghz xenon
I have a sample API from the a bar code scanner I have and the code is in a WPF form. I would like to make it into a class library for easier integration into my app. They are overriding the "OnSourceInitalized" and adding a hook. I have researched around and I am unable to figure out how to do this without a WPF window.

API Code:

protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            string barcode;
            string s;
            Int32 prm;
            int index;

            if (msg == msgint_raw)
            {
                m_Isdc.GetRawData(BarcodeBuffer,out nBytesReturned);
                barcode = "";
                Encoding ascii = Encoding.ASCII;
                s = ascii.GetString(BarcodeBuffer);
                for (int i = 0; i < nBytesReturned; i++)
                {
                    if ((s[i]>0x1F)&&(s[i]<0x7F))
                    barcode += s[i].ToString();
                }
                listBox1.Items.Add(barcode);
            }

            if (msg == msgint_iscp)
            {
                prm = wParam.ToInt32();
                if (prm == 0x71)
                {
                    m_Isdc.GetImageData(ImageBuffer, out nBytesReturned);
                    DisplayImage(ImageBuffer, nBytesReturned);
                }

                if (prm == 0x64)
                {
                    m_Isdc.GetBarcodeDataEx(BarcodeBuffer, out nBytesReturned);
                    barcode = "";
                    Encoding ascii = Encoding.ASCII;
                    s = ascii.GetString(BarcodeBuffer);
                    for (index = 0; index < BarcodeBuffer[8]; index++)
                        barcode += s[13+index].ToString();
                    listBox1.Items.Add(barcode);
                }
                
            }
            return IntPtr.Zero;
        }


See More: WPF API hook to class library

Report •


#1
May 6, 2014 at 07:00:55
I've seen scanners that require a message pump to work. It looks like this .NET API is designed to use those. You can make a message loop with Application.Run(), but I'm not sure if this code'll let you get away with that.

Your other option is to complain to the manufacturer.

How To Ask Questions The Smart Way


Report •

#2
May 6, 2014 at 07:15:09
I have complained to the manufacture (intermect\honeywell) and they do not seem to care. Unfortunately it is the only scanner of its kind (Long range 2D). The original API seems to be written in C++ and they made a .NET wrapper. Looking at some of the C++ SDK examples it is a little beyond my skill set. Is there a way to make the same hook call in a class library as they have done in the WPF form? I might just have to give up on integrating it to the app and set the scanner up as a HID which wouldn't be as clean but definitely do-able.

Report •

#3
May 6, 2014 at 12:54:03
✔ Best Answer
This is sample code, right? All it's showing you is how to interact with your message pump in a WPF application, and what you should do in that message pump. It's possible the example also shows how to register for those messages, but I assume the scanner drivers are just broadcasting the messages. Path of least resistance, and all that.

So let's strip away the layers of .NET abstraction, and look at what a Win32 app would need, because .NET doesn't negate the requirements of Win32. Be forewarned, my knowledge on this stuff is over 8 years old. I doubt it's changed that much, but don't exactly ask for working code samples.

So, what would a native app need? Well, you need a way to get the data from the drivers. In the code sample you provided, that appears to be done by m_Isdc. There's 1/2 of the job done.

The drivers are broadcasting messages out, so you'd need a message pump to catch those broadcasts, and your message pump needs to look for those messages. That code sample shows you how to tap in to your existing WPF message pump. I assume there's no WinForms example 'cause you'd just override your WndProc.

WPF isn't the relevant part. A message pump is all you need. If your application won't have a GUI, or you want to separate out the GUI from the I/O, you'll need to implement your own pump. Ideally not on the GUI thread. Easiest way to do this is to inherit a System.Windows.Forms.Form and System.Windows.Forms.ApplicationContext, override the WndProc and constructor respectively, then call your context from Application.Run. Optional: wrap all of this up in its own thread. Now you have to deal with multi-threading, so good luck with that.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •
Related Solutions


Ask Question