CHECKERROR macro for file amd line info in Exceptions

svn path=/trunk/; revision=6755
This commit is contained in:
Martin Fuchs 2003-11-23 03:06:01 +00:00
parent 915d0c21ff
commit 17b9c8d455
8 changed files with 145 additions and 75 deletions

View file

@ -3,7 +3,7 @@
<tr>
<td><address style="align: right;"><small>
ROS Explore Source Code Documentation
<br>generated on 15.11.2003 by <a href="http://www.doxygen.org/index.html">
<br>generated on 23.11.2003 by <a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0>
</small></address>
</td>

View file

@ -70,7 +70,7 @@ ResString::ResString(UINT nid)
int len = LoadString(g_Globals._hInstance, nid, buffer, sizeof(buffer)/sizeof(TCHAR));
assign(buffer, len);
super::assign(buffer, len);
}

View file

@ -340,6 +340,8 @@ LRESULT MainFrame::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
int MainFrame::Command(int id, int code)
{
CONTEXT("MainFrame::Command()");
#ifndef _NO_MDI
HWND hwndClient = (HWND) SendMessage(_hmdiclient, WM_MDIGETACTIVE, 0, 0);
@ -426,6 +428,8 @@ int MainFrame::Command(int id, int code)
ExecuteDialog dlg = {{0}, 0};
if (DialogBoxParam(g_Globals._hInstance, MAKEINTRESOURCE(IDD_EXECUTE), _hwnd, ExecuteDialog::WndProc, (LPARAM)&dlg) == IDOK) {
CONTEXT("ShellExecute()");
HINSTANCE hinst = ShellExecute(_hwnd, NULL/*operation*/, dlg.cmd/*file*/, NULL/*parameters*/, NULL/*dir*/, dlg.cmdshow);
if ((int)hinst <= 32)

View file

@ -145,6 +145,8 @@ void ShellDirectory::get_path(PTSTR path) const
BOOL ShellEntry::launch_entry(HWND hwnd, UINT nCmdShow)
{
CONTEXT("ShellEntry::launch_entry()");
SHELLEXECUTEINFO shexinfo;
shexinfo.cbSize = sizeof(SHELLEXECUTEINFO);

View file

@ -70,22 +70,26 @@ Context* Context::s_current = &Context::s_main;
void HandleException(COMException& e, HWND hwnd)
{
TCHAR msg[4*BUFFER_LEN];
LPTSTR p = msg;
p += _stprintf(TEXT("%s"), e.ErrorMessage());
if (e._ctx)
p += _stprintf(p, TEXT("\nContext: %s"), e._ctx);
if (!e._obj.empty())
p += _stprintf(p, TEXT("\nObject: %s"), (LPCTSTR)e._obj);
if (e._file)
#ifdef UNICODE
p += _stprintf(p, TEXT("\nLocation: %hs(%d)"), e._file, e._line);
#else
p += _stprintf(p, TEXT("\nLocation: %s(%d)"), e._file, e._line);
#endif
SetLastError(0);
String msg = e.ErrorMessage();
if (e._ctx) {
TCHAR buffer[BUFFER_LEN];
_stprintf(buffer, TEXT("%s\nContext: %s"), (LPCTSTR)msg, e._ctx);
msg = buffer;
}
if (!e._obj.empty()) {
TCHAR buffer[BUFFER_LEN];
_stprintf(buffer, TEXT("%s\nObject: %s"), (LPCTSTR)msg, (LPCTSTR)e._obj);
msg = buffer;
}
MessageBox(hwnd, msg, TEXT("ShellClasses COM Exception"), MB_ICONHAND|MB_OK);
// If displaying the error message box _with_ parent was not successfull, display it now without a parent window.
@ -182,7 +186,7 @@ ShellFolder::ShellFolder()
IShellFolder* desktop;
CheckError(SHGetDesktopFolder(&desktop));
CHECKERROR(SHGetDesktopFolder(&desktop));
super::Attach(desktop);
desktop->AddRef();
@ -203,10 +207,10 @@ ShellFolder::ShellFolder(IShellFolder* parent, LPCITEMIDLIST pidl)
IShellFolder* ptr;
if (!pidl)
CheckError(E_INVALIDARG);
CHECKERROR(E_INVALIDARG);
if (pidl && pidl->mkid.cb)
CheckError(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&ptr));
CHECKERROR(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&ptr));
else
ptr = parent;
@ -222,7 +226,7 @@ ShellFolder::ShellFolder(LPCITEMIDLIST pidl)
IShellFolder* parent = Desktop();
if (pidl && pidl->mkid.cb)
CheckError(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&ptr));
CHECKERROR(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&ptr));
else
ptr = parent;
@ -237,7 +241,7 @@ void ShellFolder::attach(IShellFolder* parent, LPCITEMIDLIST pidl)
IShellFolder* ptr;
if (pidl && pidl->mkid.cb)
CheckError(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&ptr));
CHECKERROR(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&ptr));
else
ptr = parent;
@ -251,7 +255,7 @@ ShellFolder::ShellFolder()
{
CONTEXT("ShellFolder::ShellFolder()");
CheckError(SHGetDesktopFolder(&_p));
CHECKERROR(SHGetDesktopFolder(&_p));
_p->AddRef();
}
@ -269,7 +273,7 @@ ShellFolder::ShellFolder(IShellFolder* parent, LPCITEMIDLIST pidl)
CONTEXT("ShellFolder::ShellFolder(IShellFolder*, LPCITEMIDLIST)");
if (pidl && pidl->mkid.cb)
CheckError(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&_p));
CHECKERROR(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&_p));
else
_p = Desktop();
@ -281,7 +285,7 @@ ShellFolder::ShellFolder(LPCITEMIDLIST pidl)
CONTEXT("ShellFolder::ShellFolder(LPCITEMIDLIST)");
if (pidl && pidl->mkid.cb)
CheckError(Desktop()->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&_p));
CHECKERROR(Desktop()->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&_p));
else
_p = Desktop();
@ -294,7 +298,7 @@ void ShellFolder::attach(IShellFolder* parent, LPCITEMIDLIST pidl)
IShellFolder* h = _p;
CheckError(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&_p));
CHECKERROR(parent->BindToObject(pidl, 0, IID_IShellFolder, (LPVOID*)&_p));
_p->AddRef();
h->Release();
@ -314,7 +318,7 @@ String ShellFolder::get_name(LPCITEMIDLIST pidl, SHGDNF flags) const
if (hr == S_OK)
strret.GetString(pidl->mkid, buffer, MAX_PATH);
else {
CheckError(hr);
CHECKERROR(hr);
*buffer = TEXT('\0');
}
@ -356,9 +360,9 @@ void ShellPath::GetUIObjectOf(REFIID riid, LPVOID* ppvOut, HWND hWnd, ShellFolde
if (parent && parent->mkid.cb)
// use the IShellFolder of the parent
CheckError(ShellFolder((IShellFolder*)sf,parent)->GetUIObjectOf(hWnd, 1, &idl, riid, 0, ppvOut));
CHECKERROR(ShellFolder((IShellFolder*)sf,parent)->GetUIObjectOf(hWnd, 1, &idl, riid, 0, ppvOut));
else // else use desktop folder
CheckError(sf->GetUIObjectOf(hWnd, 1, &idl, riid, 0, ppvOut));
CHECKERROR(sf->GetUIObjectOf(hWnd, 1, &idl, riid, 0, ppvOut));
}
#ifndef __MINGW32__ // ILCombine() is currently missing in MinGW.

