이것은 내 Visual C ++ MFC 응용 프로그램 내에서 콘솔 응용 프로그램 을 실행하는 코드입니다 .
bool CGoogleAuthandSync::ExecuteProgram(CString strCommand, DWORD &rExitCode)
{
PROCESS_INFORMATION processInformation = { 0 };
STARTUPINFO startupInfo = { 0 };
int nStrBuffer;
BOOL bProcessResult, bExitCodeProcess;
bool bOK = false;
CWaitCursor wait;
rExitCode = -1;
startupInfo.cb = sizeof(startupInfo);
nStrBuffer = strCommand.GetLength() + 50;
bProcessResult = CreateProcess(NULL, strCommand.GetBuffer(nStrBuffer),
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, NULL, &startupInfo, &processInformation);
strCommand.ReleaseBuffer();
if (!bProcessResult)
{
// CreateProcess() failed
// Get the error from the system
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
// Display the error
CString strError = (LPTSTR)lpMsgBuf;
TRACE(_T("Authenticate failed at CreateProcess()\nCommand=%s\nMessage=%s\n\n"), strCommand, strError);
// Free resources created by the system
LocalFree(lpMsgBuf);
// We failed.
return false;
}
else
{
// Successfully created the process. Wait for it to finish.
//WaitForSingleObject(processInformation.hProcess, INFINITE);
// AJT v14.0.3
DWORD WaitResult;
do
{
WaitResult = MsgWaitForMultipleObjects(1,
// only 1 wait object
&processInformation.hProcess, // worker thread
FALSE, // stop if any
INFINITE, // no timeout
QS_ALLINPUT);
if (WaitResult == WAIT_OBJECT_0 + 1)
{
// Handle windows message
MSG Msg;
while (PeekMessage(&Msg, NULL, 0, (UINT)-1, PM_REMOVE))
{
TRACE3("%d %d %d\n", Msg.message, Msg.wParam, Msg.lParam);
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
} while (WaitResult != WAIT_OBJECT_0);
//} while (WaitResult == WAIT_OBJECT_0 + 1);
ASSERT(WaitResult == WAIT_OBJECT_0);
// Get the exit code.
bExitCodeProcess = GetExitCodeProcess(processInformation.hProcess, &rExitCode);
// Close the handles.
CloseHandle(processInformation.hProcess);
CloseHandle(processInformation.hThread);
if (!bExitCodeProcess)
{
// Could not get exit code.
TRACE(_T("Executed command but couldn't get exit code.\nCommand=%s\n"), strCommand);
return false;
}
// if (rExitCode == 0) // We should have returned a value
// return false;
return true;
}
return false;
}
호출 코드는 다음을 수행합니다.
bool CGoogleAuthandSync::AddEventsToCalendarXML(CString strXML)
{
CString strCommand = BuildCommandLine(_T("addtocalendarxml"), strXML);
DWORD dwExitCode;
if (ExecuteProgram(strCommand, dwExitCode))
{
if (dwExitCode == 3)
return true;
else
{
CString strError;
strError.Format(IDS_TPL_GOOGLE_UPDATE_FAILED, g_strResultDesc[dwExitCode]);
AfxMessageBox(strError, MB_OK | MB_ICONINFORMATION);
return false;
}
}
return false;
}
마지막으로, 외부 유틸리티를 실행하기 전에 일부 데이터를 컴퓨터에 쓰는 저장 메커니즘을 실행하는 기본 대화 상자 (내 애플리케이션의 팝업 창) :
bool bSyncToGoogle = false;
if (CGoogleCalendarSettingsDlg::GetSyncToGooglePrompt())
{
if (AfxMessageBox(IDS_STR_SYNC_GOOGLE_CALENDAR_PROMPT, MB_YESNO | MB_ICONQUESTION) == IDYES)
ExportToGoogleCalendar();
}
else if (CGoogleCalendarSettingsDlg::GetSyncToGoogle())
ExportToGoogleCalendar();
SetModified(false);
그래서 우리는 내 응용 프로그램에 팝업 편집기 (대화 상자)가 있습니다.
자, 나는 이것이 계속되기 전에 유틸리티가 끝날 때까지 메인 윈도우가 기다리는 방식으로 이것을 작성했다고 생각했습니다.
하지만 일부 사용자와 함께 찾은 것은 저장을 클릭 한 다음 편집기의 오른쪽 상단에있는 X를 빠르게 클릭 할 수 있다는 것입니다. 동기화가 완료 되기 전에 이를 수행 할 수 있습니다. 그래서 그것은 여전히 수정되었다고 생각합니다.
오른쪽 상단의 X를 클릭하기 전에 몇 초 정도 기다리면 올바르게 닫히고 문서가 더 이상 수정되지 않은 것으로 간주합니다.
그래서 어떤 종류의 인종 문제가 있다고 생각합니다. 그런데 왜? 내 애플리케이션이 Google과의 동기화가 완료 될 때까지 기다리지 않고 사용자가 편집기 닫기를 고려하도록 허용하기 전에 수정 된 플래그를 false로 설정하는 이유는 무엇입니까?
혼란스러워.
이것은 트릭을 할 것입니다. 프로세스 경로를 매개 변수로 전달하십시오.
BOOL exeWait( char* file ) const
{
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).
file, // 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;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return TRUE;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다