-
April 13th, 2024, 04:35 AM
#1
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))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
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.
-
April 14th, 2024, 01:13 AM
#2
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.
https://learn.microsoft.com/en-us/wi...in32/ipc/pipes
https://learn.microsoft.com/en-us/wi...inmsg/wm-timer
2. Post messages
You create a new user message ID that your UI thread understands as meaning 'new IP pair'.
https://learn.microsoft.com/en-us/wi...threadmessagea
-
April 14th, 2024, 04:23 AM
#3
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 https://creativecommons.org/publicdomain/zero/1.0/ 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)
-
April 21st, 2024, 06:56 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|