开发者

Problem with passing object as an argument to createProcess

I am trying to correct a program that makes to other programs with the createProces call.

The problem is when I pass an object of the Brick class as a parameter of the createProcess call.

I create the object (in the main) this way:

char IpApplicationName[1000];

STARTUPINFO StartInfo;
PROCESS_INFORMATION ProcessInfo;

strcpy(IpApplicationName, "c:\\Documents and Settings\\Eigenaar\\Bureaublad\\BluetoothTestr\\recvProc\\bin\\Debug\\recvProc.exe");

//set up the NXT
Connection *connection = new Bluetooth();
Brick *nxt = new Brick(connection);
char *nxt_ptr = (char *)&nxt;

Then I connect like this (6 is the comm port of the bluetooth dongle):

connection->connect(6);

CreateProcess(IpApplicationName, nxt_ptr, NULL, NULL, FALSE,    CREATE_NEW_CONSOLE, NULL, NULL,     &StartInfo, &ProcessInfo);

This all works fine I think, but the problem is when I cast the char* back to the Brick class in the recvProc.exe process like this:

Brick *nxt = (Brick*)argv[0];

If I comment th开发者_开发技巧is, then the program works fine... What is wrong with this line? Or do I need to pass the Connection object in createProcess?


You are passing, as a command line argument, a pointer to a pointer to a class. This is broken in quite a few ways:

  • CreateProcess's lpCommandLine argument takes a text string. There cannot be embedded NUL bytes. There must be a NUL byte to mark the end of the string. No such guarentee is present with a pointer-to-a-pointer, or indeed with a pointer or any sort of non-text-data in general.
  • Even if the pointer, by some miracle, was passed properly to the new process, the new process has its own address space. It cannot use pointers from the old process, at all; they are meaningless in a process different from that which they were created. So the argument is meaningless.
  • Even if it could access the old process's address space, there is nothing stopping the pointed-to brick from being destroyed before the new process completes its work.
  • Even if all these were fixed, you're passing a pointer-to-a-pointer-to-a-brick, and trying to use it as a regular pointer-to-a-brick.

In short, you can't pass objects as command-line arguments. Only text.

So, what do do here? Here are your options:

  • Have the child process connect to the brick on its own. Don't try to pass it a connection.
  • Set up a protocol of some sort (over stdin/stdout, or possibly DCOM) to allow the child process to remote control the Brick object in the parent.
  • Make recvProc into a DLL that runs inside the parent process.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