All Classes Namespaces Files Functions Variables Enumerations Enumerator
libs/connection/isUsingComDriver.cpp
Go to the documentation of this file.
00001 
00010 #include "isUsingComDriver.h"
00011 #include "DetectOS.h"
00012 #ifdef WINDOWS_OS
00013 #include <windows.h>
00014 #include <string>
00015 #endif
00016 
00017 using namespace std;
00018 
00019 
00020 #ifdef WINDOWS_OS
00021 namespace
00022 {
00023     bool existRegValue(HKEY hkey, LPCSTR subkey, const char* find_value)
00024     {
00025         HKEY next_hkey;
00026         if (RegOpenKeyExA(hkey, subkey,
00027                           0, KEY_READ, &next_hkey) != ERROR_SUCCESS) {
00028             return false;
00029         }
00030         enum { MaxLength = 1024 };
00031 
00032         // find_value が存在するかを探索
00033         CHAR device[MaxLength + 1];
00034         char name[MaxLength + 1];
00035 
00036         DWORD ret = ERROR_SUCCESS;
00037         for (int i = 0; ret == ERROR_SUCCESS; ++i) {
00038             DWORD dl = MaxLength;
00039             DWORD nl = MaxLength;
00040             ret = RegEnumValueA(hkey, i, device, &dl,
00041                                 NULL, NULL, (BYTE*)name, &nl);
00042             if (ret != ERROR_SUCCESS) {
00043                 break;
00044             }
00045             if (! strncmp(name, find_value, nl)) {
00046                 RegCloseKey(next_hkey);
00047                 return true;
00048             }
00049         }
00050 
00051         // find_value が存在しなければ、再帰探索を行う
00052         char next_subkey[MaxLength];
00053         FILETIME filetime;
00054 
00055         ret = ERROR_SUCCESS;
00056         for (int i = 0; ret == ERROR_SUCCESS; ++i) {
00057             DWORD dl = MaxLength, nl = MaxLength;
00058             ret = RegEnumKeyExA(next_hkey, i, next_subkey,
00059                                 &dl, NULL, NULL, &nl, &filetime);
00060             if (ret != ERROR_SUCCESS) {
00061                 break;
00062             }
00063 
00064             bool value_exist =
00065                 existRegValue(next_hkey, next_subkey, find_value);
00066             if (value_exist) {
00067                 RegCloseKey(next_hkey);
00068                 return true;
00069             }
00070         }
00071 
00072         RegCloseKey(next_hkey);
00073         return false;
00074     }
00075 }
00076 
00077 
00078 bool qrk::isUsingComDriver(const char* com_port, const char* driver_name)
00079 {
00080     // 指定した名前と一致するかを確認する
00081     // !!!
00082 
00083     // 指定ドライバ名が が Value に含まれているか検索
00084     string value_pattern = string(driver_name) + " (" + com_port + ")";
00085     if (existRegValue(HKEY_LOCAL_MACHINE,
00086                       "SYSTEM\\CurrentControlSet\\Enum\\USB",
00087                       value_pattern.c_str())) {
00088         return true;
00089     }
00090     return false;
00091 }
00092 #else
00093 
00094 
00095 bool qrk::isUsingComDriver(const char* com_port, const char* driver_name)
00096 {
00097     static_cast<void>(com_port);
00098     static_cast<void>(driver_name);
00099 
00100     return false;
00101 }
00102 #endif