2007-11-08 12:47:25 +00:00
|
|
|
|
|
|
|
#include "todo.h"
|
2007-11-06 13:53:34 +00:00
|
|
|
|
|
|
|
#define NUM_SETTINGS 6
|
|
|
|
LPWSTR lpSettings[NUM_SETTINGS] =
|
|
|
|
{
|
|
|
|
L"screen mode id",
|
|
|
|
L"desktopwidth",
|
|
|
|
L"desktopheight",
|
|
|
|
L"session bpp",
|
|
|
|
L"full address",
|
|
|
|
L"compression",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
|
2007-11-07 13:49:31 +00:00
|
|
|
BOOL
|
|
|
|
SetIntegerToSettings(PRDPSETTINGS pRdpSettings,
|
|
|
|
LPWSTR lpKey,
|
|
|
|
INT Value)
|
|
|
|
{
|
|
|
|
BOOL bRet = FALSE;
|
|
|
|
|
|
|
|
if (pRdpSettings)
|
|
|
|
{
|
|
|
|
INT i;
|
|
|
|
|
|
|
|
for (i = 0; i < pRdpSettings->NumSettings; i++)
|
|
|
|
{
|
|
|
|
if (wcscmp(pRdpSettings->pSettings[i].Key, lpKey) == 0)
|
|
|
|
{
|
|
|
|
if (pRdpSettings->pSettings[i].Type == L'i')
|
|
|
|
{
|
|
|
|
pRdpSettings->pSettings[i].Value.i = Value;
|
|
|
|
bRet = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
SetStringToSettings(PRDPSETTINGS pRdpSettings,
|
|
|
|
LPWSTR lpKey,
|
|
|
|
LPWSTR lpValue)
|
|
|
|
{
|
|
|
|
BOOL bRet = FALSE;
|
|
|
|
|
|
|
|
if (pRdpSettings)
|
|
|
|
{
|
|
|
|
INT i;
|
|
|
|
|
|
|
|
for (i = 0; i < pRdpSettings->NumSettings; i++)
|
|
|
|
{
|
|
|
|
if (wcscmp(pRdpSettings->pSettings[i].Key, lpKey) == 0)
|
|
|
|
{
|
|
|
|
if (pRdpSettings->pSettings[i].Type == L's')
|
|
|
|
{
|
2007-11-08 00:23:55 +00:00
|
|
|
wcscpy(pRdpSettings->pSettings[i].Value.s, lpValue);
|
2007-11-07 13:49:31 +00:00
|
|
|
bRet = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
INT
|
|
|
|
GetIntegerFromSettings(PRDPSETTINGS pRdpSettings,
|
|
|
|
LPWSTR lpKey)
|
|
|
|
{
|
|
|
|
INT Value = -1;
|
|
|
|
|
|
|
|
if (pRdpSettings)
|
|
|
|
{
|
|
|
|
INT i;
|
|
|
|
|
|
|
|
for (i = 0; i < pRdpSettings->NumSettings; i++)
|
|
|
|
{
|
|
|
|
if (wcscmp(pRdpSettings->pSettings[i].Key, lpKey) == 0)
|
|
|
|
{
|
|
|
|
if (pRdpSettings->pSettings[i].Type == L'i')
|
|
|
|
{
|
|
|
|
Value = pRdpSettings->pSettings[i].Value.i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LPWSTR
|
|
|
|
GetStringFromSettings(PRDPSETTINGS pRdpSettings,
|
|
|
|
LPWSTR lpKey)
|
|
|
|
{
|
|
|
|
LPWSTR lpValue = NULL;
|
|
|
|
|
|
|
|
if (pRdpSettings)
|
|
|
|
{
|
|
|
|
INT i;
|
|
|
|
|
|
|
|
for (i = 0; i < pRdpSettings->NumSettings; i++)
|
|
|
|
{
|
|
|
|
if (wcscmp(pRdpSettings->pSettings[i].Key, lpKey) == 0)
|
|
|
|
{
|
|
|
|
if (pRdpSettings->pSettings[i].Type == L's')
|
|
|
|
{
|
|
|
|
lpValue = pRdpSettings->pSettings[i].Value.s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lpValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-06 13:53:34 +00:00
|
|
|
static BOOL
|
|
|
|
WriteRdpFile(HANDLE hFile,
|
2007-11-06 23:23:25 +00:00
|
|
|
PRDPSETTINGS pRdpSettings)
|
2007-11-06 13:53:34 +00:00
|
|
|
{
|
2007-11-06 23:23:25 +00:00
|
|
|
WCHAR line[MAXKEY + MAXVALUE + 4];
|
|
|
|
DWORD BytesToWrite, BytesWritten;
|
|
|
|
BOOL bRet;
|
2007-11-07 00:10:24 +00:00
|
|
|
INT i, k;
|
2007-11-06 13:53:34 +00:00
|
|
|
|
2007-11-06 23:23:25 +00:00
|
|
|
for (i = 0; i < pRdpSettings->NumSettings; i++)
|
|
|
|
{
|
2007-11-07 00:10:24 +00:00
|
|
|
/* only write out values in the lpSettings struct */
|
|
|
|
for (k = 0; k < NUM_SETTINGS; k++)
|
2007-11-06 23:23:25 +00:00
|
|
|
{
|
2007-11-07 00:10:24 +00:00
|
|
|
if (wcscmp(lpSettings[k], pRdpSettings->pSettings[i].Key) == 0)
|
|
|
|
{
|
|
|
|
if (pRdpSettings->pSettings[i].Type == L'i')
|
|
|
|
{
|
|
|
|
_snwprintf(line, MAXKEY + MAXVALUE + 4, L"%s:i:%d\r\n",
|
|
|
|
pRdpSettings->pSettings[i].Key,
|
|
|
|
pRdpSettings->pSettings[i].Value.i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_snwprintf(line, MAXKEY + MAXVALUE + 4, L"%s:s:%s\r\n",
|
|
|
|
pRdpSettings->pSettings[i].Key,
|
|
|
|
pRdpSettings->pSettings[i].Value.s);
|
|
|
|
}
|
2007-11-06 23:23:25 +00:00
|
|
|
|
2007-11-07 00:10:24 +00:00
|
|
|
BytesToWrite = wcslen(line) * sizeof(WCHAR);
|
2007-11-06 23:23:25 +00:00
|
|
|
|
2007-11-07 00:10:24 +00:00
|
|
|
bRet = WriteFile(hFile,
|
|
|
|
line,
|
|
|
|
BytesToWrite,
|
|
|
|
&BytesWritten,
|
|
|
|
NULL);
|
|
|
|
if (!bRet || BytesWritten == 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2007-11-06 23:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2007-11-06 13:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
static VOID
|
|
|
|
ParseSettings(PRDPSETTINGS pRdpSettings,
|
|
|
|
LPWSTR lpBuffer)
|
2007-11-06 13:53:34 +00:00
|
|
|
{
|
|
|
|
LPWSTR lpStr = lpBuffer;
|
|
|
|
WCHAR lpKey[MAXKEY];
|
|
|
|
WCHAR lpValue[MAXVALUE];
|
|
|
|
INT NumSettings = 0;
|
2007-11-08 00:23:55 +00:00
|
|
|
INT s;
|
2007-11-06 13:53:34 +00:00
|
|
|
|
2007-11-06 15:48:10 +00:00
|
|
|
if (lpStr)
|
2007-11-06 13:53:34 +00:00
|
|
|
{
|
|
|
|
/* get number of settings */
|
|
|
|
while (*lpStr)
|
|
|
|
{
|
|
|
|
if (*lpStr == L'\n')
|
|
|
|
NumSettings++;
|
|
|
|
lpStr++;
|
|
|
|
}
|
|
|
|
lpStr = lpBuffer;
|
|
|
|
|
2007-11-06 15:48:10 +00:00
|
|
|
if (NumSettings == 0)
|
2007-11-06 20:36:31 +00:00
|
|
|
return;
|
2007-11-06 13:53:34 +00:00
|
|
|
|
2007-11-06 15:48:10 +00:00
|
|
|
/* move past unicode byte order */
|
|
|
|
if (lpStr[0] == 0xFEFF || lpStr[0] == 0xFFFE)
|
|
|
|
lpStr += 1;
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
pRdpSettings->pSettings = HeapAlloc(GetProcessHeap(),
|
|
|
|
0,
|
|
|
|
sizeof(SETTINGS) * NumSettings);
|
|
|
|
if (pRdpSettings->pSettings)
|
2007-11-06 13:53:34 +00:00
|
|
|
{
|
2007-11-06 20:36:31 +00:00
|
|
|
pRdpSettings->NumSettings = NumSettings;
|
|
|
|
|
2007-11-06 13:53:34 +00:00
|
|
|
for (s = 0; s < NumSettings; s++)
|
|
|
|
{
|
2007-11-08 00:23:55 +00:00
|
|
|
INT i = 0, k;
|
2007-11-06 13:53:34 +00:00
|
|
|
|
|
|
|
/* get a key */
|
|
|
|
while (*lpStr != L':')
|
|
|
|
{
|
|
|
|
lpKey[i++] = *lpStr++;
|
|
|
|
}
|
|
|
|
lpKey[i] = 0;
|
|
|
|
|
|
|
|
for (k = 0; k < NUM_SETTINGS; k++)
|
|
|
|
{
|
|
|
|
if (wcscmp(lpSettings[k], lpKey) == 0)
|
|
|
|
{
|
2007-11-06 20:36:31 +00:00
|
|
|
wcscpy(pRdpSettings->pSettings[s].Key, lpKey);
|
2007-11-06 13:53:34 +00:00
|
|
|
|
|
|
|
/* get the type */
|
|
|
|
lpStr++;
|
|
|
|
if (*lpStr == L'i' || *lpStr == L's')
|
2007-11-06 20:36:31 +00:00
|
|
|
pRdpSettings->pSettings[s].Type = *lpStr;
|
2007-11-06 13:53:34 +00:00
|
|
|
|
|
|
|
lpStr += 2;
|
|
|
|
|
|
|
|
/* get a value */
|
|
|
|
i = 0;
|
|
|
|
while (*lpStr != L'\r')
|
|
|
|
{
|
|
|
|
lpValue[i++] = *lpStr++;
|
|
|
|
}
|
|
|
|
lpValue[i] = 0;
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
if (pRdpSettings->pSettings[s].Type == L'i')
|
2007-11-06 13:53:34 +00:00
|
|
|
{
|
2007-11-06 20:36:31 +00:00
|
|
|
pRdpSettings->pSettings[s].Value.i = _wtoi(lpValue);
|
2007-11-06 13:53:34 +00:00
|
|
|
}
|
2007-11-06 20:36:31 +00:00
|
|
|
else if (pRdpSettings->pSettings[s].Type == L's')
|
2007-11-06 13:53:34 +00:00
|
|
|
{
|
2007-11-06 20:36:31 +00:00
|
|
|
wcscpy(pRdpSettings->pSettings[s].Value.s, lpValue);
|
2007-11-06 13:53:34 +00:00
|
|
|
}
|
|
|
|
else
|
2007-11-06 20:36:31 +00:00
|
|
|
pRdpSettings->pSettings[s].Type = 0;
|
2007-11-06 13:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-06 15:48:10 +00:00
|
|
|
/* move onto next setting */
|
2007-11-06 13:53:34 +00:00
|
|
|
while (*lpStr != L'\n')
|
|
|
|
{
|
|
|
|
lpStr++;
|
|
|
|
}
|
|
|
|
lpStr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
|
2007-11-06 13:53:34 +00:00
|
|
|
static LPWSTR
|
|
|
|
ReadRdpFile(HANDLE hFile)
|
|
|
|
{
|
2007-11-08 00:23:55 +00:00
|
|
|
LPWSTR lpBuffer = NULL;
|
2007-11-06 13:53:34 +00:00
|
|
|
DWORD BytesToRead, BytesRead;
|
|
|
|
BOOL bRes;
|
|
|
|
|
|
|
|
if (hFile)
|
|
|
|
{
|
|
|
|
BytesToRead = GetFileSize(hFile, NULL);
|
|
|
|
if (BytesToRead)
|
|
|
|
{
|
|
|
|
lpBuffer = HeapAlloc(GetProcessHeap(),
|
|
|
|
0,
|
2007-11-06 15:48:10 +00:00
|
|
|
BytesToRead + 2);
|
2007-11-06 13:53:34 +00:00
|
|
|
if (lpBuffer)
|
|
|
|
{
|
|
|
|
bRes = ReadFile(hFile,
|
|
|
|
lpBuffer,
|
|
|
|
BytesToRead,
|
|
|
|
&BytesRead,
|
|
|
|
NULL);
|
|
|
|
if (bRes)
|
|
|
|
{
|
|
|
|
lpBuffer[BytesRead / 2] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(),
|
|
|
|
0,
|
|
|
|
lpBuffer);
|
|
|
|
|
|
|
|
lpBuffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lpBuffer;
|
|
|
|
}
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
|
2007-11-06 13:53:34 +00:00
|
|
|
static HANDLE
|
2007-11-06 15:48:10 +00:00
|
|
|
OpenRdpFile(LPWSTR path, BOOL bWrite)
|
2007-11-06 13:53:34 +00:00
|
|
|
{
|
2007-11-08 00:23:55 +00:00
|
|
|
HANDLE hFile = NULL;
|
2007-11-06 13:53:34 +00:00
|
|
|
|
|
|
|
if (path)
|
|
|
|
{
|
2007-11-06 15:48:10 +00:00
|
|
|
hFile = CreateFileW(path,
|
|
|
|
bWrite ? GENERIC_WRITE : GENERIC_READ,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
bWrite ? CREATE_ALWAYS : OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
2007-11-06 13:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VOID
|
|
|
|
CloseRdpFile(HANDLE hFile)
|
|
|
|
{
|
|
|
|
if (hFile)
|
|
|
|
CloseHandle(hFile);
|
|
|
|
}
|
2007-11-06 14:06:18 +00:00
|
|
|
|
2007-11-06 15:48:10 +00:00
|
|
|
|
2007-11-06 23:23:25 +00:00
|
|
|
BOOL
|
|
|
|
SaveRdpSettingsToFile(LPWSTR lpFile,
|
|
|
|
PRDPSETTINGS pRdpSettings)
|
|
|
|
{
|
|
|
|
WCHAR pszPath[MAX_PATH];
|
|
|
|
HANDLE hFile;
|
|
|
|
BOOL bRet = FALSE;
|
|
|
|
|
|
|
|
/* use default file */
|
|
|
|
if (lpFile == NULL)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
LPITEMIDLIST lpidl= NULL;
|
|
|
|
|
|
|
|
hr = SHGetFolderLocation(NULL,
|
|
|
|
CSIDL_PERSONAL,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&lpidl);
|
|
|
|
if (hr == S_OK)
|
|
|
|
{
|
|
|
|
if (SHGetPathFromIDListW(lpidl, pszPath))
|
|
|
|
{
|
|
|
|
wcscat(pszPath, L"\\Default.rdp");
|
|
|
|
lpFile = pszPath;
|
|
|
|
CoTaskMemFree(lpidl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpFile)
|
|
|
|
{
|
|
|
|
hFile = OpenRdpFile(lpFile, TRUE);
|
|
|
|
if (hFile)
|
|
|
|
{
|
|
|
|
if (WriteRdpFile(hFile, pRdpSettings))
|
|
|
|
{
|
|
|
|
bRet = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseRdpFile(hFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
PRDPSETTINGS
|
2007-11-06 15:48:10 +00:00
|
|
|
LoadRdpSettingsFromFile(LPWSTR lpFile)
|
|
|
|
{
|
2007-11-08 00:23:55 +00:00
|
|
|
PRDPSETTINGS pRdpSettings = NULL;
|
2007-11-06 15:48:10 +00:00
|
|
|
WCHAR pszPath[MAX_PATH];
|
|
|
|
HANDLE hFile;
|
|
|
|
|
|
|
|
/* use default file */
|
|
|
|
if (lpFile == NULL)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
LPITEMIDLIST lpidl= NULL;
|
|
|
|
|
|
|
|
hr = SHGetFolderLocation(NULL,
|
|
|
|
CSIDL_PERSONAL,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&lpidl);
|
|
|
|
if (hr == S_OK)
|
|
|
|
{
|
|
|
|
if (SHGetPathFromIDListW(lpidl, pszPath))
|
|
|
|
{
|
|
|
|
wcscat(pszPath, L"\\Default.rdp");
|
|
|
|
lpFile = pszPath;
|
2007-11-06 20:36:31 +00:00
|
|
|
CoTaskMemFree(lpidl);
|
2007-11-06 15:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpFile)
|
|
|
|
{
|
|
|
|
LPWSTR lpBuffer = NULL;
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
pRdpSettings = HeapAlloc(GetProcessHeap(),
|
|
|
|
0,
|
|
|
|
sizeof(RDPSETTINGS));
|
|
|
|
if (pRdpSettings)
|
2007-11-06 15:48:10 +00:00
|
|
|
{
|
2007-11-06 20:36:31 +00:00
|
|
|
hFile = OpenRdpFile(lpFile, FALSE);
|
|
|
|
if (hFile)
|
2007-11-06 15:48:10 +00:00
|
|
|
{
|
2007-11-06 20:36:31 +00:00
|
|
|
lpBuffer = ReadRdpFile(hFile);
|
|
|
|
if (lpBuffer)
|
|
|
|
{
|
|
|
|
ParseSettings(pRdpSettings, lpBuffer);
|
2007-11-06 15:48:10 +00:00
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
HeapFree(GetProcessHeap(),
|
|
|
|
0,
|
|
|
|
lpBuffer);
|
|
|
|
}
|
2007-11-06 15:48:10 +00:00
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
CloseRdpFile(hFile);
|
|
|
|
}
|
2007-11-06 15:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-06 20:36:31 +00:00
|
|
|
return pRdpSettings;
|
2007-11-06 15:48:10 +00:00
|
|
|
}
|