CreateProcess的参数传递出现问题,求助

leixp3636 2010-01-06 08:58:02
父进程如下调用(hThread是一个线程的句柄,想传递到子进程中),

//参数设置
char pParam[128] = {0};
int loc = 0;
//设置子进程
memcpy(pParam, "Child.exe ", sizeof("Child.exe ") - 1);
loc += sizeof("Child.exe ") - 1;
//设置线程的句柄
memcpy(pParam + loc, &hThread, sizeof(hThread));
loc += sizeof(hThread);
memset(pParam + loc, '\0', 1);

CreateProcess(NULL, pParam, NULL, NULL, FALSE, 0, NULL, NULL, &si, &piProcess);


pParam赋值之后,如下:-
pParam 0x0012fe50 "Child.exe ?" char [128]
[0x0] 0x43 'C' char
[0x1] 0x68 'h' char
[0x2] 0x69 'i' char
[0x3] 0x6c 'l' char
[0x4] 0x64 'd' char
[0x5] 0x2e '.' char
[0x6] 0x65 'e' char
[0x7] 0x78 'x' char
[0x8] 0x65 'e' char
[0x9] 0x20 ' ' char
[0xa] 0xa0 char
[0xb] 0x0f '␏' char

子进程:

int main (int argc, TCHAR * argv[])

取argv[1]的值看,变成这样了。
argv[1][0] 0x3f '?' char
argv[1][1] 0x00 char
argv[1][2] 0xfd char
argv[1][3] 0xfd char
argv[1][4] 0xfd char


传递字符正常,但是非字符都变成'3f'了,请教各位,这里有什么问题?
小弟没分了,所以。。
...全文
315 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
leixp3636 2010-01-07
  • 打赏
  • 举报
回复
好,我再试验试验,谢谢大家的帮助
SolidRabbit 2010-01-06
  • 打赏
  • 举报
回复
我不知道CreateProcess里有没有对参数做处理,不过我刚看了crtexe.c里面的代码,那里对参数做了过滤:
1.会被'\0'截断
2.会被小于' '(空格)的字符截断

虽然,你的例子里给的句柄是不会被这么早隔断,但是一个句柄里出现0和出现小于20的字节的概率还是不小的吧。换个法子吧。
leixp3636 2010-01-06
  • 打赏
  • 举报
回复
上面还是笔误了,创建子进程时,那个参数是TRUE
CreateProcess(NULL, pParam, NULL, NULL, TRUE, 0, NULL, NULL, &si, &piProcess);
leixp3636 2010-01-06
  • 打赏
  • 举报
回复
谢谢大家。
to tttyd:
我创建的线程句柄设置的是可以被子进程继承的。
创建子进程的时候
CreateProcess(NULL, pParam, NULL, NULL, FALSE, 0, NULL, NULL, &si, &piProcess);(之前贴的那个参数是FALSE,笔误)

所以这个句柄在子进程中应该是已经复制了。

我只是想把数据传过去,按理说,不管是传句柄还是ID,数据传过去之后应该是不变的啊?

我可以保证,如果句柄数据传递正确的话,在子进程中是可以使用该句柄的(我将句柄的值硬写到子进程中实验过)。

to gkisme和SuperLy
我也有这个疑问,不过感觉这个疑问应该是不成立的,不知道是不是别的地方出什么问题了。


雪影 2010-01-06
  • 打赏
  • 举报
回复
//设置线程的句柄
memcpy(pParam + loc, &hThread, sizeof(hThread));
传递句柄啊?

刚才没仔细看,线程句柄不能这样传递的!可以传递线程ID,然后使用OpenThread
SuperLy 2010-01-06
  • 打赏
  • 举报
回复
传递字符正常,但是非字符都变成'3f'了
个人认为,进程参数应当是字符串(比如"12fcd3",而不是值0x12fcd3),非正常字符不一定能正常传递;
建议用消息SendMessage();PostMessage()传递句柄。
SolidRabbit 2010-01-06
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080724/13/0e94ee70-b055-4ad2-a41b-c4a6d0184bff.html
SolidRabbit 2010-01-06
  • 打赏
  • 举报
回复
参数好像不支持非字符串?

int main (int argc, TCHAR * argv[])


自己传字符串进来转换吧
leixp3636 2010-01-06
  • 打赏
  • 举报
回复
谢谢楼上回复。
我在lpApplicationName参数为空的时候,如果lpCommandLine的值是全字符的参数是可以传递的。

另外我刚才也实验了lpApplicationName中赋为子进程的路径,lpCommandLine还是为之前的参数。
结果在子进程中查看参数,还是和之前一样,全部为3f。
雪影 2010-01-06
  • 打赏
  • 举报
回复
BOOL WINAPI CreateProcess(
__in LPCTSTR lpApplicationName,
__in_out LPTSTR lpCommandLine,
__in LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in LPVOID lpEnvironment,
__in LPCTSTR lpCurrentDirectory,
__in LPSTARTUPINFO lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation
);
If both lpApplicationName and lpCommandLine are non-NULL, the null-terminated string pointed to by lpApplicationName specifies the module to execute, and the null-terminated string pointed to by lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. Console processes written in C can use the argc and argv arguments to parse the command line. Because argv[0] is the module name, C programmers generally repeat the module name as the first token in the command line.

If lpApplicationName is NULL, the first white-space – delimited token of the command line specifies the module name. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin (see the explanation for the lpApplicationName parameter). If the file name does not contain an extension, .exe is appended. Therefore, if the file name extension is .com, this parameter must include the .com extension. If the file name ends in a period (.) with no extension, or if the file name contains a path, .exe is not appended. If the file name does not contain a directory path, the system searches for the executable file in the following sequence:

只有当lpApplicationName为非空时,子进程才能获取父进程的输入参数

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