Example #1
0
int rashelper_validate_phonebook_entry(char *entry_name)
{
        DWORD nRet = 0;
                
        // NULL as a phonebook uses default user selected phonebook file, usually:
        // C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk
        nRet = RasValidateEntryName(NULL, entry_name);

        return nRet;
}
//
// CustomEntryDlgProc
//
// This function is the dialog procedure for the custom entry dialog and handles
// the basic events that happen within that dialog
//
INT_PTR CALLBACK CustomEntryDlgProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
)
{  
    HWND hEditBox = NULL;                           // handle to an edit box
    HWND hComboBox = NULL;                          // handle to the combo box
    LPRASENTRYDLG lpInfo = NULL;                    // pointer to the RASENTRYDLG structure
    static LPRASENTRY lpRasEntry = NULL;            // pointer to the RASENTRY structure
    static PSAMPLE_CUSTOM_ENTRY_DATA pData = NULL;  // pointer to the SAMPLE_CUSTOM_ENTRY_DATA structure

    switch (uMsg)
    {
    case WM_INITDIALOG:    
        {
            // set up the dialog and the parameters
            pData = (PSAMPLE_CUSTOM_ENTRY_DATA) lParam;
            if (!pData) 
            {
                return FALSE;
            }

            lpInfo = &(pData->tEntryDlg);
            lpRasEntry = pData->ptEntry;                
          
            hComboBox = GetDlgItem(hwndDlg, IDC_LIST_MODEMS);

            hEditBox = GetDlgItem(hwndDlg, IDC_EDIT_ENTRYNAME);
            if (hEditBox)
            {
                Edit_LimitText(hEditBox, RAS_MaxEntryName); // Count doesn't include NULL
                Edit_SetText(hEditBox, (LPTSTR)pData->szEntryName);
            }

            hEditBox = GetDlgItem(hwndDlg, IDC_EDIT_PHONENO);
            if (hEditBox)
            {
                Edit_LimitText(hEditBox, CELEMS(lpRasEntry->szLocalPhoneNumber) - 1); // Count doesn't include NULL
                Edit_SetText(hEditBox, lpRasEntry->szLocalPhoneNumber);
            }
            
            //
            // enumerate to get the device name to use - we'll just do modems
            //
              
            // first get the size of the buffer required
            LPRASDEVINFO lpRasDevInfo = NULL;   // RASDEVINFO structure pointer
            DWORD dwNumEntries = 0;             // number of entries returned
            DWORD dwSize = sizeof(RASDEVINFO);  // size of buffer
            DWORD rc = 0;                       // return code


            // Allocate buffer with space for at least one structure
            lpRasDevInfo = (LPRASDEVINFO)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
            if (NULL == lpRasDevInfo)
            {
                return FALSE;
            }

            lpRasDevInfo->dwSize = sizeof(RASDEVINFO);

            rc = RasEnumDevices(lpRasDevInfo, &dwSize, &dwNumEntries);
            if (ERROR_BUFFER_TOO_SMALL == rc)
            {
                // If the buffer is too small, free the allocated memory and allocate a bigger buffer.
                if (HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasDevInfo))
                {
                    lpRasDevInfo = (LPRASDEVINFO)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
                    if (NULL == lpRasDevInfo)
                    {
                        OutputTraceString(L"--- Error allocating memory (HeapAlloc) for RASDEVINFO structures for RasEnumDevices(): %d\n", GetLastError());
                        return FALSE;
                    }

                    lpRasDevInfo->dwSize = sizeof(RASDEVINFO);

                    rc = RasEnumDevices(lpRasDevInfo, &dwSize, &dwNumEntries);
                }
                else
                {
                    // Couldn't free the memory
                    return FALSE;
                }
            }

            // Check whether RasEnumDevices succeeded
            if (ERROR_SUCCESS == rc)
            {
                lpRasDevInfo = (LPRASDEVINFO) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
                if (NULL != lpRasDevInfo)
                {
                    lpRasDevInfo->dwSize = sizeof(RASDEVINFO);

                    rc = RasEnumDevices(lpRasDevInfo, &dwSize, &dwNumEntries);
                    if (ERROR_SUCCESS == rc)
                    {
                        for (UINT i = 0; i < dwNumEntries; i++, lpRasDevInfo++)
                        {        
                            if (lstrcmpi(lpRasDevInfo->szDeviceType, RASDT_Modem) == 0)
                            {
                                if (hComboBox)
                                {
                                    // add to the list
                                    ComboBox_AddString(hComboBox, lpRasDevInfo->szDeviceName);         
                                }
                            }          
                        }
                    }

                    HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasDevInfo);
                    lpRasDevInfo = NULL;
                }
                else
                {
                    OutputTraceString(L"--- Error allocating memory (HeapAlloc) for RASDEVINFO structures for RasEnumDevices(): %d\n", GetLastError());
                }
            }
        
            

            if (hComboBox)
            {
                // select the item we have in the entry
                ComboBox_SelectString(hComboBox, -1, lpRasEntry->szDeviceName);
            }

            // move dialog and position according to structure paramters
            // NOTE: we don't take into account multiple monitors or extreme
            // cases here as this is only a quick sample
            
            DWORD xPos = 0;         // x coordinate position for centering
            DWORD yPos = 0;         // y coordinate position for centering

            if (lpInfo->dwFlags & RASDDFLAG_PositionDlg)
            {
                xPos = lpInfo->xDlg;
                yPos = lpInfo->yDlg;
            }
            else
            {
                RECT rectTop;         // parent rectangle used for centering
                RECT rectDlg;         // dialog rectangle used for centering

                // center window within the owner or desktop
                GetWindowRect(lpInfo->hwndOwner != NULL ? lpInfo->hwndOwner : GetDesktopWindow(), &rectTop);
                GetWindowRect(hwndDlg, &rectDlg);
                         
                xPos = ((rectTop.left + rectTop.right) / 2) - ((rectDlg.right - rectDlg.left) / 2);
                yPos = ((rectTop.top + rectTop.bottom) / 2) - ((rectDlg.bottom - rectDlg.top) / 2);
            }

            SetWindowPos(hwndDlg, NULL, xPos, yPos, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

            return TRUE;
        }
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDOK:          
            {
            TCHAR szEntryName[RAS_MaxEntryName + 1] = {0};  // Entry name
            TCHAR szPhoneBook[MAX_PATH + 1] = {0};          // Phonebook path
            TCHAR szPhoneNumber[RAS_MaxPhoneNumber + 1] = {0};

            // copy back the phonenumber, entry name, & device name
            hEditBox = GetDlgItem(hwndDlg, IDC_EDIT_PHONENO);
            if (hEditBox)
            {
                Edit_GetText(hEditBox, lpRasEntry->szLocalPhoneNumber, CELEMS(lpRasEntry->szLocalPhoneNumber));
            }
          
            hEditBox = GetDlgItem(hwndDlg, IDC_EDIT_ENTRYNAME);          
            if (hEditBox)
            {
                Edit_GetText(hEditBox, szEntryName, CELEMS(szEntryName));
            }
              
            hEditBox = GetDlgItem(hwndDlg, IDC_EDIT_PHONENO);
            if (hEditBox)
            {
                Edit_GetText(hEditBox, szPhoneNumber, CELEMS(szPhoneNumber));
            }

            hComboBox = GetDlgItem(hwndDlg, IDC_LIST_MODEMS);          
            if (hComboBox)
            {
                ComboBox_GetLBText(hComboBox, ComboBox_GetCurSel(hComboBox), lpRasEntry->szDeviceName);
            }
            
            if (!pData) return FALSE;

            // Check entry name for validity
            StringCchCopy(szPhoneBook, CELEMS(szPhoneBook), (LPTSTR)pData->szPhoneBookPath);          
            if (RasValidateEntryName(szPhoneBook, szEntryName) == ERROR_INVALID_NAME)
            {
                MessageBox(hwndDlg, L"The Entry Name is Invalid.", L"Entry name error", MB_OK);
            }
            else          
            {
                StringCchCopy(pData->szEntryName, CELEMS(pData->szEntryName), szEntryName);
                StringCchCopy(lpRasEntry->szLocalPhoneNumber, CELEMS(lpRasEntry->szLocalPhoneNumber), szPhoneNumber);
                
                EndDialog(hwndDlg, TRUE);
            }
            
            return TRUE;
            }
        case IDCANCEL:
            EndDialog(hwndDlg, FALSE);
            return TRUE;
        }
        break;      
    }

    return FALSE;
}