plhelp.cpp
上传用户:hcyyun520
上传日期:2019-05-14
资源大小:365k
文件大小:3k
源码类别:

TAPI编程

开发平台:

Visual C++

  1. // plhelp.cpp: Provider List Helpers
  2. #include "pch.h"
  3. // Caller responsible for freeing returned memory
  4. LONG GetProviderList(
  5.     LINEPROVIDERLIST**  ppd)
  6. {
  7.     // Load tapi32.dll explictly. This won't matter
  8.     // for clients that already have tapi32.dll loaded,
  9.     // but makes all the difference if they don't.
  10.     HINSTANCE   hinstDll = 0;
  11.     LONG        (WINAPI* pfnGetProviderList)(DWORD, LINEPROVIDERLIST*) = 0;
  12.     if( hinstDll = LoadLibrary("tapi32.dll") )
  13.     {
  14.         *(FARPROC*)&pfnGetProviderList = GetProcAddress(hinstDll, "lineGetProviderList");
  15.         if( !pfnGetProviderList )
  16.         {
  17.             FreeLibrary(hinstDll);
  18.             return LINEERR_OPERATIONFAILED;
  19.         }
  20.     }
  21.     else
  22.     {
  23.         return LINEERR_OPERATIONFAILED;
  24.     }
  25.     // Fill LINEPROVIDERLIST
  26.     DWORD dwNeededSize = sizeof(LINEPROVIDERLIST);
  27.     LONG  tr = 0;
  28.     do
  29.     {
  30.         // Get some more memory if we don't have enough
  31.         if( !*ppd || (*ppd)->dwTotalSize < dwNeededSize )
  32.         {
  33.             *ppd = (LINEPROVIDERLIST*)::realloc(*ppd, dwNeededSize);
  34.             if( *ppd )
  35.             {
  36.                 (*ppd)->dwTotalSize = dwNeededSize;
  37.             }
  38.             else
  39.             {
  40.                 return LINEERR_NOMEM;
  41.             }
  42.         }
  43.         // Fill in the buffer
  44.         tr = pfnGetProviderList(TAPI_CURRENT_VERSION, *ppd);
  45.         // Check how much memory we need
  46.         // (some TSPs succeed even if the
  47.         //  data size is too small)
  48.         if( tr == LINEERR_STRUCTURETOOSMALL ||
  49.             (tr == 0 &&
  50.              (*ppd)->dwTotalSize < (*ppd)->dwNeededSize) )
  51.         {
  52.             dwNeededSize = (*ppd)->dwNeededSize;
  53.             tr = LINEERR_STRUCTURETOOSMALL;
  54.         }
  55.     }
  56.     while( tr == LINEERR_STRUCTURETOOSMALL );
  57.     // Unload tapi32.dll (or just release our reference to it)
  58.     FreeLibrary(hinstDll);
  59.     return tr;
  60. }
  61. bool GetPermanentProviderID(LPCSTR pszProvider, DWORD* pdwID)
  62. {
  63.     bool    bSucceeded = false;
  64.     LINEPROVIDERLIST*   ppl = 0;
  65.     if( GetProviderList(&ppl) == 0 )
  66.     {
  67.         LPLINEPROVIDERENTRY rglpe = (LPLINEPROVIDERENTRY)((BYTE*)ppl + ppl->dwProviderListOffset);
  68.         for( DWORD i = 0; i < ppl->dwNumProviders; i++ )
  69.         {
  70.             LPCSTR pszProviderFilename = (LPCSTR)((BYTE*)ppl + rglpe[i].dwProviderFilenameOffset);
  71.             if( strcmpi(pszProviderFilename, pszProvider) == 0 )
  72.             {
  73.                 *pdwID = rglpe[i].dwPermanentProviderID;
  74.                 bSucceeded = true;
  75.                 break;
  76.             }
  77.         }
  78.         free(ppl);
  79.     }
  80.     return bSucceeded;
  81. }