Tuesday, August 19, 2008

How to set command line argument in dialog based program?

Use GetCommandLine function as following:

BOOL CColdBootApp::InitInstance()
{
// 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.
LPTSTR lCmd = ::GetCommandLine();

// MessageBox(NULL, lCmd, L"", MB_OK);

if(_tcscmp(lCmd, L"/nobar")==0)
{
//cold boot
// MessageBox(NULL, L"Cold boot now!", L"", MB_OK);
ColdReboot();
return FALSE;
}

CColdBootDlg dlg;
m_pMainWnd = &dlg;

int nResponse = dlg.DoModal();
...


}

(
CString aa = ::GetCommandLine();
aa.MakeLower();
BOOL bShowBar = FALSE;
BOOL bDoAsk = FALSE;
if(aa.Find(L"/nobar")==-1)
bShowBar = TRUE;
else
bShowBar = FALSE;

if(aa.Find(L"/noask")==-1)
bDoAsk = TRUE;
else
bDoAsk = FALSE;

if(bDoAsk)
{
if(MessageBox(NULL, L"Are you sure?", L"Cold Boot", MB_YESNOCANCEL|MB_TOPMOST|MB_ICONEXCLAMATION )!=IDYES)
return FALSE;
else
{
if(!bShowBar)
{
ColdReboot();
return FALSE;
}
}
}else
{
if(!bShowBar)
{
ColdReboot();
return FALSE;
}
}
)
//*************How to use command line exe file**********************//
/*
C# 5.0:
// Run the CAB
Process CabRunner = Process.Start( "wceload" , "/noui " + CABFile );
CabRunner.WaitForExit();

C# 4.0:
// Used to create a new process.
[DllImport("Coredll", EntryPoint="CreateProcess", SetLastError=true)]
private static extern Int32 CreateProcess(
string sImageName,
string sCmdLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
Int32 boolInheritHandles,
Int32 dwCreationFlag,
IntPtr lpEnvironment,
IntPtr lpCurrentDir,
byte[] si,
ProcessInfo pi);
public IntPtr OpenExternalProgram(string sExeName, string sCmdLine)
{
byte[] b = new byte[128];
ProcessInfo pi = new ProcessInfo();

try
{
// Create a new process that opens the external (exe) program.
CreateProcess(sExeName, sCmdLine, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, b, pi);
return pi.hProcess;
}
catch
{
return IntPtr.Zero;
}
}
C++:
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));

STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);

if(::CreateProcess(_T("\\ColdBoot.exe"), _T(""), NULL, NULL, NULL, FALSE, 0, NULL, &si, &pi) != 0)
{
::WaitForSingleObject(pi.hProcess, 300L); // wait to its finish
::CloseHandle(pi.hProcess);
}else
MessageBox(NULL,L"WarmReboot failed!",L"",MB_OK);

*/

No comments: