我做了一个exe程序,要放到光盘上,插入光盘后自动播放。程序名AutoApp,在InitInsttance中注册此程序需要使用的rty.ocx.等注册成功后,跳出主对话框。问题是:
 自动播放光盘时注册OCX的方法regocx()执行注册是失败的。进到光盘目录中,双击打开的时候regocx()执行注册是成功的。
 代码如下:BOOL AutoApp::InitInstance()
{
AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
//  of your final executable, you should remove from the following
//  the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif InitLog();
if( !RegOCX() )// 注册ocx有wait操作
theApp.WriteLog("RegOCX失败");
CPAutoDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
//  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
//  dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
//  application, rather than start the application's message pump.
return FALSE;
}RegOCX:BOOL AutoApp::RegOCX()
{
CString strAppPath;
GetAppPath(strAppPath);
        CString strOcxPath = strAppPath + "\\" + "rty.ocx";
        if ( !CreateAndWaitProcess("c:\\windows\\system32\\regsvr32.exe",strOcxPath,5*1000) )
if ( !CreateAndWaitProcess("c:\\windows\\system32\\regsvr32.exe","/s rty.ocx",5*1000) )
return FALSE;
}CreateAndWaitProcess:BOOL CAutoApp::CreateAndWaitProcess(CString strAppName, CString strCMDLine, DWORD dwMillSeconds)
{
STARTUPINFO si;
memset(&si,0,sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;

DWORD dwExitCode =0 ;

CString strCommandLine = strAppName + " " +strCMDLine;
PROCESS_INFORMATION pi;
if ( CreateProcess( strAppName.GetBuffer(0),
strCommandLine.GetBuffer(0),
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi) )
{
if ( WAIT_OBJECT_0 == WaitForSingleObject(pi.hProcess,dwMillSeconds) )
{
if( GetExitCodeProcess(pi.hProcess,&dwExitCode) )
{
return (dwExitCode == (DWORD) 0)?TRUE:FALSE;
}
else
return FALSE;
}
else
{
return FALSE;
}
}
else// creatprocess failed
{
return FALSE;
}
}