views:

178

answers:

7

My native C++ Win32 program spawns a worker process and needs to pass a huge configuration string to it. Currently it just passes the string as a command line to CreateProcess(). The problem is the string is getting longer and now it doesn't fit into the 32K characters limitation imposed by Windows.

Of course I could do something like complicating the worker process start - I use the RPC server in it anyway and I could introduce an RPC request for passing the configuration string, but this will require a lot of changes and make the solution not so reliable. Saving the data into a file for passing is also not very elegant - the file could be left on the filesystem and become garbage.

What other simple ways are there for passing long strings to a worker process started by my program on Windows?

A: 

Is it a possibility to set up a named shared memory segment?

http://msdn.microsoft.com/en-us/library/aa366551(VS.85).aspx

Anders Abel
+1  A: 

Use shared memory. Pass to a worker process name of shared memory object. Another solution is to use WM_COPYDATA message.

Kirill V. Lyadvinsky
+6  A: 

One possible strategy is to create a named Pipe and pass the handle ( or pipe name) to the other process. Then use normal Read\Write operations on Pipe to extract the data.

aJ
+6  A: 

There are several good answers already, but the easiest way is to save it in a file, and pass the filename in the command line.

As well as being simple, an advantage of this approach is that the apps will be very loosely coupled (you'll potentially be able to use the child application stand-alone in other ways, rather than always having to launch it from a program that knows how to pipe data into it via a specialised interface)

If you want to be sure that the file is cleaned up after processing, mark it for deletion on the next reboot. THen if anybody forgets to clean it up, the OS will deal with it for you on the next reboot.

Jason Williams
A: 

How about reading it from stdin :) It seems to work for the Unix folks.

Guaranteed a lot easier than passing pipe names/handles around!

Here is some official code from MSDN for creating child processes with I/O pipes.

spong
A: 
nhaa123
A: 

You could use an inheritable handle to a section object. In your parent process create a section object (CreateFileMapping) and specify that its handle is to be inherited by the child process; then pass the handle value to the child process on the command line. The child process can then open the section object (OpenFileMapping). Though I would prefer a named section object as the semantics of using it are easier to understand.

Luke