Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Use event from C++ library in C# application

Use event from C++ library in C# application

Scheduled Pinned Locked Moved C#
csharpc++question
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #1

    Hi all, I have written a C++ library which communicates with an external device. The device sends back certain X and Y coordinates. I am trying to send back these X and Y coordinates to a calling c# application, but I don't want to poll the library with a thread or a loop. Is there a way I can create an event in the C++ library and register to it from the c# application, so that when the X and Y coordinates have values, the event will be fired in the c# application? Many thanks in advance. Kind regards,

    L K 2 Replies Last reply
    0
    • P Programm3r

      Hi all, I have written a C++ library which communicates with an external device. The device sends back certain X and Y coordinates. I am trying to send back these X and Y coordinates to a calling c# application, but I don't want to poll the library with a thread or a loop. Is there a way I can create an event in the C++ library and register to it from the c# application, so that when the X and Y coordinates have values, the event will be fired in the c# application? Many thanks in advance. Kind regards,

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      I assume you are using P/Invoke; if you somehow get a signal (such as an interrupt) in your native code, all you need is a way to call a managed method; this is known as a callback. You can see an example in this article[^]. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      P 2 Replies Last reply
      0
      • L Luc Pattyn

        I assume you are using P/Invoke; if you somehow get a signal (such as an interrupt) in your native code, all you need is a way to call a managed method; this is known as a callback. You can see an example in this article[^]. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #3

        Hi Luc, Thanks for the reply. I have heard about P/Invoke, but wasn't sure about how to go about using it. But many thanks for the link, I think the delegate section is what I needed. Kind regards,

        L 1 Reply Last reply
        0
        • P Programm3r

          Hi Luc, Thanks for the reply. I have heard about P/Invoke, but wasn't sure about how to go about using it. But many thanks for the link, I think the delegate section is what I needed. Kind regards,

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          You're welcome. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          1 Reply Last reply
          0
          • P Programm3r

            Hi all, I have written a C++ library which communicates with an external device. The device sends back certain X and Y coordinates. I am trying to send back these X and Y coordinates to a calling c# application, but I don't want to poll the library with a thread or a loop. Is there a way I can create an event in the C++ library and register to it from the c# application, so that when the X and Y coordinates have values, the event will be fired in the c# application? Many thanks in advance. Kind regards,

            K Offline
            K Offline
            KarstenK
            wrote on last edited by
            #5

            with Microsoft.Win32.SafeHandles.SafeWaitHandle you can use C++ Handles in C#. -> AutoResetEvent

            Press F1 for help or google it. Greetings from Germany

            P 1 Reply Last reply
            0
            • L Luc Pattyn

              I assume you are using P/Invoke; if you somehow get a signal (such as an interrupt) in your native code, all you need is a way to call a managed method; this is known as a callback. You can see an example in this article[^]. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              P Offline
              P Offline
              Programm3r
              wrote on last edited by
              #6

              Hi Luc, After some testing, I found myself back at square one, reason being that the example does not illustrate what happens within the c++ section of the code. The author makes use of standard windows methods and I have no idea what they are doing in the method. More specifically, the EnumWindows method. I must create my own method so that I can receive the X and Y coordinates (and that is what the CALLBACK is for ... i guess), thus what parameters must I send to the c++ method? I apologize for my ignorance but I'm in the dark here :doh: :confused: C++ SectionBOOL EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam ); BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam ); BOOL IsWindowVisible( HWND hWnd );
              C# Sectionclass DelegateExample { private List<IntPtr> windowList; public List<IntPtr> GetVisibleWindowHandles() { windowList=new List<IntPtr>(); EnumWindows(new EnumWindowsProc(CollectVisibleWindows), IntPtr.Zero); Console.WriteLine("There are {0} visible windows", windowList.Count); return windowList; } private bool CollectVisibleWindows(IntPtr hWnd, IntPtr lParam) { if(IsWindowVisible(hWnd)) windowList.Add(hWnd); return true; // please continue enumeration ! } [DllImport("user32.dll")] private static extern int EnumWindows(LP_EnumWindowsProc ewp, IntPtr lParam); private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr hWnd); }
              Many thanks in advance. Kind regards,

              L 1 Reply Last reply
              0
              • K KarstenK

                with Microsoft.Win32.SafeHandles.SafeWaitHandle you can use C++ Handles in C#. -> AutoResetEvent

                Press F1 for help or google it. Greetings from Germany

                P Offline
                P Offline
                Programm3r
                wrote on last edited by
                #7

                Hi Karsten, Could you please elaborate on your answer? Many thanks. Kind regards,

                1 Reply Last reply
                0
                • P Programm3r

                  Hi Luc, After some testing, I found myself back at square one, reason being that the example does not illustrate what happens within the c++ section of the code. The author makes use of standard windows methods and I have no idea what they are doing in the method. More specifically, the EnumWindows method. I must create my own method so that I can receive the X and Y coordinates (and that is what the CALLBACK is for ... i guess), thus what parameters must I send to the c++ method? I apologize for my ignorance but I'm in the dark here :doh: :confused: C++ SectionBOOL EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam ); BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam ); BOOL IsWindowVisible( HWND hWnd );
                  C# Sectionclass DelegateExample { private List<IntPtr> windowList; public List<IntPtr> GetVisibleWindowHandles() { windowList=new List<IntPtr>(); EnumWindows(new EnumWindowsProc(CollectVisibleWindows), IntPtr.Zero); Console.WriteLine("There are {0} visible windows", windowList.Count); return windowList; } private bool CollectVisibleWindows(IntPtr hWnd, IntPtr lParam) { if(IsWindowVisible(hWnd)) windowList.Add(hWnd); return true; // please continue enumeration ! } [DllImport("user32.dll")] private static extern int EnumWindows(LP_EnumWindowsProc ewp, IntPtr lParam); private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr hWnd); }
                  Many thanks in advance. Kind regards,

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  OK, it seems you are having two problems: 1. you don't want to poll, i.e. periodically read and process the data. You want something that is event driven, a cause-and-effect kind of thing. That is possible if and only if your peripheral is giving you a cause, maybe by issuing an interrupt; or by sending data. If not, all you can do is have a polling loop. 2. Calling managed code from unmanaged code, is called a callback. There are at least two examples in my article, the more informative one may well be the "Two-side logging" which includes all the code involved for both parties. Hope that helps.

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  P 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    OK, it seems you are having two problems: 1. you don't want to poll, i.e. periodically read and process the data. You want something that is event driven, a cause-and-effect kind of thing. That is possible if and only if your peripheral is giving you a cause, maybe by issuing an interrupt; or by sending data. If not, all you can do is have a polling loop. 2. Calling managed code from unmanaged code, is called a callback. There are at least two examples in my article, the more informative one may well be the "Two-side logging" which includes all the code involved for both parties. Hope that helps.

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    P Offline
                    P Offline
                    Programm3r
                    wrote on last edited by
                    #9

                    Thanks again Luc. P.s the demo application link does not work. Kind regards,

                    L 1 Reply Last reply
                    0
                    • P Programm3r

                      Thanks again Luc. P.s the demo application link does not work. Kind regards,

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      Programm3r wrote:

                      link does not work

                      You're right, that is one of the reasons it still is Work In Progress. However, the relevant stuff is in the article itself. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups