Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings! it's me again!

This question is quite hard, for I'll do my best explaining it: As I mentioned in a previous question, I'm working in a scanner management on C#, using a C++ dll sent by provider. According to the API's manual, there are certain messages sent under certain conditions. In example: After Starting up the scanner, it should send the message DEVICE_CONNECTED (with a value of 0), and then change it state.

Those messages values are defined in the .dll

My question is: How can I catch those messages with my C# project?

What I have tried:

I been looking for information about messages transfering, and I found out there's a WndProc that processes Windows messages. Then I tried doing this:

C#
private const int DEVICE_CONNECTED = 0;
/*<<<<< Some code >>>>>*/     [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
    if (m.Msg == DEVICE_CONNECTED)
       listBox1.Items.Add("Connected");
    base.WndProc(ref m);
}


Of course it fails. I suppose I need to define, somehow, the messages source. Reading further there's another thing called WMI, but after checking the info at MicrosoftDocs... I feel they're talking in chinese or something...

So any good guindance? Suggestions?

Thanks in advance


///////////////////////////////////

Edited:

OK, after a long research, I found out where can I possibly get the message.

I imported previously another function form that dll:

C#
[DllImport(path, EntryPoint = "?StartUp@@YGKPAUHWND__@@I@Z")]
public static extern int StartUp(IntPtr HWMD, uint StMsg);


That one allows me to turn on scanner.

inside of the .dll is defined like this:

C++
DWORD StartUp( HWND Handle, UINT SorterMessage )


And in the manual says: "HWND Handle – the handle to the application’s messages destination window."

So, if I undertstood correrctly (english is not my main language) I got a pointer from where I should get those messages.

Now here comes another question: How can I use that pointer?

Tried following this example I found in another forum:

C#
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct MSG
{
    public IntPtr hwnd;
    public int message;
    public IntPtr wParam;
    public IntPtr lParam;
    public int time;
    public int pt_x;
    public int pt_y;
};

[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern bool GetMessage([In, Out] ref MSG msg, IntPtr hWnd, int uMsgFilterMin, int uMsgFilterMax);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr DispatchMessage([In] ref MSG msg);

MSG msg = new MSG();
while (GetMessage(ref msg, IntPtr.Zero, 0, 0))
    DispatchMessage(ref msg);


Tried to emulate it like this:

C#
// Added a constructor inside of the struct:
public MSG(IntPtr hwndPtr)
{
    hwnd = hwndPtr;
    message = -1;
    wParam = new IntPtr();
    lParam = new IntPtr();
    time = 0;
    pt_x = 0;
    pt_y = 0;
}

// Left the dll imports like in their example (although I fixed the path)

// Calling the method in my main
int ID, st;
ID = Class1.StartUp(hwnd, 10); // Just used 10 like in the API's manual
Console.WriteLine("Turning on device");
MSG msg = new MSG(hwnd);
while(Class1.GetMessage(ref msg, IntPtr.Zero, 0, 0))
    Class1.DispatchMessage(ref msg);
Console.WriteLine(msg.message);

do { Class1.GetState(ID, out st); }
while (st != (int) DevStates.chgParams);
Console.WriteLine("Device on");


But now I'm getting an empty response. After printing "Turning on device", cursor just blinks and program doesnt advance. It's like is waiting for something.

What am I missing?
Posted
Updated 9-Apr-19 11:51am
v4
Comments
[no name] 3-Apr-19 13:35pm    
It would help if you said what "brand" / model of scanner you are using.

"My car won't start". Is it gas or electric or propane or Flintstones ...
Member 14150748 3-Apr-19 14:35pm    
Gonna submit it, but then gonna delete this repply later or tomorrow, for confidenciality

It's a Panini Vision X.
[no name] 6-Apr-19 17:30pm    
The vendor provides a PC "app" to interface with the device; what is the point of your code? Check scanning isn't something that requires "customizing".
Member 14150748 8-Apr-19 9:01am    
The point is the client requires specific functions. Sides, vendor just provides a demo software to test the scanner. If there's no needs of programming they wouldn't send the dll

1 solution

Solved it (finally)

This is how I did it:


1) Used windows forms, since it has the class "Message"
2) Imported the .dll I was working on to make stuff easier, placed all methods in a "ScanMgr" class.

C#
using ...
using APIcsharp;

class ScanMgr
{
  int ID = 0;

  public string startUp(IntPtr hwmd, uint wmApp)
  {
    int state;
    ID = NativeAPI.StartUp(hwmd, wmApp);
    if(ID != 0)
    {
      do { NativeAPI.GetState(ID, out state); }
      while(state == (int)(DevStates.StartingUp)); // DevStates is a enum, ok? :P
      return "Device on";
    }
    return "Error turning on";
  }

  /* Other stuff to do */
}


3) Then, defined an override method for the messages

C#
public partial class Form1 : Form
{
  const uint wm_channel = 0x8000 + 1;
  ScanMgr scanner = new ScanMgr();

  public Form1()
  { InitializeComponent(); }

  private void StartBtn_Click(object sender, EventArgs e)
  { log.Items.Add(scanner.startUp(this.Handle, wm_channel)); }

  /* Other stuff yadda yadda */

  protected override void WndProc(ref Message m)
  {
    base.WndProc(ref m);
    if(m.Msg == wm_channel)
    { /* To do stuff with m.WParam and m.LParam */ }
  }
}
 
Share this answer
 
v4
Comments
Nirav Prabtani 10-Apr-19 9:21am    
Congrats,

Mark it as a solved for the solution credibilit, it will help others too
Maciej Los 10-Apr-19 16:11pm    
5ed!
Member 15998787 31-May-23 10:16am    
HI, can we have a version with wpf c# application?

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