(C) Windows Handles and Createprocess

415 views Asked by At

Ive been trying to grasp the idea of handle inheritance and application memory is shared on windows compared to unix.

Since windows does not provide a fork(), or anything similar in fact, ive been reading into createprocess and handles.

I still cant get my head around how new process can read the handles passed to them from their parents. Ive looked at the msdn docs and it seems like ReadFile is the answer here but not sure how to read it.

What im trying to do, is client/server with server passing on socket. Im using Winsock1 so WSAduplicate is not an option. I read that sockets cant be passed, but they can be duplicated. (http://tangentsoft.net/wskfaq/articles/passing-sockets.html)

So long story short, lets say i pass a handle value (ex. 0x0000070) as a command line argument, how do i have that converted into a valid handle (ie Socket).

Cheers,

**Update I know the approach is not ideal and threads are a much better way of tackling this. But it has been explicitly requested from us to create child processes instead of threading.

2

There are 2 answers

1
JackCColeman On

Depending on the exact nature of your application, the typical design is to use multiple threads within a process. Then items like a socket can be stored in global or pass the address of the socket, without having to worry about inheritance issues.

Remember, a server typically runs as a separate process and server code is usually quite different than client code.

Finally, at the risk of downvotes, there is nothing special about fork. In fact, it is a low grade way to create a second process. If you read the code for a module that is forked, it just looks weird!!

0
Harry Johnston On

The integral value of an inherited handle is the same as it was in the parent process. So all you do is convert the integer to a handle:

HANDLE h = (HANDLE) handle_value;