/****************************************************************** * does_port_exist (internal) * * returns TRUE, when the Port already exists * */ static BOOL does_port_exist(LPCWSTR myname) { LPPORT_INFO_1W pi; DWORD needed = 0; DWORD returned; DWORD id; TRACE("(%s)\n", debugstr_w(myname)); id = EnumPortsW(NULL, 1, NULL, 0, &needed, &returned); pi = heap_alloc(needed); returned = 0; if (pi) id = EnumPortsW(NULL, 1, (LPBYTE) pi, needed, &needed, &returned); if (id && returned > 0) { /* we got a number of valid names. */ for (id = 0; id < returned; id++) { if (lstrcmpiW(myname, pi[id].pName) == 0) { TRACE("(%u) found %s\n", id, debugstr_w(pi[id].pName)); heap_free(pi); return TRUE; } } } heap_free(pi); return FALSE; }
/* PortExists * * Calls EnumPorts to check whether the port name already exists. * This asks every monitor, rather than just this one. * The function will return TRUE if the specified port is in the list. * If an error occurs, the return is FALSE and the variable pointed * to by pError contains the return from GetLastError(). * The caller must therefore always check that *pError == NO_ERROR. */ BOOL PortExists( _In_opt_ LPWSTR pName, _In_ LPWSTR pPortName, _Out_ PDWORD pError ) { DWORD cbNeeded = 0; DWORD cReturned = 0; DWORD cbPorts = 0; LPPORT_INFO_1 pPorts = NULL; DWORD i = 0; BOOL Found = TRUE; *pError = NO_ERROR; if (!EnumPortsW(pName, 1, NULL, 0, &cbNeeded, &cReturned)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { cbPorts = cbNeeded; pPorts = (LPPORT_INFO_1)AllocSplMem(cbPorts); if (pPorts) { if (EnumPortsW(pName, 1, (LPBYTE)pPorts, cbPorts, &cbNeeded, &cReturned)) { Found = FALSE; for (i = 0; i < cReturned; i++) { if (!lstrcmpi(pPorts[i].pName, pPortName)) { Found = TRUE; break; } } } } FreeSplMem(pPorts); } } else { Found = FALSE; } return Found; }