View file

@ -94,30 +94,6 @@ protected:
#endif
struct Context
{
Context(LPCTSTR ctx)
: _ctx(ctx)
{
_last = s_current;
s_current = this;
}
~Context()
{
s_current = _last;
}
LPCTSTR _ctx;
Context* _last;
static Context* s_current;
static Context s_main;
};
#define CONTEXT(x) Context __ctx__(TEXT(x))
/// COM Exception with context information
struct COMException : public COMExceptionBase
@ -125,30 +101,54 @@ struct COMException : public COMExceptionBase
typedef COMExceptionBase super;
COMException(HRESULT hr)
: super(hr)
: super(hr),
_ctx(Context::current()._ctx),
_obj(Context::current()._obj),
_file(NULL), _line(0)
{
}
COMException(HRESULT hr, const char* file, int line)
: super(hr),
_ctx(Context::current()._ctx),
_obj(Context::current()._obj),
_file(file), _line(line)
{
_ctx = Context::s_current->_ctx;
}
COMException(HRESULT hr, const String& obj)
: super(hr),
_obj(obj)
_ctx(Context::current()._ctx),
_obj(obj),
_file(NULL), _line(0)
{
_ctx = Context::current()._ctx;
}
COMException(HRESULT hr, const String& obj, const char* file, int line)
: super(hr),
_ctx(Context::current()._ctx),
_obj(obj),
_file(file), _line(line)
{
_ctx = Context::s_current->_ctx;
}
LPCTSTR _ctx;
String _obj;
const char* _file;
int _line;
};
#define CHECKERROR(hr) ((void)(FAILED(hr)? throw COMException(hr, __FILE__, __LINE__): 0))
#ifdef _NO_COMUTIL
inline void CheckError(HRESULT hr)
{
if (FAILED(hr)) {
if (FAILED(hr))
throw COMException(hr);
}
}
#endif
@ -160,13 +160,13 @@ struct ComInit
{
ComInit()
{
CheckError(CoInitialize(0));
CHECKERROR(CoInitialize(0));
}
#if (_WIN32_WINNT>=0x0400) || defined(_WIN32_DCOM)
ComInit(DWORD flag)
{
CheckError(CoInitializeEx(0, flag));
CHECKERROR(CoInitializeEx(0, flag));
}
#endif
@ -183,7 +183,7 @@ struct OleInit
{
OleInit()
{
CheckError(OleInitialize(0));
CHECKERROR(OleInitialize(0));
}
~OleInit()
@ -210,7 +210,7 @@ struct CommonShellMalloc
void init()
{
if (!_p)
CheckError(SHGetMalloc(&_p));
CHECKERROR(SHGetMalloc(&_p));
}
~CommonShellMalloc()
@ -542,18 +542,18 @@ struct ShellPath : public SShellPtr<ITEMIDLIST>
if (path) {
ULONG l;
CheckError(folder->ParseDisplayName(0, 0, (LPOLESTR)path, &l, &_p, 0));
CHECKERROR(folder->ParseDisplayName(0, 0, (LPOLESTR)path, &l, &_p, 0));
} else
_p = NULL;
}
ShellPath(LPCWSTR path)
{
CONTEXT("ShellPath::ShellPath(LPCWSTR)");
OBJ_CONTEXT("ShellPath::ShellPath(LPCWSTR)", path);
if (path) {
ULONG l;
CheckError(Desktop()->ParseDisplayName(0, 0, (LPOLESTR)path, &l, &_p, 0));
CHECKERROR(Desktop()->ParseDisplayName(0, 0, (LPOLESTR)path, &l, &_p, 0));
} else
_p = NULL;
}
@ -567,7 +567,7 @@ struct ShellPath : public SShellPtr<ITEMIDLIST>
if (path) {
MultiByteToWideChar(CP_ACP, 0, path, -1, b, MAX_PATH);
CheckError(folder->ParseDisplayName(0, 0, b, &l, &_p, 0));
CHECKERROR(folder->ParseDisplayName(0, 0, b, &l, &_p, 0));
} else
_p = NULL;
}
@ -581,7 +581,7 @@ struct ShellPath : public SShellPtr<ITEMIDLIST>
if (path) {
MultiByteToWideChar(CP_ACP, 0, path, -1, b, MAX_PATH);
CheckError(Desktop()->ParseDisplayName(0, 0, b, &l, &_p, 0));
CHECKERROR(Desktop()->ParseDisplayName(0, 0, b, &l, &_p, 0));
} else
_p = NULL;
}
@ -854,7 +854,7 @@ struct SpecialFolderPath : public ShellPath
SpecialFolderPath(int folder, HWND hwnd)
{
HRESULT hr = SHGetSpecialFolderLocation(hwnd, folder, &_p);
CheckError(hr);
CHECKERROR(hr);
}
};
@ -913,7 +913,7 @@ struct SpecialFolderFSPath : public FileSysShellPath
CONTEXT("SpecialFolderFSPath::SpecialFolderFSPath()");
HRESULT hr = SHGetSpecialFolderLocation(hwnd, folder, &_p);
CheckError(hr);
CHECKERROR(hr);
}
};
@ -928,6 +928,6 @@ struct ShellItemEnumerator : public SIfacePtr<IEnumIDList>
{
CONTEXT("ShellItemEnumerator::ShellItemEnumerator()");
CheckError(folder->EnumObjects(0, flags, &_p));
CHECKERROR(folder->EnumObjects(0, flags, &_p));
}
};

