home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / listcomx.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  2KB  |  99 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5. static char ** serial_list = NULL;
  6. static int     serial_cnt  = 0;
  7.  
  8. __declspec(dllexport) DWORD _stdcall
  9. init_serial_port_list(void)
  10. {
  11.     HKEY  hk=0;
  12.     DWORD dwType=0;
  13.     DWORD dwValueSize=0, dwDataSize=0;
  14.     CHAR *lpszValueName=NULL;
  15.     char  serialdevice[64]="";
  16.     char  dosname[12]="";
  17.     int i;
  18.     DWORD rc;
  19.  
  20.     if ( serial_list )
  21.     {
  22.         for ( i=0 ; i < serial_cnt ; i++ )
  23.             free( serial_list[i] ) ;
  24.         free ( serial_list )  ;
  25.     }
  26.     serial_list = NULL;
  27.     serial_cnt = 0;
  28.  
  29.     if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  30.                        "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0,
  31.                         KEY_READ, &hk) )
  32.         goto init_exit;
  33.  
  34.     for ( ;; ) {
  35.         dwValueSize = sizeof(serialdevice);
  36.         dwDataSize = sizeof(dosname);
  37.         rc = RegEnumValue(hk, serial_cnt, serialdevice, &dwValueSize, 0,
  38.                            &dwType, dosname, &dwDataSize);
  39.         if ( rc == ERROR_SUCCESS || rc == ERROR_MORE_DATA )
  40.             serial_cnt++;
  41.         else 
  42.             break;
  43.     }
  44.     if (serial_cnt == 0)
  45.         goto init_exit;
  46.  
  47.     serial_list = (char **) malloc( serial_cnt * sizeof(char *) );
  48.     if ( !serial_list ) {
  49.         serial_cnt = 0;
  50.         goto init_exit;
  51.     }
  52.  
  53.     for ( i=0;i<serial_cnt;i++ ) {
  54.         dwValueSize = sizeof(serialdevice);
  55.         dwDataSize = sizeof(dosname);
  56.         rc = RegEnumValue(hk, i, serialdevice, &dwValueSize, 0,
  57.                            &dwType, dosname, &dwDataSize);
  58.         if ( rc == ERROR_SUCCESS || rc == ERROR_MORE_DATA ) {
  59.             serial_list[i] = strdup(dosname);
  60.         }
  61.     }
  62.  
  63.   init_exit:
  64.     RegCloseKey( hk );
  65.     return(serial_cnt);
  66. }
  67.  
  68. __declspec(dllexport) DWORD _stdcall
  69. get_serial_port_display_string(int index, char * str, int len)
  70. {
  71.     if ( index < 0 || index >= serial_cnt || !serial_list )
  72.         return(0);
  73.  
  74.     if ( len < strlen(serial_list[index]) + 1 )
  75.         return(-(long)strlen(serial_list[index]));
  76.  
  77.     strncpy(str,serial_list[index],len);
  78.     str[len-1] = '\0';
  79.     return(1);
  80. }
  81.  
  82.  
  83. int 
  84. main(void)
  85. {
  86.     char str[256], s1[64], s2[64], s3[64];
  87.     int  n;
  88.  
  89.     n = init_serial_port_list();
  90.     printf("%d serial ports\n");
  91.     while ( n-- > 0 ) {
  92.         get_serial_port_display_string(n,str,sizeof(str));
  93.         printf("%s\n",str);
  94.     }
  95.  
  96.     return(0);
  97. }
  98.  
  99.