2 * PROJECT: Ports installer library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll\win32\msports\comdb.c
5 * PURPOSE: COM port database
6 * COPYRIGHT: Copyright 2011 Eric Kohl
12 #include <wine/debug.h>
14 WINE_DEFAULT_DEBUG_CHANNEL(msports
);
16 #define BITS_PER_BYTE 8
29 ComDBClaimPort(IN HCOMDB hComDB
,
42 if (hComDB
== INVALID_HANDLE_VALUE
||
45 ComNumber
> COMDB_MAX_PORTS_ARBITRATED
)
46 return ERROR_INVALID_PARAMETER
;
48 pComDB
= (PCOMDB
)hComDB
;
50 /* Update the bitmap */
51 dwSize
= pComDB
->dwSize
;
52 lError
= RegQueryValueExW(pComDB
->hKey
,
58 if (lError
!= ERROR_SUCCESS
)
61 /* Get the bit index */
62 dwBitIndex
= ComNumber
- 1;
64 /* Check if the bit to set fits into the bitmap */
65 if (dwBitIndex
>= (pComDB
->dwSize
* BITS_PER_BYTE
))
67 /* FIXME: Resize the bitmap */
68 return ERROR_INVALID_PARAMETER
;
71 /* Get a pointer to the affected byte and calculate a mask for the affected bit */
72 pByte
= &(pComDB
->pBitmap
[dwBitIndex
/ BITS_PER_BYTE
]);
73 cMask
= 1 << (dwBitIndex
% BITS_PER_BYTE
);
75 /* Check if the bit is not set */
76 if ((*pByte
& cMask
) == 0)
80 lError
= ERROR_SUCCESS
;
84 /* The bit is already set */
85 lError
= ERROR_SHARING_VIOLATION
;
88 /* Save the bitmap if it was modified */
89 if (lError
== ERROR_SUCCESS
)
91 lError
= RegSetValueExW(pComDB
->hKey
,
105 ComDBClose(IN HCOMDB hComDB
)
109 if (hComDB
== HCOMDB_INVALID_HANDLE_VALUE
|| hComDB
== NULL
)
110 return ERROR_INVALID_PARAMETER
;
112 pComDB
= (PCOMDB
)hComDB
;
114 /* Close the registry key */
115 if (pComDB
->hKey
!= NULL
)
116 RegCloseKey(pComDB
->hKey
);
118 /* Release the bitmap */
119 if (pComDB
->pBitmap
!= NULL
)
120 HeapFree(GetProcessHeap(), 0, pComDB
->pBitmap
);
122 /* Release the database */
123 HeapFree(GetProcessHeap(), 0, pComDB
);
125 return ERROR_SUCCESS
;
131 ComDBOpen(OUT HCOMDB
*phComDB
)
138 TRACE("ComDBOpen(%p)\n", phComDB
);
140 /* Allocate a new database */
141 pComDB
= HeapAlloc(GetProcessHeap(),
146 ERR("Failed to allocaete the database!\n");
147 return ERROR_ACCESS_DENIED
;
150 /* Create or open the database key */
151 lError
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
152 L
"System\\CurrentControlSet\\Control\\COM Name Arbiter",
160 if (lError
!= ERROR_SUCCESS
)
163 /* Get the required bitmap size */
164 lError
= RegQueryValueExW(pComDB
->hKey
,
170 if (lError
== ERROR_FILE_NOT_FOUND
)
172 /* Allocate a new bitmap */
173 pComDB
->dwSize
= COMDB_MIN_PORTS_ARBITRATED
/ BITS_PER_BYTE
;
174 pComDB
->pData
= HeapAlloc(GetProcessHeap(),
177 if (pComDB
->pData
== NULL
)
179 ERR("Failed to allocaete the bitmap!\n");
180 lError
= ERROR_ACCESS_DENIED
;
184 /* Read the bitmap from the registry */
185 lError
= RegSetValueExW(pComDB
->hKey
,
194 if (lError
!= ERROR_SUCCESS
)
196 /* Clean up in case of failure */
197 if (pComDB
->hKey
!= NULL
)
198 RegCloseKey(pComDB
->hKey
);
200 if (pComDB
->pData
!= NULL
)
201 HeapFree(GetProcessHeap(), 0, pComDB
->pData
);
203 HeapFree(GetProcessHeap(), 0, pComDB
);
205 *phComDB
= HCOMDB_INVALID_HANDLE_VALUE
;
209 /* Return the database handle */
210 *phComDB
= (HCOMDB
)pComDB
;
213 TRACE("done (Error %lu)\n", lError
);