CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2023

    How to run WndProc and pcap_loop callbacks

    After writing a console based app that uses Npcap libs to process packets, I've started on a Windows desktop app using VS 2022 C++ Win32 API which, at the moment, just displays packet source and destination IPs in a listbox. I have 2 buttons; Start and Stop which starts and stops the pcap_loop callback function.
    When I click the Start button, packets are displayed in the Listbox OK, but clicking the Stop button takes a few clicks and a few seconds to respond and when it does, an exception occurs - Access violation at the pcap_loop function call.
    I'm thinking this is because the pcap_loop is a callback function and is running at the same time as the WndProc callback function that processes Window messages, but correct me if I'm wrong.

    WinMain has the standard code:
    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))

    If my thinking is right, how can I run the 2 callback functions and gracefully stop the packet capture in the pcap_loop callback?

    Thanks for any advice.

  2. #2
    Join Date
    Nov 2018

    Re: How to run WndProc and pcap_loop callbacks

    Did you create a separate thread to handle pcap_loop?

    > When I click the Start button, packets are displayed in the Listbox OK,
    Generally, accessing the UI from a non-UI thread is a bad idea.
    You might get short-term "it works", but eventually it all falls apart.

    Your non-UI thread need to send some kind of message to the UI telling it 'here is a new pair of IP addresses'.

    There are a couple of ways to do this.

    1. Use a pipe
    The pcap thread queues messages on the pipe, and the UI thread reads from it under the control of say a WM_TIMER.

    2. Post messages
    You create a new user message ID that your UI thread understands as meaning 'new IP pair'.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: How to run WndProc and pcap_loop callbacks

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Mar 2023

    Re: How to run WndProc and pcap_loop callbacks

    Sorry for the delay in replying, I've been offline a bit. I haven't created a separate thread for pcap_loop yet, I was looking for advice on the best approach and I wanted opinions of threads before starting further development. I'll learn a bit about threads but start looking at pipes and how to post messages if they are a better option.

    Thanks @salem_c and @2kaud for replying, I appreciate it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width