source: trunk/Utilities/ReadHardwareIdentifiers/ReadHardwareIdentifiers.cpp @ 937

Last change on this file since 937 was 94, checked in by roman, 12 years ago
  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2012
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: ReadHardwareIdentifiers.cpp 94 2012-08-17 11:21:09Z roman $
6
7#include "stdafx.h"
8
9////////////////////////////////////////////////////////////
10// CModule
11
12class CModule :
13        public CAtlExeModuleT<CModule>
14{
15public:
16
17public:
18
19public:
20// CModule
21        CModule() throw()
22        {
23                AtlTraceSetDefaultSettings();
24                _Z4(atlTraceRefcount, 4, _T("this 0x%p"), this);
25                _W(CExceptionFilter::Initialize());
26        }
27        ~CModule() throw()
28        {
29                _Z4(atlTraceRefcount, 4, _T("this 0x%p"), this);
30                CExceptionFilter::Terminate();
31        }
32        HRESULT PreMessageLoop(INT nShowCommand)
33        {
34                const HRESULT nResult = __super::PreMessageLoop(nShowCommand);
35                return SUCCEEDED(nResult) ? S_OK : nResult;
36        }
37        static BOOL IsFourCharacterCode(DWORD nCode, CStringA* psString = NULL, BOOL bReverse = TRUE)
38        {
39                const BYTE* pnCodeData = (const BYTE*) &nCode;
40                for(SIZE_T nIndex = 0; nIndex < 4; nIndex++)
41                        if(pnCodeData[nIndex] < 0x20 || pnCodeData[nIndex] >= 0x80)
42                                return FALSE;
43                if(psString)
44                {
45                        if(bReverse)
46                        {
47                                CHAR pszCodeString[5] = { pnCodeData[3], pnCodeData[2], pnCodeData[1], pnCodeData[0], 0 };
48                                *psString = pszCodeString;
49                        } else
50                        {
51                                CHAR pszCodeString[5] = { pnCodeData[0], pnCodeData[1], pnCodeData[2], pnCodeData[3], 0 };
52                                *psString = pszCodeString;
53                        }
54                }
55                return TRUE;
56        }
57        static CStringA FormatCode(DWORD nCode, BOOL bReverse = TRUE)
58        {
59                CStringA sCodeString;
60                if(IsFourCharacterCode(nCode, &sCodeString, bReverse))
61                        return sCodeString;
62                return AtlFormatStringT<CStringA>("0x%08x", nCode);
63        }
64        VOID RunMessageLoop()
65        {
66                // NOTE: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724259%28v=vs.85%29.aspx
67                static const DWORD g_pnProviderSignatures[] =
68                {
69                        'ACPI', // The ACPI firmware table provider.
70                        'FIRM', // The raw firmware table provider.
71                        'RSMB', // The raw SMBIOS firmware table provider.
72                };
73                for(SIZE_T nProviderSignatureIndex = 0; nProviderSignatureIndex < DIM(g_pnProviderSignatures); nProviderSignatureIndex++)
74                {
75                        const DWORD nProviderSignature = g_pnProviderSignatures[nProviderSignatureIndex];
76                        _ATLTRY
77                        {
78                                DWORD nDataSize = EnumSystemFirmwareTables(nProviderSignature, NULL, 0);
79                                CTempBufferT<BYTE> pnData(nDataSize);
80                                __E(EnumSystemFirmwareTables(nProviderSignature, pnData, nDataSize));
81                                _tprintf(_T("Obtained %d bytes of firmware identifier table %hs\n"), nDataSize, FormatCode(nProviderSignature));
82                                const SIZE_T nIdentifierCount = nDataSize / sizeof (DWORD);
83                                for(SIZE_T nIdentifierIndex = 0; nIdentifierIndex < nIdentifierCount; nIdentifierIndex++)
84                                {
85                                        const DWORD nIdentifier = ((const DWORD*) (const BYTE*) pnData)[nIdentifierIndex];
86                                        DWORD nDataSize = GetSystemFirmwareTable(nProviderSignature, nIdentifier, NULL, 0);
87                                        CTempBufferT<BYTE> pnData(nDataSize);
88                                        __E(GetSystemFirmwareTable(nProviderSignature, nIdentifier, pnData, nDataSize));
89                                        _tprintf(_T("\t") _T("Obtained %d bytes of firmware table %hs\n"), nDataSize, FormatCode(nIdentifier, FALSE));
90                                        switch(nProviderSignature)
91                                        {
92                                        //case 'APIC':
93                                        //      switch(nIdentifier)
94                                        //      {
95                                        //      case 'PCAF': // ”FACP” Fixed ACPI Description Table
96                                        //              {
97                                        //                      typedef struct FACP
98                                        //                      {
99                                        //                              UINT32 nSignature;
100                                        //                              UINT32 nLength;
101                                        //                              UINT8 nRevision;
102                                        //                              UINT8 nChecksum;
103                                        //                              BYTE pnOemIdentifier[6];
104                                        //                              BYTE pnOemTableIdentifier[8];
105                                        //                              UINT32 nOemRevision;
106                                        //                              UINT32 nCreatorIdentifier;
107                                        //                              UINT32 nCreatorRevision;
108                                        //                              // TODO: ...
109                                        //                      };
110                                        //              }
111                                        //              break;
112                                        //      }
113                                        //      break;
114                                        case 'RSMB':
115                                                {
116                                                        typedef struct _SMBIOSDATA
117                                                        {
118                                                                BYTE Used20CallingMethod;
119                                                                BYTE SMBIOSMajorVersion;
120                                                                BYTE SMBIOSMinorVersion;
121                                                                BYTE DmiRevision;
122                                                                DWORD Length;
123                                                                BYTE SMBIOSTableData[1];
124                                                        } SMBIOSDATA;
125                                                        const SMBIOSDATA* pSmBiosData = (const SMBIOSDATA*) (const BYTE*) pnData;
126                                                        _tprintf(_T("\t") _T("\t") _T("Version: %d.%d\n"), pSmBiosData->SMBIOSMajorVersion, pSmBiosData->SMBIOSMinorVersion);
127                                                        const BYTE* pnItemBoundary = pSmBiosData->SMBIOSTableData + pSmBiosData->Length;
128                                                        const BYTE* pnItem = pSmBiosData->SMBIOSTableData;
129                                                        for(; pnItem < pnItemBoundary; pnItem++)
130                                                        {
131                                                                const BYTE nType = pnItem[0];
132                                                                _tprintf(_T("\t") _T("\t") _T("\t") _T("%d, %d bytes of formatted area\n"), nType, pnItem[1]);
133                                                                const CHAR* pszTitle = (const CHAR*) pnItem + pnItem[1];
134                                                                if((const BYTE*) pszTitle >= pnItemBoundary)
135                                                                        break;
136                                                                for(; *pszTitle; )
137                                                                {
138                                                                        _tprintf(_T("\t") _T("\t") _T("\t") _T("\t") _T("%hs\n"), pszTitle);
139                                                                        pszTitle += strlen(pszTitle) + 1;
140                                                                }
141                                                                pnItem = (const BYTE*) pszTitle;
142                                                        }
143                                                }                                       
144                                                break;
145                                        }
146                                }
147                        }
148                        _ATLCATCHALL()
149                        {
150                                _tprintf(_T("Failed to obtain firmware table %hs\n"), FormatCode(nProviderSignature));
151                        }
152                }
153        }
154};
155
156////////////////////////////////////////////////////////////
157// CArgumentException
158
159class CArgumentException :
160        public CAtlException
161{
162public:
163        CString m_sMessage;
164
165public:
166// CArgumentException
167        CArgumentException(const CString& sMessage, HRESULT nCode = E_FAIL) throw() :
168                CAtlException(nCode),
169                m_sMessage(sMessage)
170        {
171        }
172};
173
174////////////////////////////////////////////////////////////
175// Main
176
177int _tmain(int argc, _TCHAR* argv[])
178{
179        CModule Module;
180        _ATLTRY
181        {
182                #pragma region Syntax
183                //if(argc <= 1)
184                //{
185                //      CPath sName = FindFileName(argv[0]);
186                //      sName.RemoveExtension();
187                //      _tprintf(_T("Syntax:  %s [options] <media-file-path>\n"), sName);
188                //      _tprintf(_T("\n"));
189                //      _tprintf(_T("Options: ") _T("/x") _T(" - ") _T("show message box before running the graph\n"));
190                //      _tprintf(_T("Options: ") _T("/p") _T(" - ") _T("use default audio renderer\n"));
191                //      _tprintf(_T("\n"));
192                //      return 2;
193                //}
194                #pragma endregion
195                //for(INT nArgumentIndex = 1; nArgumentIndex < argc; nArgumentIndex++)
196                //{
197                //      CString sArgument = argv[nArgumentIndex];
198                //      _A(!sArgument.IsEmpty());
199                //      if(_tcschr(_T("-/"), sArgument[0]))
200                //      {
201                //              sArgument.Delete(0, 1);
202                //              if(sArgument.IsEmpty())
203                //                      throw CArgumentException(_T("Unexpected argument"));
204                //              switch(sArgument[0])
205                //              {
206                //              case 'X':
207                //              case 'x':
208                //                      Module.m_bShowMessageBox = TRUE;
209                //                      break;
210                //              case 'P':
211                //              case 'p':
212                //                      Module.m_bUseAudioRenderer = TRUE;
213                //                      break;
214                //              case 'A':
215                //              case 'a':
216                //                      Module.m_bUseParallelProcessing = TRUE;
217                //                      break;
218                //              default:
219                //                      throw CArgumentException(AtlFormatString(_T("Unexpected argument (%s)"), sArgument));
220                //              }
221                //              continue;
222                //      }
223                //      if(_tcslen(Module.m_sPath))
224                //              throw CArgumentException(_T("Unexpected argument: Input file name already specified"));
225                //      Module.m_sPath = (LPCTSTR) sArgument;
226                //      //throw CArgumentException(AtlFormatString(_T("Unexpected argument (%s)"), sArgument));
227                //}
228                //if(!_tcslen(Module.m_sPath))
229                //      throw CArgumentException(_T("Missing argument: No input file name specified"));
230                Module.WinMain(SW_SHOWNORMAL);
231        }
232        catch(CArgumentException Exception)
233        {
234                _tprintf(_T("Fatal Error: %s\n"), Exception.m_sMessage);
235                return 1;
236        }
237        _ATLCATCH(Exception)
238        {
239                _tprintf(_T("Fatal Error: 0x%08x %s\n"), (HRESULT) Exception, AtlFormatSystemMessage(Exception).TrimRight(_T("\t\n\r .")));
240                return 1;
241        }
242        _ATLCATCHALL()
243        {
244                _tprintf(_T("Fatal Error\n"));
245                return 1;
246        }
247        return 0;
248}
Note: See TracBrowser for help on using the repository browser.