Board index » cppbuilder » How to detect COM installed

How to detect COM installed


2008-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
 
 

Re:How to detect COM installed

"James5950" < XXXX@XXXXX.COM >wrote in message
Quote
How to detect COM install on user system?
COM is a built-in part of Windows. It's always "installed". Why are you
asking, are you having a specific issue?
 

Re:How to detect COM installed

Quote
asking, are you having a specific issue?


Yes, I use 3rd party COM, I need install this COM first, so my software need
to detect COM is installed.
Thanks you,
James
 

{smallsort}

Re:How to detect COM installed

"James5950" < XXXX@XXXXX.COM >wrote in message
Quote
>asking, are you having a specific issue?
>
>
Yes, I use 3rd party COM, I need install this COM first, so my software
need to detect COM is installed.
I would assume that if the 3rd party COM library is not installed, then when
you tried to CoCreateInstance or QueryInterface one of the COM objects or
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
 

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:
Quote
Dear All,

How to detect COM install on user system?

Thansk any comments and help.
Best regards,
James

 

Re:How to detect COM installed

Colin B Maharaj wrote:
Quote

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.

Ahhh you mean COM Port.
You can test the existance of a COM port using the CreateFile() function.
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;
}
 

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:
Quote
Colin B Maharaj wrote:
>
>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.
>

Ahhh you mean COM Port.

You can test the existance of a COM port using the CreateFile() function.

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;
}