I have a program that closely mimicks the "rasdial" command in Windows, which basically takes a parameter that is the name of a RAS entry, and causes that entry to get dialed.
This program works fine under 32 bit Windows, but under 64 bit windows (confirmed problem in Windows Server 2003 R2 x64), exhibits a strange behavior, in which 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 entry does not? have this option checked.
There must be some assumption being made about default behavior which changes with the 64 bit windows, in the attached code, that is erroneous. I want to identify the nature of the cause of this problem, and fix it (I am pretty sure the problem will be fixed by adding one or two lines of code, which makes some implied assumption explicit, and tells Windows to respect this setting).
The source code for the program in question is about 120 lines long. I am pasting this in the detailed description section. The actual Visual Studio 2005 solution will be provided to the winner, to work with.
## Deliverables
// Below is the source code for the program that // needs to be debugged. // The solution file will be supplied to the winner // which is native to Visual Studio 2005. // [login to view URL] : Defines the entry point for the application. // #include "stdafx.h" #include "rasdialx.h" #include "rasnames.h" #include // RAS Api #include // CommandLineToArgvW #include #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; }