Merge pull request #156 from mscdex/fix-vs-express

windows: fix compilation with VS Express
This commit is contained in:
Chris Williams 2013-04-07 12:14:20 -07:00
commit 1738281adf
8 changed files with 4662 additions and 124 deletions

View file

@ -1,11 +1,10 @@
{
"targets": [
'targets': [
{
"target_name": "serialport",
"sources": [
"src/serialport.cpp",
"src/serialport_unix.cpp",
"src/serialport.h"
'target_name': 'serialport',
'sources': [
'src/serialport.cpp',
'src/serialport_unix.cpp',
],
'conditions': [
['OS=="win"',
@ -13,21 +12,18 @@
'sources': [
"src/serialport_win.cpp",
'src/win/disphelper.c',
'src/win/disphelper.h',
'src/win/enumser.cpp',
'src/win/enumser.h',
'src/win/stdafx.h'
]
],
}
],
['OS!="win"',
{
'sources': [
"src/serialport_unix.cpp"
]
'src/serialport_unix.cpp',
],
}
]
]
}
]
],
],
},
],
}

72
src/win/AutoHModule.h Normal file
View file

@ -0,0 +1,72 @@
/*
Module : AutoHModule.h
Purpose: Defines the interface for a class which supports auto closing of a HMODULE via FreeLibrary and
setting of the last Win32 error via SetLastError
Created: PJN / 10-01-2013
Copyright (c) 2013 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 __AUTOHMODULE_H__
#define __AUTOHMODULE_H__
///////////////////////// Classes /////////////////////////////////////////////
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;
};
#endif //__AUTOHMODULE_H__

62
src/win/AutoHandle.h Normal file
View file

@ -0,0 +1,62 @@
/*
Module : AutoHandle.h
Purpose: Defines the interface for a class which supports auto closing of a handle via CloseHandle
Created: PJN / 10-01-2013
Copyright (c) 2013 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 __AUTOHANDLE_H__
#define __AUTOHANDLE_H__
///////////////////////// Classes /////////////////////////////////////////////
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;
};
#endif //__AUTOHANDLE_H__

72
src/win/AutoHeapAlloc.h Normal file
View file

@ -0,0 +1,72 @@
/*
Module : AutoHeapAlloc.h
Purpose: Defines the interface for a class which supports auto closing of a heap pointer allocated via HeapAlloc
Created: PJN / 10-01-2013
Copyright (c) 2013 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 __AUTOHEAPALLOC_H__
#define __AUTOHEAPALLOC_H__
///////////////////////// Includes ////////////////////////////////////////////
#include <assert.h>
///////////////////////// Classes /////////////////////////////////////////////
class CAutoHeapAlloc
{
public:
//Constructors / Destructors
CAutoHeapAlloc(HANDLE hHeap = GetProcessHeap(), DWORD dwHeapFreeFlags = 0) : m_pData(NULL),
m_hHeap(hHeap),
m_dwHeapFreeFlags(dwHeapFreeFlags)
{
}
BOOL Allocate(SIZE_T dwBytes, DWORD dwFlags = 0)
{
//Validate our parameters
assert(m_pData == NULL);
m_pData = HeapAlloc(m_hHeap, dwFlags, dwBytes);
return (m_pData != NULL);
}
~CAutoHeapAlloc()
{
if (m_pData != NULL)
{
HeapFree(m_hHeap, m_dwHeapFreeFlags, m_pData);
m_pData = NULL;
}
}
//Methods
//Member variables
LPVOID m_pData;
HANDLE m_hHeap;
DWORD m_dwHeapFreeFlags;
};
#endif //__AUTOHEAPALLOC_H__

View file

@ -114,8 +114,16 @@ History: PJN / 23-02-1999 Code now uses QueryDosDevice if running on NT to deter
from raw Win32 API call RegQueryValueEx is null terminated before it is treated as such
in the code. Thanks to Jeffrey Walton for reporting this security issue.
5. Updated the code to clean compile on VC 2012
PJN / 10-01-2013 1. Updated copyright details
2. Spun off CAutoHModule class into its own header file
3. Spun off CAutoHandle class into its own header file
4. Addition of a new CAutoHeapAlloc class which encapsulates HeapAlloc / HeapFree calls
in a C++ class.
5. Removed ATL usage completely from UsingQueryDevice, UsingSetupAPI2 and UsingEnumPorts.
This should allow these methods to support compilers which do not have support for ATL such
as VC Express SKUs.
Copyright (c) 1998 - 2012 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)
Copyright (c) 1998 - 2013 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)
All rights reserved.
@ -134,6 +142,9 @@ to maintain a single distribution point for the source code.
#include "stdafx.h"
#include "enumser.h"
#include "AutoHModule.h"
#include "AutoHandle.h"
#include "AutoHeapAlloc.h"
#ifndef NO_ENUMSERIAL_USING_WMI
#ifndef __ATLBASE_H__
@ -435,10 +446,11 @@ BOOL CEnumerateSerial::UsingQueryDosDevice(CSimpleArray<UINT>& ports)
BOOL bWantStop = FALSE;
while (nChars && !bWantStop)
{
ATL::CHeapPtr<TCHAR> szDevices;
if (szDevices.Allocate(nChars))
CAutoHeapAlloc devices;
if (devices.Allocate(nChars * sizeof(TCHAR)))
{
DWORD dwChars = QueryDosDevice(NULL, szDevices, nChars);
LPTSTR pszDevices = static_cast<LPTSTR>(devices.m_pData);
DWORD dwChars = QueryDosDevice(NULL, pszDevices, nChars);
if (dwChars == 0)
{
DWORD dwError = GetLastError();
@ -455,10 +467,11 @@ BOOL CEnumerateSerial::UsingQueryDosDevice(CSimpleArray<UINT>& ports)
bSuccess = TRUE;
bWantStop = TRUE;
size_t i=0;
while (szDevices[i] != _T('\0'))
while (pszDevices[i] != _T('\0'))
{
//Get the current device name
TCHAR* pszCurrentDevice = &(szDevices[i]);
TCHAR* pszCurrentDevice = &(pszDevices[i]);
//If it looks like "COMX" then
//add it to the array which will be returned
@ -727,8 +740,8 @@ BOOL CEnumerateSerial::UsingSetupAPI2(CSimpleArray<UINT>& ports, CSimpleArray<CS
}
//Allocate the needed memory
ATL::CHeapPtr<GUID> pGuids;
if (!pGuids.Allocate(dwGuids))
CAutoHeapAlloc guids;
if (!guids.Allocate(dwGuids * sizeof(GUID)))
{
//Set the error to report
setupAPI.m_dwError = ERROR_OUTOFMEMORY;
@ -737,6 +750,7 @@ BOOL CEnumerateSerial::UsingSetupAPI2(CSimpleArray<UINT>& ports, CSimpleArray<CS
}
//Call the function again
GUID* pGuids = static_cast<GUID*>(guids.m_pData);
if (!lpfnSETUPDICLASSGUIDSFROMNAME(_T("Ports"), pGuids, dwGuids, &dwGuids))
{
//Set the error to report
@ -850,13 +864,14 @@ BOOL CEnumerateSerial::UsingEnumPorts(CSimpleArray<UINT>& ports)
BOOL bSuccess = FALSE;
//Allocate the buffer and recall
ATL::CHeapPtr<BYTE> pPorts;
if (pPorts.Allocate(cbNeeded))
CAutoHeapAlloc portsBuffer;
if (portsBuffer.Allocate(cbNeeded))
{
BYTE* pPorts = static_cast<BYTE*>(portsBuffer.m_pData);
bSuccess = EnumPorts(NULL, 1, pPorts, cbNeeded, &cbNeeded, &dwPorts);
if (bSuccess)
{
PORT_INFO_1* pPortInfo = reinterpret_cast<PORT_INFO_1*>(pPorts.m_pData);
PORT_INFO_1* pPortInfo = reinterpret_cast<PORT_INFO_1*>(pPorts);
for (DWORD i=0; i<dwPorts; i++)
{
//If it looks like "COMX" then
@ -1032,16 +1047,18 @@ BOOL CEnumerateSerial::UsingComDB(CSimpleArray<UINT>& ports)
if (dwPortUsage == ERROR_SUCCESS)
{
//Allocate some heap space and recall the function
ATL::CHeapPtr<BYTE> portBytes;
CAutoHeapAlloc portBytes;
if (portBytes.Allocate(dwMaxPortsReported))
{
bSuccess = TRUE;
if (lpfnCOMDBGETCURRENTPORTUSAGE(hComDB, portBytes, dwMaxPortsReported, CDB_REPORT_BYTES, &dwMaxPortsReported) == ERROR_SUCCESS)
PBYTE pPortBytes = static_cast<PBYTE>(portBytes.m_pData);
if (lpfnCOMDBGETCURRENTPORTUSAGE(hComDB, pPortBytes, dwMaxPortsReported, CDB_REPORT_BYTES, &dwMaxPortsReported) == ERROR_SUCCESS)
{
//Work thro the byte bit array for ports which are in use
for (DWORD i=0; i<dwMaxPortsReported; i++)
{
if (portBytes[i])
if (pPortBytes[i])
{
#if defined CENUMERATESERIAL_USE_STL
ports.push_back(i + 1);
@ -1109,9 +1126,9 @@ BOOL CEnumerateSerial::UsingRegistry(CSimpleArray<CString>& ports)
DWORD dwMaxValueDataSizeInBytes = dwMaxValueDataSizeInChars * sizeof(TCHAR);
//Allocate some space for the value name and value data
ATL::CHeapPtr<TCHAR> szValueName;
ATL::CHeapPtr<BYTE> byValue;
if (szValueName.Allocate(dwMaxValueNameSizeInChars) && byValue.Allocate(dwMaxValueDataSizeInBytes))
CAutoHeapAlloc valueName;
CAutoHeapAlloc valueData;
if (valueName.Allocate(dwMaxValueNameSizeInBytes) && valueData.Allocate(dwMaxValueDataSizeInBytes))
{
bSuccess = TRUE;
@ -1120,15 +1137,17 @@ BOOL CEnumerateSerial::UsingRegistry(CSimpleArray<CString>& ports)
DWORD dwType;
DWORD dwValueNameSize = dwMaxValueNameSizeInChars;
DWORD dwDataSize = dwMaxValueDataSizeInBytes;
memset(szValueName.m_pData, 0, dwMaxValueNameSizeInBytes);
memset(byValue.m_pData, 0, dwMaxValueDataSizeInBytes);
memset(valueName.m_pData, 0, dwMaxValueNameSizeInBytes);
memset(valueData.m_pData, 0, dwMaxValueDataSizeInBytes);
TCHAR* szValueName = static_cast<TCHAR*>(valueName.m_pData);
BYTE* byValue = static_cast<BYTE*>(valueData.m_pData);
LONG nEnum = RegEnumValue(hSERIALCOMM, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, 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<TCHAR*>(byValue.m_pData);
TCHAR* szPort = reinterpret_cast<TCHAR*>(byValue);
#if defined CENUMERATESERIAL_USE_STL
ports.push_back(szPort);
#else
@ -1139,8 +1158,8 @@ BOOL CEnumerateSerial::UsingRegistry(CSimpleArray<CString>& ports)
//Prepare for the next time around
dwValueNameSize = dwMaxValueNameSizeInChars;
dwDataSize = dwMaxValueDataSizeInBytes;
memset(szValueName.m_pData, 0, dwMaxValueNameSizeInBytes);
memset(byValue.m_pData, 0, dwMaxValueDataSizeInBytes);
memset(valueName.m_pData, 0, dwMaxValueNameSizeInBytes);
memset(valueData.m_pData, 0, dwMaxValueDataSizeInBytes);
++dwIndex;
nEnum = RegEnumValue(hSERIALCOMM, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, byValue, &dwDataSize);
}

View file

@ -4,7 +4,7 @@ Purpose: Defines the interface for a class to enumerate the serial ports install
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)
Copyright (c) 1998 - 2013 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)
All rights reserved.
@ -42,8 +42,7 @@ to maintain a single distribution point for the source code.
#include <string>
#pragma message("To avoid this message, please put string in your pre compiled header (normally stdafx.h)")
#endif
#endif
#else
#if defined _AFX
#ifndef __AFXTEMPL_H__
#include <afxtempl.h>
@ -55,6 +54,7 @@ to maintain a single distribution point for the source code.
#pragma message("To avoid this message, please put atlstr.h in your pre compiled header (normally stdafx.h)")
#endif
#endif
#endif
///////////////////////// Classes /////////////////////////////////////////////
@ -170,82 +170,6 @@ public:
#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);

View file

@ -1,10 +1,50 @@
#pragma once
#include <atlstr.h>
#include <setupapi.h>
#include <WBemCli.h>
#include <comdef.h>
#include <vector>
#include <string>
#define CENUMERATESERIAL_USE_STL //Uncomment this line if you want to test the STL support in CEnumerateSerial
#define CENUMERATESERIAL_USE_STL
#ifndef _SECURE_ATL
#define _SECURE_ATL 1 //Use the Secure C Runtime in ATL
#endif
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN
#endif
#ifndef WINVER
#define WINVER 0x0500
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0500
#endif
#ifndef _WIN32_IE
#define _WIN32_IE 0x0500
#endif
#ifndef CENUMERATESERIAL_USE_STL
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#define _AFX_ALL_WARNINGS // turns off MFC's hiding of some common and often safely ignored warning messages
#include <afxext.h>
#include <afxtempl.h>
#include <atlbase.h>
#else
#include <vector>
#include <string>
#include "stdstring.h"
#define CString CStdString
#define NO_ENUMSERIAL_USING_WMI
#endif
#include <setupapi.h>
#include <malloc.h>
#include <winspool.h>
#include <Wbemcli.h>
#include <comdef.h>
#include <stdio.h>

4353
src/win/stdstring.h Normal file

File diff suppressed because it is too large Load diff