SHSTDAPI_(LPWSTR *) CommandLineToArgvW(_In_ LPCWSTR lpCmdLine, _Out_ int* pNumArgs);
例:
PTSTR strCmd = GetCommandLine();
int nCount = 0;
LPWSTR* pData = CommandLineToArgvW(strCmd, &nCount);
for (int i = 0; i < nCount; ++i)
{
OutputDebugString(pData[i]);
OutputDebugString(_T("\n"));
}
//取得指定環境變量
void PrintEnvironmentVariable(PCTSTR pszVariableName)
{
PTSTR pszValue = NULL;
// Get the size of the buffer that is required to store the value
DWORD dwResult = GetEnvironmentVariable(pszVariableName, pszValue, 0);
if (dwResult != 0)
{
// Allocate the buffer to store the environment variable value
DWORD size = dwResult * sizeof(TCHAR);
pszValue = (PTSTR)malloc(size);
GetEnvironmentVariable(pszVariableName, pszValue, size);
_tprintf(TEXT("%s=%s\n"), pszVariableName, pszValue);
free(pszValue);
}
else
{
_tprintf(TEXT("'%s'=<unknown value>\n"), pszVariableName);
}
}
//取得所有環境變量
void DumpEnvStrings()
{
PTSTR pEnvBlock = GetEnvironmentStrings();
// Parse the block with the following format:
// =::=::\
// =...
// var=value\0
// ...
// var=value\0\0
// Note that some other strings might begin with '='.
// Here is an example when the application is started from a network share.
// [0] =::=::\
// [1] =C:=C:\Windows\System32
// [2] =ExitCode=00000000
//
TCHAR szName[MAX_PATH];
TCHAR szValue[MAX_PATH];
PTSTR pszCurrent = pEnvBlock;
HRESULT hr = S_OK;
PCTSTR pszPos = NULL;
int current = 0;
while (pszCurrent != NULL)
{
// Skip the meaningless strings like:
// "=::=::\"
if (*pszCurrent != TEXT('='))
{
// Look for '=' separator.
pszPos = _tcschr(pszCurrent, TEXT('='));
// Point now to the first character of the value.
pszPos++;
// Copy the variable name.
size_t cbNameLength = // Without the' ='
(size_t)pszPos - (size_t)pszCurrent - sizeof(TCHAR);
hr = StringCbCopyN(szName, MAX_PATH, pszCurrent, cbNameLength);
if (FAILED(hr))
{
break;
}
// Copy the variable value with the last NULL character
// and allow truncation because this is for UI only.
hr = StringCchCopyN(szValue, MAX_PATH, pszPos, _tcslen(pszPos) + 1);
if (SUCCEEDED(hr))
{
_tprintf(TEXT("[%u] %s=%s\r\n"), current, szName, szValue);
}
else // something wrong happened; check for truncation.
if (hr == STRSAFE_E_INSUFFICIENT_BUFFER)
{
_tprintf(TEXT("[%u] %s=%s...\r\n"), current, szName, szValue);
}
else
{ // This should never occur.
_tprintf(
TEXT("[%u] %s=???\r\n"), current, szName
);
break;
}
}
else
{
_tprintf(TEXT("[%u] %s\r\n"), current, pszCurrent);
}
// Next variable please.
current++;
// Move to the end of the string.
while (*pszCurrent != TEXT('\0'))
pszCurrent++;
pszCurrent++;
// Check if it was not the last string.
if (*pszCurrent == TEXT('\0'))
break;
};
// Don't forget to free the memory.
FreeEnvironmentStrings(pEnvBlock);
}
//將由百分號封閉起來的環境變量名轉換成那個變量的內容。比如,“%path%”會擴充成完整路徑
void TestExpandEnvironmentString()
{
DWORD chValue =ExpandEnvironmentStrings(TEXT("PATH='%PATH%'"), NULL, 0);
PTSTR pszBuffer = new TCHAR[chValue];
chValue = ExpandEnvironmentStrings(TEXT("PATH='%PATH%'"), pszBuffer, chValue);
_tprintf(TEXT("%s\r\n"), pszBuffer);
delete[] pszBuffer;
}
//使用SetEnvironmentVariable函數添加一個變量,刪除一個變量,或者修改一個變量的值:
BOOL SetEnvironmentVariable(PCTSTR pszName,PCTSTR pszValue);
此函數將pszName所標識的一個變量設爲pszValue參數所標識的值。如果已經存在具有指定名
稱的一個變量,SetEnvironmentVariable函數會修改它的值。如果指定的變量不存在,就添加
這個變量。如果pszValue爲NULL,變量會從環境塊中刪除