INFO: GetCommProperties Returns Error 122 If Called from TAPI Ap (162136)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows NT 4.0
    • the operating system: Microsoft Windows 2000
    • the operating system: Microsoft Windows XP

This article was previously published under Q162136

SUMMARY

Under Microsoft Windows NT 4.0, Windows 2000, and Windows XP GetCommProperties API call fails when called using a communication handle obtained from the TAPI lineGetID API if COMMPROP structure is not properly initialized.

Under Windows 95, UNIMODEM expects the structure to be zero initialized.

This behavior is specific to UNIMODEM as a TAPI Service Provider.

MORE INFORMATION

Under Windows 95's UNIMODEM, GetCommProperties succeeds when COMMPROP is allocated and zero initialized as follows:
   DWORD dwError;
   COMMPROP commprop;

   memset(&commprop, 0, sizeof(COMMPROP));
   if(!GetCommProperties(hCommHandle, &commprop))
   {
     dwError = GetLastError();
   }
				
The same code fails under Windows NT, Windows 2000, and Window XP and dwError will be set to 122 (ERROR_INSUFFICIENT_BUFFER).

This occurs when UNIMODEM tries to append provider specific information to the end of COMMPROP in a form of MODEMDEVCAPS. To work around this, you need to allocate space for MODEMDEVCAPS structure after COMMPROP. You also need to set the appropriate member variable in COMMPROP so UNIMODEM knows that the structure has been allocated to the proper size. The following code sample demonstrates this process:
   DWORD dwSize;
   COMMPROP *commprop;
   DWORD dwError;

   dwSize = sizeof(COMMPROP) + sizeof(MODEMDEVCAPS) ;
   commprop = (COMMPROP *)malloc(dwSize);
   memset(commprop, 0, dwSize);

   commprop->wPacketLength = dwSize;
   commprop->dwProvSubType = PST_MODEM;
   commprop->dwProvSpec1 = COMMPROP_INITIALIZED;

   if(!GetCommProperties(hNewCommFile, commprop))
   {
     dwError = GetLastError();
   }
				
The following example demonstrates a method that works for both platforms. The code below calls GetCommProperties first with the Windows 95 style structure allocation. If that fails, it calls GetCommProperties with Windows NT, Windows 2000, and Windows XP style structure allocation.
   COMMTIMEOUTS commtimeouts;
   DCB dcb;
   DWORD dwSize;
   COMMPROP *commprop;
   DWORD fdwEvtMask;
   DWORD dwError;

   dwSize = sizeof(COMMPROP);
   commprop = (COMMPROP *)malloc(dwSize);
   memset(commprop, 0, dwSize);

   GetCommState(hComm, &dcb);
   if(!GetCommProperties(hComm, commprop))
   {
     if(GetLastError()==122)
     {
        free(commprop);
        dwSize = sizeof(COMMPROP) + sizeof(MODEMDEVCAPS);
        commprop = (COMMPROP *)malloc(dwSize);
        memset(commprop, 0, dwSize);

        commprop->wPacketLength = dwSize;
        commprop->dwProvSubType = PST_MODEM;
        commprop->dwProvSpec1 = COMMPROP_INITIALIZED;
        if(!GetCommProperties(hComm, commprop))
        {
          dwError = GetLastError();
        }
     }
   }

   free(commprop);
				

Modification Type:MajorLast Reviewed:4/13/2004
Keywords:kbAPI kbinfo kbKernBase kbTAPI KB162136