15,979
社区成员
发帖
与我相关
我的任务
分享
/**********************************************************************
// 描 述: 打开另一个进程
// 返 回: BOOL
// 参 数: LPCTSTR pszCmdLine 包含程序路径名称和参数的命令行
//**********************************************************************/
BOOL OpenProgram(LPCTSTR pszCmdLine)
{
ASSERT(pszCmdLine);
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
(LPTSTR)pszCmdLine, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
return FALSE;
}
return TRUE;
}
//调用这个函数打开B.exe
//注:CreateProcess()函数中,第一个参数本来应该是执行程序名称,它可以为NULL
// 如果为NULL,则第二个参数一定要先写上执行程序全路径。
// 如果执行程序路径中包含空格,则执行程序前后一定要加上双引号
char szCmdLine[512] = {0};
strcat(szCmdLine, "\""); //前面加上双引号
strcat(szCmdLine, "C:\\B.exe");
strcat(szCmdLine, "\" "); //后面加上双引号
strcat(szCmdLine, "\""); //参数前面加上双引号
char szParam[64];
memset(szParam, 0, sizeof(szParam));
//下面这些值 是根据你的A.exe程序的大小来的,nOrgX,nOrgY :A的左上角坐标,后面两个是宽度和高度
int nOrgX = 23, nOrgY = 24, nWidth = 400, nHeigth = 600;
CString strTemp;
strTemp.Format("%d-%d-%d-%d",nOrgX,nOrgY,nWidth,nHeight);
strcat(szCmdLine, strTemp.GetBuffer(strTemp.GetLength()));
strTemp.ReleaseBuffer();
strcat(szCmdLine, "\" ");
//B.exe启动的时候 解析命令行参数,来决定自身应用程序的大小。
if (__argc==2)
{
CString strParam = __targv[1];//[0]是应用程序的路径,
//[1]这里就是A传进来的"23-24-400-600"
int nLeft = strParam.Find('-');
nWndOrgX = strParam.Left(nLeft);
nRigth = strParam.Find('-',nLeft+1);
nWndOrgY = strParam.Mid(nLeft+1,nRigth-nLeft-1);
nLeft = nRigth;
nRigth = strParam.Find('-',nLeft+1);
nWidth = strParam.Mid(nLeft+1,nRigth-nLeft-1);
nHeigth = strParam.Right(strParam.GetLength()-nRight-1);
//这些值都是A.exe程序的位置及大小,B程序可以根据这些值来调整自身窗口大小。
用SetWindowPos();//函数,具体参数请查看MSDN
//以上字符串的操作法,大家自己写的时候请注意检查参数,我在这没有判断,直接就把
//Find到的'-'的Index传进去了,实践过程中可能由于不同的原因可能没有找到,会返回-1
//需要判断,一旦把负值传进去,就不对了。
}