/*
Module : StartX.cpp
Purpose: Implementation of a simple CreateProcess wrapper app
Created: PJN / 29-01-2003
History: PJN / 20-08-2004 1. Now returns the error code back to the OS as returned from any failures when calling
the Win32 SDK calls which StartX uses.
2. Now includes a /NOUI command line parameter, which suppresses any messages boxes which
can appear when an error occurs. Thanks to Trevor Hart for these 2 suggestions.
Copyright (c) 2003 - 2004 by PJ Naughter. (Web: www.naughter.com, Email: [email protected])
All rights reserved.
Copyright / Usage Details:
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
when your product is released in binary form. You are allowed to modify the source code in any way you want
except you cannot modify the copyright details at the top of each module. If you want to distribute source
code with your application, then you are only allowed to distribute versions released by the author. This is
to maintain a single distribution point for the source code.
*/
////////////////////////// Includes ////////////////////////////////////////////////
#include "stdafx.h"
#include "StartX.h"
#include "ParametersPromptDlg.h"
////////////////////////// Macros //////////////////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#ifndef BELOW_NORMAL_PRIORITY_CLASS
#define BELOW_NORMAL_PRIORITY_CLASS 0x00004000
#endif
#ifndef ABOVE_NORMAL_PRIORITY_CLASS
#define ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
#endif
typedef BOOL (WINAPI CREATEPROCESSWITHLOGONW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPCWSTR, LPWSTR, DWORD, LPVOID, LPWSTR, LPSTARTUPINFOW, LPPROCESS_INFORMATION);
typedef CREATEPROCESSWITHLOGONW* LPCREATEPROCESSWITHLOGONW;
typedef DWORD (WINAPI CREDUIPROMPTFORCREDENTIALS)(PCREDUI_INFO, PCTSTR, PCtxtHandle, DWORD, PCTSTR, ULONG, PCTSTR, ULONG, PBOOL, DWORD);
typedef CREDUIPROMPTFORCREDENTIALS* LPCREDUIPROMPTFORCREDENTIALS;
typedef HRESULT (WINAPI SH_GET_FOLDERPATH)(HWND, int, HANDLE, DWORD, LPTSTR);
typedef SH_GET_FOLDERPATH* LPSH_GET_FOLDERPATH;
#ifndef LOGIN_WITH_PROFILE
#define LOGON_WITH_PROFILE 0x00000001
#endif
#ifndef LOGIN_NETCREDENTIALS_ONLY
#define LOGON_NETCREDENTIALS_ONLY 0x00000002
#endif
#ifndef SHGFP_TYPE_CURRENT
#define SHGFP_TYPE_DEFAULT 1
#endif
#ifndef SHGFP_TYPE_DEFAULT
#define SHGFP_TYPE_DEFAULT 1
#endif
////////////////////////// Implementation //////////////////////////////////////////
class CMyCommandLineInfo : public CCommandLineInfo
{
public:
//Constructors / Destructors
CMyCommandLineInfo();
//Methods
void FixStrings(CString& sValue);
void ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast);
CString MapFromGenericPath(const CString& sPath);
CString GetIdlPath(int nIdl);
//Data
CString sUser; //The user to run the request as
CString sDomain; //The domain of the user to run the request as
BOOL bPromptPassword; //Should we prompt for the users password
CString sPassword; //The password for "sUser" to login with
CString sCommandLine; //The command line to actually run
DWORD dwCreationFlags; //The creation flags to pass to "CreateProcess.."
CString sDesktop; //The "lpDesktop" attribute in the STARTUPINFO structure
BOOL bDesktop; //TRUE if the "lpDesktop" attribute should be used
CString sConsoleTitle; //The "lpTitle" attribute in the STARTUPINFO structure
CPoint WindowPoint; //The x and y values of the "dwX" and "dwY" attributes in the STARTUPINFO structure
CSize WindowSize; //The width and height values of the "dwXSize" and "dwYSize" attributes in the STARTUPINFO structure
CSize ConsoleBufferSize; //The width and height values of the "dwXCountChars" and "dwYCountChars" attributes in the STARTUPINFO structure
DWORD dwFillAttribute; //The "dwFillAttribute" attribute in the STARTUPINFO structure
DWORD dwFlags; //The "dwFlags" attribute in the STARTUPINFO structure
WORD wShowWindow; //The "wShowWindow" attribute in the STARTUPINFO structure
CString sCurrentDirectory; //The "lpCurrentDirectory" attribute to pass to "CreateProcess.."
DWORD dwLogonFlags; //The "dwLogonFlags" attribute to pass to "CreateProcessWithLogonW"
BOOL bWait; //Should we wait for the process to terminate before this app exits
CString sDrive; //The value which is replaced in all occurrences of "@EXEDRV@"
CString sFolder; //The value which is replaced in all occurrences of "@EXEDIR@"
BOOL bNoUI; //Supress any GUI
};
CMyCommandLineInfo::CMyCommandLineInfo()
{
bPromptPassword = FALSE;
dwCreationFlags = 0;
bDesktop = bDesktop;
WindowPoint = CPoint(0, 0);
WindowSize = CSize(0, 0);
ConsoleBufferSize = CSize(0, 0);
dwFillAttribute = 0;
dwFlags = 0;
wShowWindow = SW_SHOW;
dwLogonFlags = 0;
bWait = FALSE;
bNoUI = FALSE;
}
void CMyCommandLineInfo::FixStrings(CString& sValue)
{
//Replace any variables with their actual values as determined above
sValue.Replace(_T("@EXEDRV@"), sDrive);
sValue.Replace(_T("@EXEDIR@"), sFolder);
sValue.Replace(_T("@QUOTE@"), _T("\""));
sValue = MapFromGenericPath(sValue);
}
CString CMyCommandLineInfo::MapFromGenericPath(const CString& sPath)
{
CString sExpanded(sPath);
CString sIdl;
int nQuote1 = sPath.Find(_T('@'));
if (nQuote1 != -1)
{
CString sGeneric(sPath.Right(sPath.GetLength() - nQuote1 - 1));
int nQuote2 = sGeneric.Find(_T('@'));
if (nQuote2 > 0)
{
sIdl = sGeneric.Left(nQuote2);
int nIdl = _ttoi(sIdl);
sExpanded = sPath.Left(nQuote1) + GetIdlPath(nIdl) + sPath.Right(sPath.GetLength() - nQuote2 - nQuote1 - 2);
}
}
return sExpanded;
}
CString CMyCommandLineInfo::GetIdlPath(int nIdl)
{
//What will be the return value
CString sIdl;
//Use the SHGetFolderPath API
HINSTANCE hshfolder = LoadLibrary(_T("SHFOLDER.DLL"));
if (hshfolder)
{
#ifdef _UNICODE
LPSH_GET_FOLDERPATH lpfnShGetFolderPath = (LPSH_GET_FOLDERPATH) GetProcAddress(hshfolder, "SHGetFolderPathW");
#else
LPSH_GET_FOLDERPATH lpfnShGetFolderPath = (LPSH_GET_FOLDERPATH) GetProcAddress(hshfolder, "SHGetFolderPathA");
#endif
if (lpfnShGetFolderPath)
{
TCHAR pszPath[_MAX_PATH];
if (SUCCEEDED(lpfnShGetFolderPath(NULL, nIdl, NULL, SHGFP_TYPE_DEFAULT, pszPath)))
sIdl = pszPath;
}
FreeLibrary(hshfolder);
}
return sIdl;
}
void CMyCommandLineInfo::ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast)
{
CString sUpCaseParam(lpszParam);
sUpCaseParam.MakeUpper();
int nLength = sUpCaseParam.GetLength();
if (!bFlag)
{
if (!bLast)
{
sConsoleTitle = lpszParam;
FixStrings(sConsoleTitle);
}
else
{
sCommandLine = lpszParam;
FixStrings(sCommandLine);
}
}
else
{
if (nLength && sUpCaseParam.GetAt(0) == _T('D'))
{
sCurrentDirectory = &lpszParam[1];
FixStrings(sCurrentDirectory);
}
else if (sUpCaseParam == _T("B"))
{
dwCreationFlags |= CREATE_NO_WINDOW;
}
else if (sUpCaseParam == _T("NOUI"))
{
bNoUI = TRUE;
}
else if (sUpCaseParam == _T("PP"))
{
bPromptPassword = TRUE;
}
else if (sUpCaseParam == _T("MIN"))
{
dwFlags |= STARTF_USESHOWWINDOW;
wShowWindow = SW_SHOWMINIMIZED;
}
else if (sUpCaseParam == _T("MAX"))
{
dwFlags |= STARTF_USESHOWWINDOW;
wShowWindow = SW_SHOWMAXIMIZED;
}
else if (sUpCaseParam == _T("SEPAR
DLU.rar_Windows编程
版权申诉
106 浏览量
2022-09-21
22:23:00
上传
评论
收藏 82KB RAR 举报
JaniceLu
- 粉丝: 79
- 资源: 1万+