@@ -120,25 +120,71 @@ serial_test
120120----------------------------------------------------------*/
121121int serial_test ( char * filename )
122122{
123- unsigned long * hcomm ;
124- int ret ;
125- hcomm = CreateFile ( filename , GENERIC_READ |GENERIC_WRITE , 0 , 0 , OPEN_EXISTING , 0 , 0 );
126- if ( hcomm == INVALID_HANDLE_VALUE )
123+ int ret = 0 ;
124+
125+ // Getting the Windows Version
126+ OSVERSIONINFO osvi ;
127+ BOOL bGetVer ;
128+ osvi .dwOSVersionInfoSize = sizeof (OSVERSIONINFO );
129+ bGetVer = GetVersionEx (& osvi );
130+
131+ // Using the QueryDosDevice API (on NT)
132+ if (bGetVer && (osvi .dwPlatformId == VER_PLATFORM_WIN32_NT ))
133+ {
134+ // This solution is based on http://www.codeproject.com/KB/system/enumports.aspx
135+ TCHAR szDevices [65535 ];
136+ DWORD dwChars = QueryDosDevice (NULL , szDevices , 65535 );
137+
138+ if (dwChars )
139+ {
140+ int i = 0 ;
141+ for (;;)
142+ {
143+ //Get the current device name
144+ char * pszCurrentDevice = & szDevices [i ];
145+
146+ if (strlen (pszCurrentDevice ) > 3 && strcmp (pszCurrentDevice ,filename )== 0 )
147+ {
148+ ret = 1 ;
149+ break ;
150+ }
151+
152+ // Go to next NULL character
153+ while (szDevices [i ] != '\0' )
154+ i ++ ;
155+
156+ // Bump pointer to the next string
157+ i ++ ;
158+
159+ // The list is double-NULL terminated, so if the character is
160+ // now NULL, we're at the end
161+ if (szDevices [i ] == '\0' )
162+ break ;
163+ }
164+ }
165+ }
166+ else
127167 {
128- if (GetLastError () == ERROR_ACCESS_DENIED )
168+ // Buggy way to test if we can open the comport (on Win9x)
169+ unsigned long * hcomm ;
170+ hcomm = CreateFile ( filename , GENERIC_READ |GENERIC_WRITE , 0 , 0 , OPEN_EXISTING , 0 , 0 );
171+ if ( hcomm == INVALID_HANDLE_VALUE )
129172 {
130- ret = 1 ;
173+ if (GetLastError () == ERROR_ACCESS_DENIED )
174+ {
175+ ret = 1 ;
176+ }
177+ else
178+ {
179+ ret = 0 ;
180+ }
131181 }
132182 else
133183 {
134- ret = 0 ;
184+ ret = 1 ;
135185 }
186+ CloseHandle ( hcomm );
136187 }
137- else
138- {
139- ret = 1 ;
140- }
141- CloseHandle ( hcomm );
142188 return (ret );
143189}
144190
0 commit comments