AudRecordLib
AudRecordCLI/Error.h
Go to the documentation of this file.
00001 
00006 #ifndef AUDRECORDCLI_AUDRECORD_H
00007 #define AUDRECORDCLI_AUDRECORD_H
00008 
00009 #pragma once
00010 
00011 #include <boost/type_traits/is_same.hpp>
00012 #include <boost/type_traits/remove_pointer.hpp>
00013 
00014 namespace AudRecordCLI
00015 {
00020         public ref class AudRecordException : public Exception
00021         {
00022         public:
00023                 AudRecordException(String^ errorText, HRESULT hr);
00024         };
00025 }
00026 
00036 template<class Args>
00037 BOOL GetModuleMessage(HMODULE hMod, DWORD messageId, String^& errorText, Args* args)
00038 {
00039         DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER;
00040         if(!args)
00041         {
00042                 flags |= FORMAT_MESSAGE_IGNORE_INSERTS;
00043         }
00044         // this is a compile time check, its removed by optimization
00045         else if(boost::is_same<typename boost::remove_pointer<Args>::type, DWORD_PTR>::value)
00046         {
00047                 flags |= FORMAT_MESSAGE_ARGUMENT_ARRAY;
00048         }
00049         flags |= hMod ? FORMAT_MESSAGE_FROM_HMODULE : FORMAT_MESSAGE_FROM_SYSTEM;
00050         WCHAR* error;
00051         if(FormatMessageW(
00052                         flags,
00053                         hMod,
00054                         messageId,
00055                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00056                         reinterpret_cast<WCHAR*>(&error),
00057                         0, reinterpret_cast<va_list*>(args)
00058                 )
00059         )
00060         {
00061                 errorText = gcnew String(error);
00062                 LocalFree(error);
00063         }
00064         return !!error;
00065 }
00066 
00067 // these aer documented in the cpp file
00068 String^ GetModuleString(DWORD stringId);
00069 BOOL GetModuleMessage(HMODULE hMod, DWORD messageId, String^& errorText);
00070 void ThrowExceptionIfRequired(HRESULT hr);
00071 String^ FindErrorString(HRESULT hr);
00072 
00075 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Defines