Board index » cppbuilder » How to detect COM installed
James5950
![]() CBuilder Developer |
James5950
![]() CBuilder Developer |
How to detect COM installed2008-06-03 04:19:35 PM cppbuilder29 Dear All, How to detect COM install on user system? Thansk any comments and help. Best regards, James |
Jason Cipriani
![]() CBuilder Developer |
2008-06-03 10:18:41 PM
Re:How to detect COM installed
"James5950" < XXXX@XXXXX.COM >wrote in message
QuoteHow to detect COM install on user system? |
James5950
![]() CBuilder Developer |
2008-06-03 11:34:01 PM
Re:How to detect COM installedQuoteasking, are you having a specific issue? Thanks you, James {smallsort} |
Jason Cipriani
![]() CBuilder Developer |
2008-06-03 11:40:34 PM
Re:How to detect COM installed
"James5950" < XXXX@XXXXX.COM >wrote in message
Quote>asking, are you having a specific issue? interfaces in the 3rd party library, then it would fail. E.g. CoCreateInstance failing with REGDB_E_CLASSNOTREG, or QueryInterface failing with E_NOINTERFACE. You may wish to consult the documentation for the 3rd party library to see if they have any specific techniques that they recommend. Jason |
Colin B Maharaj
![]() CBuilder Developer |
2008-06-10 10:40:35 AM
Re:How to detect COM installed
Firstly, sorry the code is poorly designed, but works!
I cut and paste from some code I have. Call EnumSerialPorts() and return it into an AnsiString. The return value is a csv list of the COM ports that exists. NOTE: This code does not say if it is in use. If you call code to open a comport and it cannot be opened then either it is in use or it does not exists. //------------------------------------------------------- bool COM1, COM2, COM3, COM4; bool COM5, COM6, COM7, COM8; //------------------------------------------------------- bool COMExists(char *ComStr) { HKEY hKey; DWORD keyType = 0; DWORD retValue = 0; DWORD keyLength = 0; char *t = new char [MAX_PATH]; char *CV = new char [256]; strcpy(CV, "Hardware\\DeviceMap\\SerialComm"); // safe retValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, CV, NULL, KEY_READ, &hKey); if (retValue != ERROR_SUCCESS) { delete []CV; delete []t; return false; } RegQueryValueEx(hKey, ComStr, NULL, &keyType, NULL, &keyLength); retValue = RegQueryValueEx(hKey, ComStr, NULL, &keyType, (UCHAR *)t, &keyLength); RegCloseKey(hKey); if (retValue != ERROR_SUCCESS) { delete []CV; delete []t; return false; } if (strcmp(t, "COM1")==0) COM1 = true; if (strcmp(t, "COM2")==0) COM2 = true; if (strcmp(t, "COM3")==0) COM3 = true; if (strcmp(t, "COM4")==0) COM4 = true; if (strcmp(t, "COM5")==0) COM5 = true; if (strcmp(t, "COM6")==0) COM6 = true; if (strcmp(t, "COM7")==0) COM7 = true; if (strcmp(t, "COM8")==0) COM8 = true; delete []CV; delete []t; return true; } //------------------------------------------------------- AnsiString EnumSerialPorts() { AnsiString COMcsv = ""; if (Win32Platform == VER_PLATFORM_WIN32_NT) { COMExists("\\Device\\Serial0"); // NT/2k/xp COMExists("\\Device\\Serial1"); COMExists("\\Device\\Serial2"); COMExists("\\Device\\Serial3"); COMExists("\\Device\\Serial4"); COMExists("\\Device\\Serial5"); COMExists("\\Device\\Serial6"); COMExists("\\Device\\Serial7"); } else { COMExists("COM1"); // 9x COMExists("COM2"); COMExists("COM3"); COMExists("COM4"); COMExists("COM5"); COMExists("COM6"); COMExists("COM7"); COMExists("COM8"); } if (COM1 == true) COMcsv = "COM1"; if (COM2 == true) COMcsv += "\,COM2"; if (COM3 == true) COMcsv += "\,COM3"; if (COM4 == true) COMcsv += "\,COM4"; if (COM5 == true) COMcsv += "\,COM5"; if (COM6 == true) COMcsv += "\,COM6"; if (COM7 == true) COMcsv += "\,COM7"; if (COM8 == true) COMcsv += "\,COM8"; return COMcsv; } //------------------------------------------------------- James5950 wrote: QuoteDear All, |
Peter Stefanos
![]() CBuilder Developer |
2008-06-10 11:24:59 AM
Re:How to detect COM installed
Colin B Maharaj wrote:
Quote
Here's a simple function to test if a COM port exists. (It's a little crude) bool ComPortExists(int ComPortNumber) { HANDLE Handle; char Buffer[100]; bool Ret = false; sprintf(Buffer, "\\\\.\\COM%d", ComPortNumber); Handle = CreateFile(Buffer, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // Notes: If Handle == INVALID_HANDLE_VALUE *AND* // if errno == 5 the port does exist, but cannot be opened ... // if errno == 2 doesn't exist, otherwise error. if ((Handle != INVALID_HANDLE_VALUE) || (GetLastError() == 5)) { CloseHandle(Handle); // Not sure if this is necessary. Probably not. Ret = true; } return Ret; } |
Colin B Maharaj
![]() CBuilder Developer |
2008-06-10 11:38:13 AM
Re:How to detect COM installed
Holy..... can't believe I did
that. Saw COM and thought it was COM port... That dreaded COM. Peter Stefanos wrote: QuoteColin B Maharaj wrote: |