I want to fix a bug in a program written to mimick the function of [login to view URL] in Windows. Basically, the program takes the RAS entry name supplied as parameter, and connects to it using RAS api. The source code is about 120 lines long (see detailed section for it).
The bug manifests only under 64 bit Windows (confirmed in Windows Server 2003 R2 x64), where the "use default gateway on remote conenction" setting in the RAS entry is not being honored, and always being treated as if it were checked, even though the RAS entry being dialed has the checkmark turned off.
The actual Visual Studio 2005 solution will be provided to the winner, to work with.
## Deliverables
// [login to view URL] : Defines the entry point for the application.
//
?
#include "stdafx.h"
#include "rasdialx.h"
?
#include "rasnames.h"
?
#include <Ras.h> // RAS Api
#include <shellapi.h> // CommandLineToArgvW
#include <raserror.h>
?
#define MAX_LOADSTRING 100
?
BOOL DialConnection(LPWSTR name);
?
int APIENTRY _tWinMain(HINSTANCE hInstance,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HINSTANCE hPrevInstance,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LPTSTR? ? ? lpCmdLine,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int? ? ? ? ? ? nCmdShow)
{
? ? ? ? ? UNREFERENCED_PARAMETER(hPrevInstance);
? ? ? ? ? UNREFERENCED_PARAMETER(hInstance);
? ? ? ? ? UNREFERENCED_PARAMETER(nCmdShow);? ?
? ? ? ? ? UNREFERENCED_PARAMETER(lpCmdLine);?
?
? ? ? ? ? // Get command line arguments: 1: part of connection name
?
? ? ? ? ? LPWSTR *szArglist;
? ? ? ? ? int nArgs;
? ? ? ? ?
? ? ? ? ? szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
? ? ? ? ? if( NULL == szArglist )
? ? ? {
? ? ? ? ? MessageBoxEx(NULL, TEXT("Cannot retrieve command line arguments"), TEXT("rasdialx"), MB_ICONERROR, NULL);
? ? ? ? ? return 1;
? ? ? }
?
? ? ? ? ? if(nArgs != 2)
? ? ? ? ? {
? ? ? ? ? MessageBoxEx(NULL, TEXT("Incorrect number of arguments"), TEXT("rasdialx"), MB_ICONERROR, NULL);
? ? ? ? ? ? LocalFree(szArglist);
? ? ? ? ? return 1;
? ? ? ? ? }
?
? ? ? ? ? LPWSTR name = szArglist[1];? ?
?
? ? ? ? ? if (name[0] == '\0') {
? ? ? ? ? ? ? ? ? ? ? MessageBox(NULL, TEXT("Empty argument"), TEXT("rasdialx"), MB_OK);
? ? ? ? ? ? ? ? ? ? ? return 1;
? ? ? ? ? }
?
? ? ? ? ? // Dial a connection
? ? ? ? ? BOOL result = DialConnection(name);
? ? ? ? ?
? ? ? ? ? // Free memory allocated for CommandLineToArgvW arguments.
? ? ? ? ? LocalFree(szArglist);
?
? ? ? ? ? if (result) {
? ? ? ? ? ? ? return 0;
? ? ? ? ? } else {
? ? ? ? ? ? ? return 1;
? ? ? ? ? }
}
?
/*
? * BOOL DialConnection(LPWSTR name)
? *
? * Deletes first RAS connection entry which name contains the specified string.
? *
? * Parameters: name - substring to search for.
? *
? * Search is case-sensitive
? *
? * Return value: TRUE if the operation was fully successfull, FALSE otherwise
? *
? */
BOOL DialConnection(LPWSTR name){
? ? ? ? ? RASEntries rasentries; // create and acquire RAS entries, see rasnames project? ? ? ? ?
?
? ? ? ? ? if ([login to view URL]()) {
? ? ? ? ? ? ? ? ? ? ? bool found = false;
? ? ? ? ? ? ? ? ? ? ? for (DWORD i = 0; i < [login to view URL]; ++i)
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (_tcsstr([login to view URL][i].szEntryName, name))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RASDIALPARAMS dialparams;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HRASCONN rasconn = NULL;
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // determine dialing parameters
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [login to view URL] = sizeof(RASDIALPARAMS);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? _tcscpy_s([login to view URL], RAS_MaxEntryName + 1, [login to view URL][i].szEntryName);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? BOOL haspass;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? DWORD nRet = RasGetEntryDialParams(NULL, &dialparams, &haspass);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (nRet != 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox(NULL, TEXT("Failed to get dialing parameters"), TEXT("rasdialx"), MB_OK);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return FALSE;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // try to connect
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? nRet = RasDial(NULL, NULL, &dialparams, NULL, NULL, &rasconn);
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (nRet != 0) { // connection was not successful
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // form error message
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TCHAR lpszErrorString[ 512 ];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RasGetErrorString(nRet, lpszErrorString, 512);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TCHAR* format = TEXT("RAS error %d: %s\nFor more help on this error:\nType 'hh [login to view URL]'\nIn help, click Troubleshooting, then Error Messages, then %d");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TCHAR buffer[700];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? _sntprintf_s(buffer, 700, _TRUNCATE, format, nRet, lpszErrorString, nRet);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox(NULL, buffer, TEXT("rasdialx"), MB_OK);
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // hang up connection
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RasHangUp(rasconn);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // wait while it actually disconnects, or it will be left in undetermined state
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RASCONNSTATUS rcs;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [login to view URL] = sizeof(RASCONNSTATUS);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? while (RasGetConnectStatus(rasconn, &rcs)!=ERROR_INVALID_HANDLE) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Sleep(0);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return FALSE;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? found = true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; // dial only first connection
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? if (!found) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox(NULL, TEXT("No such connection"), TEXT("rasdialx"), MB_OK);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return FALSE;
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? MessageBox(NULL, TEXT("Failed to get list of connections"), TEXT("rasdialx"), MB_OK);
? ? ? ? ? ? ? ? ? ? ? return FALSE;
? ? ? ? ? }
? ? ? ? ? return TRUE;
}