BOOL findEntry(DWORD* foundOut) { DWORD dwCb = 0; DWORD dwRet = ERROR_SUCCESS; DWORD dwEntries = 0; LPRASENTRYNAME lpRasEntryName = NULL; // Call RasEnumEntries with lpRasEntryName = NULL. dwCb is returned with the required buffer size and // a return code of ERROR_BUFFER_TOO_SMALL dwRet = RasEnumEntries(NULL, PHONE_BOOK, lpRasEntryName, &dwCb, &dwEntries); if (dwRet == ERROR_BUFFER_TOO_SMALL) { // Allocate the memory needed for the array of RAS entry names. lpRasEntryName = (LPRASENTRYNAME)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb); if (lpRasEntryName == NULL){ fwprintf(stderr, L"[findEntry] HeapAlloc\n"); return FALSE; } // The first RASENTRYNAME structure in the array must contain the structure size lpRasEntryName[0].dwSize = sizeof(RASENTRYNAME); // Call RasEnumEntries to enumerate all RAS entry names dwRet = RasEnumEntries(NULL, PHONE_BOOK, lpRasEntryName, &dwCb, &dwEntries); // If successful, print the RAS entry names if (ERROR_SUCCESS == dwRet){ for (DWORD i = 0; i < dwEntries; i++){ if (lstrcmp(ENTRY_NAME_W, lpRasEntryName[i].szEntryName) == 0) { wprintf(L"Found entry: %ls\n", ENTRY_NAME_W); *foundOut = 1; break; } } } //Deallocate memory for the connection buffer HeapFree(GetProcessHeap(), 0, lpRasEntryName); lpRasEntryName = NULL; return TRUE; } return TRUE; }
int GetPhoneBookEntries(PHONE_ENTRY (&phoneEntries)[MAX_PHONE_ENTRY]) { DWORD dwCb = 0; DWORD dwRet = ERROR_SUCCESS; DWORD dwEntries = 0; LPRASENTRYNAME lpRasEntryName = NULL; // Call RasEnumEntries with lpRasEntryName = NULL. dwCb is returned with the required buffer size and // a return code of ERROR_BUFFER_TOO_SMALL dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries); if (dwRet == ERROR_BUFFER_TOO_SMALL) { // Allocate the memory needed for the array of RAS entry names. lpRasEntryName = (LPRASENTRYNAME) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb); if (lpRasEntryName == NULL) { wprintf(L"HeapAlloc failed!\r\n"); return 0; } // The first RASENTRYNAME structure in the array must contain the structure size lpRasEntryName[0].dwSize = sizeof(RASENTRYNAME); // Call RasEnumEntries to enumerate all RAS entry names dwRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &dwCb, &dwEntries); // If successful, print the RAS entry names if (ERROR_SUCCESS == dwRet) { wprintf(L"The following RAS entry names were found:\n"); for (DWORD i = 0; i < min(dwEntries,MAX_PHONE_ENTRY) ; i++) { //wsprintf(phoneEntries[i].entryName, L"%s\n", lpRasEntryName[i].szEntryName); memset(phoneEntries[i].entryName, 0, sizeof(phoneEntries[i].entryName)); strcpy_s( phoneEntries[i].entryName, sizeof(phoneEntries[i].entryName), lpRasEntryName[i].szEntryName); } } //Deallocate memory for the connection buffer HeapFree(GetProcessHeap(), 0, lpRasEntryName); lpRasEntryName = NULL; } return min(dwEntries,MAX_PHONE_ENTRY); }
int rashelper_check_profiles(char *profile_name, char *server_address) { RASENTRYNAME *entries = NULL; DWORD cb = sizeof(RASENTRYNAME); DWORD numEntries; int rc; int retval = 0; int matches = 0; int i; printf("rashelper_check_profiles: first LocalAlloc\n"); /* http://msdn2.microsoft.com/en-us/library/aa366597(VS.85).aspx */ entries = (RASENTRYNAME *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb); if (!entries) { goto cleanup; } entries->dwSize = sizeof(RASENTRYNAME); /* http://msdn2.microsoft.com/en-us/library/aa920280.aspx */ if (RasEnumEntries(NULL, NULL, entries, &cb, &numEntries) == ERROR_BUFFER_TOO_SMALL) { /* allocate larger buffer */ if (entries) { HeapFree(GetProcessHeap(), 0, entries); entries = NULL; } printf("rashelper_check_profiles: second LocalAlloc, size %d\n", cb); entries = (RASENTRYNAME *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb); if (!entries) { goto cleanup; } entries->dwSize = sizeof(RASENTRYNAME); } printf("rashelper_check_profiles: second RasNumEntries\n"); /* actual call */ for (i = 0; i < RAS_LOOP_COUNT; i++) { /* * Why do we loop here? The MSDN documentation indicates that this * might be smart. Further, when prohibitIpsec registry value has * been changed or created, the call seems to fail at first for * some unfathomable reason, with error 632 (invalid struct size); * this seems completely incorrect from Windows, but in this loop * we don't care what fails. * */ rc = RasEnumEntries(NULL, NULL, entries, &cb, &numEntries); if (rc == ERROR_SUCCESS) { break; } printf("rashelper_check_profiles: RasEnumEntries failed with rc %d, ignoring this round (sleep & retry)\n", rc); Sleep(1000); } if (rc != ERROR_SUCCESS) { printf("rashelper_check_profiles: RasEnumEntries failed, %d\n", rc); goto cleanup; } printf("Found %d RAS entries\n", numEntries); for (i = 0; i < numEntries; i++) { printf("RAS entry %d: %s\n", i, entries[i].szEntryName); if (strcmp(entries[i].szEntryName, profile_name) == 0) { matches ++; } } retval = matches; /* fall through */ cleanup: if (entries) { HeapFree(GetProcessHeap(), 0, entries); entries = NULL; } return retval; }
int __cdecl main(void) { LPRASENTRYNAME lpRasEntryName = NULL; LPRASENTRYNAME lpTempRasEntryName = NULL; DWORD cb = sizeof(RASENTRYNAME); DWORD cEntries = 0; int nRet = 0; DWORD i = 0; BOOL fSuccess = FALSE; TCHAR szTempBuf[256] = {0}; lpRasEntryName = (LPRASENTRYNAME)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb); if (NULL == lpRasEntryName) { printf("HeapAlloc failed.\n"); return ERROR_OUTOFMEMORY; } lpRasEntryName->dwSize = sizeof(RASENTRYNAME); // Getting the size required for the RASENTRYNAME buffer nRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &cb, &cEntries); switch (nRet) { case ERROR_BUFFER_TOO_SMALL: if (HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasEntryName)) { lpRasEntryName = (LPRASENTRYNAME)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb); if (NULL == lpRasEntryName) { printf("HeapAlloc failed.\n"); return ERROR_OUTOFMEMORY; } lpRasEntryName->dwSize = sizeof(RASENTRYNAME); // Calling RasEnumEntries to enumerate the phonebook entries for a default phonebook nRet = RasEnumEntries(NULL, NULL, lpRasEntryName, &cb, &cEntries); if (ERROR_SUCCESS != nRet) { printf("RasEnumEntries failed: Error %d\n", nRet); goto done; } else { fSuccess = TRUE; } } else { printf("HeapFree failed.\n"); return GetLastError(); } break; case ERROR_SUCCESS: fSuccess = TRUE; break; default: printf("RasEnumEntries failed: Error = %d\n", nRet); goto done; } if (fSuccess) { printf("Phone book entries in the default phonebook:\n\n"); lpTempRasEntryName = lpRasEntryName; for (i = 0; i < cEntries; i++) { ZeroMemory((LPVOID)szTempBuf, sizeof(szTempBuf)); StringCchPrintf(szTempBuf, CELEMS(szTempBuf), "%s\n",lpTempRasEntryName->szEntryName); printf(szTempBuf); lpTempRasEntryName++; } } done: HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasEntryName); return nRet; }