View file

@ -93,15 +93,15 @@ void MoveVisible(HWND hwnd)
}
void display_error(HWND hwnd, DWORD error)
void display_error(HWND hwnd, DWORD error) //@@ CONTEXT mit ausgeben
{
PTSTR msg;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
MessageBox(hwnd, msg, TEXT("Winefile"), MB_OK);
MessageBox(hwnd, msg, TEXT("ROS Explorer"), MB_OK);
else
MessageBox(hwnd, TEXT("Error"), TEXT("Winefile"), MB_OK);
MessageBox(hwnd, TEXT("Unknown Error"), TEXT("ROS Explorer"), MB_OK);
LocalFree(msg);
}
@ -129,6 +129,8 @@ BOOL time_to_filetime(const time_t* t, FILETIME* ftime)
BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow)
{
CONTEXT("launch_file()");
HINSTANCE hinst = ShellExecute(hwnd, NULL/*operation*/, cmd, NULL/*parameters*/, NULL/*dir*/, nCmdShow);
if ((int)hinst <= 32) {

View file

@ -493,8 +493,18 @@ struct String
String(const super& other) : super(other) {}
String(const String& other) : super(other) {}
String& operator=(LPCTSTR s) {assign(s); return *this;}
String& operator=(const super& s) {assign(s); return *this;}
#ifdef UNICODE
String(LPCSTR s) {assign(s);}
String& operator=(LPCSTR s) {assign(s); return *this;}
void assign(LPCSTR s) {TCHAR b[BUFFER_LEN]; MultiByteToWideChar(CP_ACP, 0, s, -1, b, BUFFER_LEN); super::assign(b);}
#else
String(LPCWSTR s) {assign(s);}
String& operator=(LPCWSTR s) {assign(s); return *this;}
void assign(LPCWSTR s) {char b[BUFFER_LEN]; WideCharToMultiByte(CP_ACP, 0, s, -1, b, BUFFER_LEN, 0, 0); super::assign(b);}
#endif
String& operator=(LPCTSTR s) {super::assign(s); return *this;}
String& operator=(const super& s) {super::assign(s); return *this;}
operator LPCTSTR() const {return c_str();}
};
@ -555,6 +565,54 @@ protected:
FCT _fct;
};
struct Context
{
Context(LPCTSTR ctx)
: _ctx(ctx)
{
_last = s_current;
s_current = this;
}
Context(LPCTSTR ctx, LPCSTR obj)
: _ctx(ctx),
_obj(obj)
{
_last = s_current;
s_current = this;
}
Context(LPCTSTR ctx, LPCWSTR obj)
: _ctx(ctx),
_obj(obj)
{
_last = s_current;
s_current = this;
}
~Context()
{
s_current = _last;
}
LPCTSTR _ctx;
String _obj;
static Context current() {return *s_current;}
protected:
Context* _last;
static Context* s_current;
static Context s_main;
};
#define CONTEXT_OBJ __ctx__._obj
#define CONTEXT(c) Context __ctx__(TEXT(c))
#define OBJ_CONTEXT(c, o) Context __ctx__(TEXT(c), o);
#endif // __cplusplus