Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use my Android phone as barcode scanner,

About Android App:
It works on WiFi, both PC and Android is connected to same WiFi connection and than android sends scanned data to PC. It works fine with windows notepad but not with c# windows form application.
Is there any code i have to write or just configure app correctly...?
Posted
Comments
bowlturner 8-Jul-14 14:03pm    
if it works in notepad, I would guess the focus just isn't in the right place?
Sergey Alexandrovich Kryukov 8-Jul-14 14:39pm    
You are right. However, one can use more fundamental and reliable approach; please see my answer.
—SA
Ghalib Mirza 10-Jul-14 5:06am    
You r right mostly i loose focus on textbox..thnx
Mohammad Reza Valadkhani 9-Jul-14 3:24am    
could you tell me how it sends the scanned data to pc, you can read it by tcp listener on your C# application for example.
Ghalib Mirza 10-Jul-14 5:02am    
there are two ways that android app provides is
through WiFi Connection.
or through Bluetooth connection..
AND
i was thinking of using tcp listeners as u mentioned but after reading Sir Alexandrovich's solution i came to know that i m using keyboard wedge technology that directly transfer scanned information to the focused control on your c# application..

"It works fine with windows notepad" means that you are using so called "keyboard wedge": http://en.wikipedia.org/wiki/Barcode_scanner#Keyboard_wedges[^].

As bowlturner correctly pointed out, you need to keep focus on some control of your application. However, this is not so reliable way, because any other application can always grab focus if the user activates its main window. Probably, the purpose of keyboard wedge design was meant to be simplicity for development, but in fact, is just creates problems like that. An, if you really want to make a reliable solution, this is not simple at all: you will need to set a global Windows hook on keyboard events and capture the scanner events. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^].

As this is native Windows API, you will need to export some functions and types to CLI. You can do it using P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

However, this might be not the best way. Here is why: first of all, you will need a global, not a local hook. According to Microsoft, you cannot set a global hook in a CLI (managed) application; instead, you will need to create a native DLL for this purpose. Your application will need to load that DLL and organize some communication with the hook. So, C# won't be enough anyway. I don't know what native-platform language would you prefer, but I would advise using C++/CLI, because you can reach all goals at once: you can create a native DLL to set the hook, you can use the same project for managed (CLI) code if you create a mixed-mode (managed+unmanaged) project. Such project will make you a DLL which can contain unmanaged code, but, from the point of view of .NET, serve as main executable module of a regular .NET assembly which you can reference in any .NET assembly, for example, the one written in C#.

C++/CLI is standardized under ECMA-372. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

See also: http://msdn.microsoft.com/en-us/magazine/cc188966.aspx[^].

And see also my past answers on related topics:
How the Keyboard Hook works and how it is being implemented[^],
How to set a window hook in other application?[^],
Creating global shortcuts.[^].

—SA
 
Share this answer
 
v2
Comments
George Jonsson 8-Jul-14 22:51pm    
My 5
Sergey Alexandrovich Kryukov 9-Jul-14 0:31am    
Thank you, George.
—SA
Ghalib Mirza 10-Jul-14 5:25am    
You are right sir Alex i was wondering to know why and how it is so simple that my scanned information is displayed in windows notepad without doing hands dirty with c# code.. ??
now i came to know how its possible because i use keyboard wedge technology....previously i only know about scan by taking picture.... thnx once again sir.. !!!
Sergey Alexandrovich Kryukov 10-Jul-14 11:23am    
Sorry, you are talking to someone else. This is not my name, no part of it.

You are very welcome.
Good luck, call again.

—SA
As a complement to Sergey's answer there are some good CodeProject articles you can read.

Global System Hooks in .NET[^]

Using Window Messages to Implement Global System Hooks in C#[^]

This is not a trivial matter and there are lots of pitfalls, for example if you have a threaded solution you will run into difficulties.

If you don't feel confident about this approach you can always use a TextBox and make sure the focus is on that control, but it basically only works in reality if you only have one window open and continuously return focus to the TextBox.
Not the best of solutions, but it might work.
 
Share this answer
 
Comments
George Jonsson 8-Jul-14 23:50pm    
Oops, just saw that Sergey already referenced the first article.
Ghalib Mirza 10-Jul-14 5:21am    
Thanx Sir George .. Your given information is also important for me atleast for an else case senario..thnx once again..!!!

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