Windows via C/C++

Category: Operating Systems
Author: Jeffrey Richter, Christophe Nasarre
All Stack Overflow 15
This Month Stack Overflow 4


by anonymous   2019-07-21

You suggest two methods of doing overlapped I/O and ignore the third (or I'm misunderstanding your question).

When you issue an overlapped operation, a WSARecv() for example, you can specify an OVERLAPPED structure which contains an event and you can wait for that event to be signalled to indicate the overlapped I/O has completed. This, I assume, is your WaitForMultipleObjects() approach and, as previously mentioned, this doesn't scale well as you're limited to the number of handles that you can pass to WaitForMultipleObjects().

Alternatively you can pass a completion routine which is called when completion occurs. This is known as 'alertable I/O' and requires that the thread that issued the WSARecv() call is in an 'alertable' state for the completion routine to be called. Threads can put themselves in an alertable state in several ways (calling SleepEx() or the various EX versions of the Wait functions, etc). The Richter book that I have open in front of me says "I have worked with alertable I/O quite a bit, and I'll be the first to tell you that alertable I/O is horrible and should be avoided". Enough said IMHO.

There's a third way, before issuing the call you should associate the handle that you want to do overlapped I/O on with a completion port. You then create a pool of threads which service this completion port by calling GetQueuedCompletionStatus() and looping. You issue your WSARecv() with an OVERLAPPED structure WITHOUT an event in it and when the I/O completes the completion pops out of GetQueuedCompletionStatus() on one of your I/O pool threads and can be handled there.

As previously mentioned, Vista/Server 2008 have cleaned up how IOCPs work a little and removed the problem whereby you had to make sure that the thread that issued the overlapped request continued to run until the request completed. Link to a reference to that can be found here. But this problem is easy to work around anyway; you simply marshal the WSARecv over to one of your I/O pool threads using the same IOCP that you use for completions...

Anyway, IMHO using IOCPs is the best way to do overlapped I/O. Yes, getting your head around the overlapped/async nature of the calls can take a little time at the start but it's well worth it as the system scales very well and offers a simple "fire and forget" method of dealing with overlapped operations.

If you need some sample code to get you going then I have several articles on writing IO completion port systems and a heap of free code that provides a real-world framework for high performance servers; see here.

As an aside; IMHO, you really should read "Windows Via C/C++ (PRO-Developer)" by Jeffrey Richter and Christophe Nasarre as it deals will all you need to know about overlapped I/O and most other advanced windows platform techniques and APIs.

by graham.reeds   2019-07-21

I always throw an exception with a message of where it occurred and what caused it to happen:

throw NException("Foo::Bar", "Mungulator cause a stack overflow!");

You can then use these strings in messageboxes etc.

I always catch via

catch (NException& ex) { ... }

If you running windows you can pass the error value and have a function derive the error message. The best example of this is in Windows via C/C++ by Jeffrey Richter.

by John T   2019-07-21

For rapid application development on windows, .NET seems to be the most efficient solution nowadays. But If you crave that extra ounce of performance out of your applications, I'd recommend giving Programming Windows and Windows via C/C++ a thorough read. Although some of the material is dated, a lot of it is still relevant with windows programming today. If you choose the .NET route some books to take a look at include Illustrated C# 2008, Accelerated C# 2008, and of course our own Jon Skeet's C# in Depth.

by user257111   2019-07-21

I can answer part one fairly simply - it depends on your platform. If you're using Win32 API, take a look at "CreateThread" function and read up on examples. The book I read on multi-threading on Windows was this one: which covers not just threading with CreateThread and the other option BeginThread but also locks and semaphones etc.

If you're using Linux, you'll want POSIX Threads via the pthread function, see as an example.

A code example for pthreads looks like this - be careful, I've left in functionality for creating several threads from the same function i.e. callocing an array of pthread_t variables. You might not need this.

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <malloc.h>

void *thread_function(void *arg) 
        /* DO SOME STUFF */

        /* Exit Thread */

int main(int argc, char** argv)
        /* variables */
        int retval = 0;
        /* array of thread handles */
    pthread_t* thread_handle = (pthread_t*) calloc(1, sizeof(pthread_t));;

        /* create function - fork() for threads */
    retval = pthread_create(&thread_handle[0], NULL, thread_function, NULL);

        /* DO SOME STUFF */

        /* join - wait for thread to finish */ 
        pthread_join(thread_handle[0], NULL); 

    return EXIT_SUCCESS;

Compile with gcc filename -o fileexe -lpthread

by anonymous   2017-08-20

IOCP is a feature that has been in Windows since the dark ages and has changed little in years since. As such, any samples etc. from 5+ years ago should still work pretty well today.

MSDN has some documentation on IOCP:

Mark Russinovich also wrote up a great intro into IOCP:

Mark also wrote a more thorough description of Windows' IO infrastructure in "Windows Internals" which is essential reading.

I also strongly recommend Jeffery Richter's "Windows via C/C++" which is also essential reading for anyone embarking on lower-level Windows programming.


by anonymous   2017-08-20

Jeffrey Richter's book Windows via C/C++ is a good one. It goes over way more than just Windows Services though.

If you have Visual Studio Pro (or Visual C++ Express), you can dig into the ATL classes around CAtlServiceModuleT. While it is mostly about exposing DCOM interfaces, I've used those classes to write stand-alone Win32 services.

There is also this (old) article on MSDN.

by anonymous   2017-08-20

Hooking a (Win)API functions is a general solution for such tasks but I suppose this is just a tip of the iceberg. There is little note about it in wiki (Detection subsection). Thus, you need to learn about how to hook API functions and about Windows internals in general. I would suggest Windows via C++ as a good start point for solving this global problem.