From: wittend99 Date: Tue, 4 Dec 2012 03:41:59 +0000 (+0000) Subject: git-svn-id: https://svn.code.sf.net/p/freetel/code@1116 01035d8c-6547-0410-b346-abe4f... X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=4298800a1b83d348c8abf43fe6716c5a5c4c8c60;p=freetel-svn-tracking.git git-svn-id: https://svn.code.sf.net/p/freetel/code@1116 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/fdmdv2/tests/serial_enum/Debug/.gitignore b/fdmdv2/tests/serial_enum/Debug/.gitignore new file mode 100644 index 00000000..e69de29b diff --git a/fdmdv2/tests/serial_enum/enumSerial.mk b/fdmdv2/tests/serial_enum/enumSerial.mk new file mode 100644 index 00000000..48d5b159 --- /dev/null +++ b/fdmdv2/tests/serial_enum/enumSerial.mk @@ -0,0 +1,111 @@ +## +## Auto Generated makefile by CodeLite IDE +## any manual changes will be erased +## +## Debug +ProjectName :=enumSerial +ConfigurationName :=Debug +WorkspacePath := "D:\Projects\Radio\fdmdv2\build" +ProjectPath := "D:\Projects\Radio\fdmdv2\tests\serial_enum" +IntermediateDirectory :=./Debug +OutDir := $(IntermediateDirectory) +CurrentFileName := +CurrentFilePath := +CurrentFileFullPath := +User :=wittend +Date :=12/3/2012 +CodeLitePath :="D:\bin\CodeLite" +LinkerName :=g++ +SharedObjectLinkerName :=g++ -shared -fPIC +ObjectSuffix :=.o +DependSuffix :=.o.d +PreprocessSuffix :=.o.i +DebugSwitch :=-gstab +IncludeSwitch :=-I +LibrarySwitch :=-l +OutputSwitch :=-o +LibraryPathSwitch :=-L +PreprocessorSwitch :=-D +SourceSwitch :=-c +OutputFile :=$(IntermediateDirectory)/$(ProjectName) +Preprocessors :=$(PreprocessorSwitch)_WX_ +ObjectSwitch :=-o +ArchiveOutputSwitch := +PreprocessOnlySwitch :=-E +ObjectsFileList :="D:\Projects\Radio\fdmdv2\tests\serial_enum\enumSerial.txt" +PCHCompileFlags := +MakeDirCommand :=makedir +RcCmpOptions := $(shell wx-config --rcflags) +RcCompilerName :=windres +LinkOptions := $(shell wx-config --debug=yes --libs --unicode=yes) +IncludePath := $(IncludeSwitch). $(IncludeSwitch)/bin/MinGW-4.6.1/msys/1.0/local/include +IncludePCH := +RcIncludePath := +Libs := +ArLibs := +LibPath := $(LibraryPathSwitch). + +## +## Common variables +## AR, CXX, CC, CXXFLAGS and CFLAGS can be overriden using an environment variables +## +AR := ar rcus +CXX := g++ +CC := gcc +CXXFLAGS := -g -O0 -Wall $(shell wx-config --cxxflags --unicode=yes --debug=yes) -DSVN_REVISION=\"1111\" $(Preprocessors) +CFLAGS := -g -O0 -Wall $(shell wx-config --cxxflags --unicode=yes --debug=yes) -DSVN_REVISION=\"1111\" $(Preprocessors) + + +## +## User defined environment variables +## +CodeLiteDir:=D:\bin\CodeLite +WXWIN:=D:\bin\wxWidgets-2.9.4 +PATH:=$(WXWIN)\lib\gcc_dll;$(PATH) +WXCFG:=gcc_dll\mswu +UNIT_TEST_PP_SRC_DIR:=D:\bin\UnitTest++-1.3 +Objects=$(IntermediateDirectory)/main$(ObjectSuffix) + +## +## Main Build Targets +## +.PHONY: all clean PreBuild PrePreBuild PostBuild +all: $(OutputFile) + +$(OutputFile): $(IntermediateDirectory)/.d $(Objects) + @$(MakeDirCommand) $(@D) + @echo "" > $(IntermediateDirectory)/.d + @echo $(Objects) > $(ObjectsFileList) + $(LinkerName) $(OutputSwitch)$(OutputFile) @$(ObjectsFileList) $(LibPath) $(Libs) $(LinkOptions) + +$(IntermediateDirectory)/.d: + @$(MakeDirCommand) "./Debug" + +PreBuild: + + +## +## Objects +## +$(IntermediateDirectory)/main$(ObjectSuffix): main.cpp $(IntermediateDirectory)/main$(DependSuffix) + $(CXX) $(IncludePCH) $(SourceSwitch) "D:/Projects/Radio/fdmdv2/tests/serial_enum/main.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/main$(ObjectSuffix) $(IncludePath) +$(IntermediateDirectory)/main$(DependSuffix): main.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/main$(ObjectSuffix) -MF$(IntermediateDirectory)/main$(DependSuffix) -MM "D:/Projects/Radio/fdmdv2/tests/serial_enum/main.cpp" + +$(IntermediateDirectory)/main$(PreprocessSuffix): main.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/main$(PreprocessSuffix) "D:/Projects/Radio/fdmdv2/tests/serial_enum/main.cpp" + + +-include $(IntermediateDirectory)/*$(DependSuffix) +## +## Clean +## +clean: + $(RM) $(IntermediateDirectory)/main$(ObjectSuffix) + $(RM) $(IntermediateDirectory)/main$(DependSuffix) + $(RM) $(IntermediateDirectory)/main$(PreprocessSuffix) + $(RM) $(OutputFile) + $(RM) $(OutputFile).exe + $(RM) "D:\Projects\Radio\fdmdv2\build\.build-debug\enumSerial" + + diff --git a/fdmdv2/tests/serial_enum/enumSerial.project b/fdmdv2/tests/serial_enum/enumSerial.project new file mode 100644 index 00000000..99160d90 --- /dev/null +++ b/fdmdv2/tests/serial_enum/enumSerial.project @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + None + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + None + + + + + + + + + + + + + + + diff --git a/fdmdv2/tests/serial_enum/enumSerial.txt b/fdmdv2/tests/serial_enum/enumSerial.txt new file mode 100644 index 00000000..e9f35e35 --- /dev/null +++ b/fdmdv2/tests/serial_enum/enumSerial.txt @@ -0,0 +1 @@ +./Debug/main.o diff --git a/fdmdv2/tests/serial_enum/enumser.h b/fdmdv2/tests/serial_enum/enumser.h new file mode 100644 index 00000000..92b4ad9e --- /dev/null +++ b/fdmdv2/tests/serial_enum/enumser.h @@ -0,0 +1,257 @@ +/* +Module : enumser.h +Purpose: Defines the interface for a class to enumerate the serial ports installed on a PC + using a number of different approaches +Created: PJN / 03-11-1998 + +Copyright (c) 1998 - 2012 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com) + +All rights reserved. + +Copyright / Usage Details: + +You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) +when your product is released in binary form. You are allowed to modify the source code in any way you want +except you cannot modify the copyright details at the top of each module. If you want to distribute source +code with your application, then you are only allowed to distribute versions released by the author. This is +to maintain a single distribution point for the source code. + +*/ + + +///////////////////////// Macros / Structs etc //////////////////////////////// + +#pragma once + +#ifndef __ENUMSER_H__ +#define __ENUMSER_H__ + +#ifndef CENUMERATESERIAL_EXT_CLASS +#define CENUMERATESERIAL_EXT_CLASS +#endif + + +///////////////////////// Includes //////////////////////////////////////////// + +#if defined CENUMERATESERIAL_USE_STL +#ifndef _VECTOR_ + #include + #pragma message("To avoid this message, please put vector in your pre compiled header (normally stdafx.h)") +#endif +#ifndef _STRING_ + #include + #pragma message("To avoid this message, please put string in your pre compiled header (normally stdafx.h)") +#endif +#endif + +#if defined _AFX + #ifndef __AFXTEMPL_H__ + #include + #pragma message("To avoid this message, please put afxtempl.h in your pre compiled header (normally stdafx.h)") + #endif +#else + #ifndef __ATLSTR_H__ + #include + #pragma message("To avoid this message, please put atlstr.h in your pre compiled header (normally stdafx.h)") + #endif +#endif + + +///////////////////////// Classes ///////////////////////////////////////////// + +class CENUMERATESERIAL_EXT_CLASS CEnumerateSerial +{ +public: +//Methods +#ifndef NO_ENUMSERIAL_USING_CREATEFILE + #if defined CENUMERATESERIAL_USE_STL + static BOOL UsingCreateFile(std::vector& ports); + #elif defined _AFX + static BOOL UsingCreateFile(CUIntArray& ports); + #else + static BOOL UsingCreateFile(CSimpleArray& ports); + #endif +#endif + +#ifndef NO_ENUMSERIAL_USING_QUERYDOSDEVICE + #if defined CENUMERATESERIAL_USE_STL + static BOOL UsingQueryDosDevice(std::vector& ports); + #elif defined _AFX + static BOOL UsingQueryDosDevice(CUIntArray& ports); + #else + static BOOL UsingQueryDosDevice(CSimpleArray& ports); + #endif +#endif + +#ifndef NO_ENUMSERIAL_USING_GETDEFAULTCOMMCONFIG + #if defined CENUMERATESERIAL_USE_STL + static BOOL UsingGetDefaultCommConfig(std::vector& ports); + #elif defined _AFX + static BOOL UsingGetDefaultCommConfig(CUIntArray& ports); + #else + static BOOL UsingGetDefaultCommConfig(CSimpleArray& ports); + #endif +#endif + +#ifndef NO_ENUMSERIAL_USING_SETUPAPI1 + #if defined CENUMERATESERIAL_USE_STL + #if defined _UNICODE + static BOOL UsingSetupAPI1(std::vector& ports, std::vector& friendlyNames); + #else + static BOOL UsingSetupAPI1(std::vector& ports, std::vector& friendlyNames); + #endif + #elif defined _AFX + static BOOL UsingSetupAPI1(CUIntArray& ports, CStringArray& friendlyNames); + #else + static BOOL UsingSetupAPI1(CSimpleArray& ports, CSimpleArray& friendlyNames); + #endif +#endif + +#ifndef NO_ENUMSERIAL_USING_SETUPAPI2 + #if defined CENUMERATESERIAL_USE_STL + #if defined _UNICODE + static BOOL UsingSetupAPI2(std::vector& ports, std::vector& friendlyNames); + #else + static BOOL UsingSetupAPI2(std::vector& ports, std::vector& friendlyNames); + #endif + #elif defined _AFX + static BOOL UsingSetupAPI2(CUIntArray& ports, CStringArray& friendlyNames); + #else + static BOOL UsingSetupAPI2(CSimpleArray& ports, CSimpleArray& friendlyNames); + #endif +#endif + +#ifndef NO_ENUMSERIAL_USING_ENUMPORTS + #if defined CENUMERATESERIAL_USE_STL + static BOOL UsingEnumPorts(std::vector& ports); + #elif defined _AFX + static BOOL UsingEnumPorts(CUIntArray& ports); + #else + static BOOL UsingEnumPorts(CSimpleArray& ports); + #endif +#endif + +#ifndef NO_ENUMSERIAL_USING_WMI + #if defined CENUMERATESERIAL_USE_STL + #if defined _UNICODE + static BOOL UsingWMI(std::vector& ports, std::vector& friendlyNames); + #else + static BOOL UsingWMI(std::vector& ports, std::vector& friendlyNames); + #endif + #elif defined _AFX + static BOOL UsingWMI(CUIntArray& ports, CStringArray& friendlyNames); + #else + static BOOL UsingWMI(CSimpleArray& ports, CSimpleArray& friendlyNames); + #endif +#endif + +#ifndef NO_ENUMSERIAL_USING_COMDB + #if defined CENUMERATESERIAL_USE_STL + static BOOL UsingComDB(std::vector& ports); + #elif defined _AFX + static BOOL UsingComDB(CUIntArray& ports); + #else + static BOOL UsingComDB(CSimpleArray& ports); + #endif +#endif + +#ifndef NO_ENUMSERIAL_USING_REGISTRY + #if defined CENUMERATESERIAL_USE_STL + #if defined _UNICODE + static BOOL UsingRegistry(std::vector& ports); + #else + static BOOL UsingRegistry(std::vector& ports); + #endif + #elif defined _AFX + static BOOL UsingRegistry(CStringArray& ports); + #else + static BOOL UsingRegistry(CSimpleArray& ports); + #endif +#endif + +protected: + +//Helper class which supports auto closing of a handle via CloseHandle + class CAutoHandle + { + public: + //Constructors / Destructors + CAutoHandle() : m_hHandle(INVALID_HANDLE_VALUE) + { + } + + explicit CAutoHandle(HANDLE hHandle) : m_hHandle(hHandle) + { + } + + ~CAutoHandle() + { + if (m_hHandle != INVALID_HANDLE_VALUE) + { + CloseHandle(m_hHandle); + m_hHandle = INVALID_HANDLE_VALUE; + } + } + + //Methods + operator HANDLE() + { + return m_hHandle; + } + + //Member variables + HANDLE m_hHandle; + }; + + +//Helper class which supports auto closing of a HMODULE via FreeLibrary and setting of the last Win32 error via SetLastError + class CAutoHModule + { + public: + //Constructors / Destructors + CAutoHModule() : m_hModule(NULL), + m_dwError(ERROR_SUCCESS) + { + } + + explicit CAutoHModule(HMODULE hModule) : m_hModule(hModule), + m_dwError(GetLastError()) + { + } + + explicit CAutoHModule(HMODULE hModule, DWORD dwError) : m_hModule(hModule), + m_dwError(dwError) + { + } + + ~CAutoHModule() + { + if (m_hModule != NULL) + { + FreeLibrary(m_hModule); + m_hModule = NULL; + } + SetLastError(m_dwError); + } + + operator HMODULE() + { + return m_hModule; + } + + //Member variables + HMODULE m_hModule; + DWORD m_dwError; + }; + + + +//Methods + static BOOL RegQueryValueString(HKEY kKey, LPCTSTR lpValueName, LPTSTR& pszValue); + static BOOL QueryRegistryPortName(HKEY hDeviceKey, int& nPort); + static HMODULE LoadLibraryFromSystem32(LPCTSTR lpFileName); + static BOOL IsNumeric(LPCSTR pszString, BOOL bIgnoreColon); + static BOOL IsNumeric(LPCWSTR pszString, BOOL bIgnoreColon); +}; + +#endif //__ENUMSER_H__ diff --git a/fdmdv2/tests/serial_enum/main.cpp b/fdmdv2/tests/serial_enum/main.cpp new file mode 100644 index 00000000..b45d5ed1 --- /dev/null +++ b/fdmdv2/tests/serial_enum/main.cpp @@ -0,0 +1,206 @@ +#include +#include +#include +#include +//#include +//#include +#include + + +static bool getReg(); +//static bool UsingRegistryWxArrayString(wxArrayString ports); +//static bool UsingRegistry(std::vector& ports); +//static bool RegQueryValueString(HKEY kKey, LPCTSTR lpValueName, LPTSTR& pszValue); +//static bool QueryRegistryPortName(HKEY hDeviceKey, int& nPort); +//static HMODULE LoadLibraryFromSystem32(LPCTSTR lpFileName); +//static bool IsNumeric(LPCSTR pszString, bool bIgnoreColon); +//static bool IsNumeric(LPCWSTR pszString, bool bIgnoreColon); + +//std::vector friendlyNames; +int main( int argc, char** argv ) +{ + // initialize wxWidgets + wxInitializer init; + wxPrintf(wxT("Enumerating Serial Comm Ports\n\n")); + //std::vector sPorts; + wxArrayString sPorts; + if(getReg()) + { + wxPrintf("\nOk\n"); + } +/* + if(UsingRegistryWxArrayString(sPorts)) + { + for(int i = 0; i < sPorts.size(); i++) + { + wxPrintf("%s\n", sPorts[i].c_str()); + } + } +*/ + return 0; +} + +#ifdef __WXMSW__ + +bool getReg() +{ + //wxRegKey *key = new wxRegKey("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM"); + wxRegKey key(wxRegKey::HKLM, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM")); + if(!key.Exists()) + { + wxPrintf("Key doesn't exist: %s\n", key.GetName()); + return false; + } + else + { + // Get the number of subkeys and enumerate them. + if(!key.Open(wxRegKey::Read)) + { + wxPrintf("Unable to open key: %s\n", key.GetName()); + return false; + } + size_t subkeys; + size_t values; + if(!key.GetKeyInfo(&subkeys, NULL, &values, NULL)) + { + wxPrintf("Unable get subkey info: %s\n", key.GetName()); + return false; + } + if(!key.HasValues()) + { + wxPrintf("Key does have values: %s\n", key.GetName()); + return false; + } + wxString key_name; + long el = 1; + key.GetFirstValue(key_name, el); + wxString valType; + wxString key_data; + for(unsigned int i = 0; i < values; i++) + { + key.QueryValue(key_name, key_data); + wxPrintf("Value: %s Data: %s\n", key_name, key_data); + //wxPrintf("Data: %s\n", key_data); + key.GetNextValue(key_name, el); + } + } + return true; +} + +bool UsingRegistry(std::vector& ports) +{ + //Make sure we clear out any elements which may already be in the array(s) + ports.clear(); + //What will be the return value from this function (assume the worst) + BOOL bSuccess = FALSE; + + HKEY hSERIALCOMM; + if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hSERIALCOMM) == ERROR_SUCCESS) + { + //Get the max value name and max value lengths + DWORD dwMaxValueNameLen; + DWORD dwMaxValueLen; + DWORD dwQueryInfo = ::RegQueryInfoKey(hSERIALCOMM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dwMaxValueNameLen, &dwMaxValueLen, NULL, NULL); + if(dwQueryInfo == ERROR_SUCCESS) + { + DWORD dwMaxValueNameSizeInChars = dwMaxValueNameLen + 1; //Include space for the NULL terminator + DWORD dwMaxValueNameSizeInBytes = dwMaxValueNameSizeInChars * sizeof(TCHAR); + DWORD dwMaxValueDataSizeInChars = dwMaxValueLen/sizeof(TCHAR) + 1; //Include space for the NULL terminator + DWORD dwMaxValueDataSizeInBytes = dwMaxValueDataSizeInChars * sizeof(TCHAR); + wxWCharBuffer *szValueName = new wxWCharBuffer(dwMaxValueNameSizeInBytes); + wxWCharBuffer *byValue = new wxWCharBuffer(dwMaxValueDataSizeInBytes); + bSuccess = TRUE; + + //Enumerate all the values underneath HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM + DWORD dwIndex = 0; + DWORD dwType; + DWORD dwValueNameSize = dwMaxValueNameSizeInChars; + DWORD dwDataSize = dwMaxValueDataSizeInBytes; + LONG nEnum = ::RegEnumValue(hSERIALCOMM, dwIndex, (wchar_t *) szValueName, &dwValueNameSize, NULL, &dwType, (LPBYTE) byValue, &dwDataSize); + + while(nEnum == ERROR_SUCCESS) + { + //If the value is of the correct type, then add it to the array + if(dwType == REG_SZ) + { + //TCHAR* szPort = reinterpret_cast(byValue.m_pData); + TCHAR *szPort; + szPort = (TCHAR *)byValue; + ports.push_back(szPort); + } + //Prepare for the next time around + dwValueNameSize = dwMaxValueNameSizeInChars; + dwDataSize = dwMaxValueDataSizeInBytes; + ++dwIndex; + nEnum = ::RegEnumValue(hSERIALCOMM, dwIndex, (wchar_t *) szValueName, &dwValueNameSize, NULL, &dwType, (LPBYTE) byValue, &dwDataSize); + } + } + //Close the registry key now that we are finished with it + ::RegCloseKey(hSERIALCOMM); + if(dwQueryInfo != ERROR_SUCCESS) + { + ::SetLastError(dwQueryInfo); + } + } + return bSuccess; +} + +bool UsingRegistryWxArrayString(wxArrayString ports) +{ + //Make sure we clear out any elements which may already be in the array(s) + ports.clear(); + //What will be the return value from this function (assume the worst) + BOOL bSuccess = FALSE; + + HKEY hSERIALCOMM; + if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hSERIALCOMM) == ERROR_SUCCESS) + { + //Get the max value name and max value lengths + DWORD dwMaxValueNameLen; + DWORD dwMaxValueLen; + DWORD dwQueryInfo = ::RegQueryInfoKey(hSERIALCOMM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dwMaxValueNameLen, &dwMaxValueLen, NULL, NULL); + if(dwQueryInfo == ERROR_SUCCESS) + { + DWORD dwMaxNameSizeInChars = dwMaxValueNameLen + 1; //Include space for the NULL terminator + DWORD dwMaxNameSizeInBytes = dwMaxNameSizeInChars * sizeof(TCHAR); + DWORD dwMaxDataSizeInChars = dwMaxValueLen/sizeof(TCHAR) + 1; //Include space for the NULL terminator + DWORD dwMaxDataSizeInBytes = dwMaxDataSizeInChars * sizeof(TCHAR); + wxWCharBuffer *szValueName = new wxWCharBuffer(dwMaxNameSizeInBytes); + wxWCharBuffer *byValue = new wxWCharBuffer(dwMaxDataSizeInBytes); + + bSuccess = TRUE; + + //Enumerate all the values underneath HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM + DWORD dwIndex = 0; + DWORD dwType; + DWORD dwValueNameSize = dwMaxNameSizeInChars; + DWORD dwDataSize = dwMaxDataSizeInBytes; + LONG nEnum = ::RegEnumValue(hSERIALCOMM, dwIndex, (wchar_t *)&szValueName, &dwValueNameSize, NULL, &dwType, (LPBYTE) byValue, &dwDataSize); + wxString szPort; + + while(nEnum == ERROR_SUCCESS) + { + //If the value is of the correct type, then add it to the array + if(dwType == REG_SZ) + { + szPort = wxString((LPBYTE)byValue); + ports.push_back(szPort); + } + //Prepare for the next time around + dwValueNameSize = dwMaxNameSizeInChars; + dwDataSize = dwMaxDataSizeInBytes; + ++dwIndex; + nEnum = ::RegEnumValue(hSERIALCOMM, dwIndex, (wchar_t *) &szValueName, &dwValueNameSize, NULL, &dwType, (LPBYTE) byValue, &dwDataSize); + } + } + //Close the registry key now that we are finished with it + ::RegCloseKey(hSERIALCOMM); + if (dwQueryInfo != ERROR_SUCCESS) + { + ::SetLastError(dwQueryInfo); + } + } + return bSuccess; +} + +#endif //__WXMSW__