|
////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2006 Werner Willemsens
//
////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ComError
{
class ComError
{
[System.Runtime.InteropServices.DllImport("oleaut32.dll")]
private static extern uint GetErrorInfo(uint dwReserved, /* IErrorInfo** */ref IntPtr ppErrorInfo);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct ErrorInfo
{
// public uint m_cRefs;
public Guid m_guid;
public string m_bstrSource;
public string m_bstrDescription;
public string m_bstrHelpFile;
public uint m_dwHelpContext;
};
public static System.Runtime.InteropServices.COMException GetRichErrorInfo(System.Runtime.InteropServices.COMException error)
{
System.Runtime.InteropServices.COMException error2;
#if WindowsCE
if ((error.ErrorCode & 0x0000FFFF) > 0x01FF)
{
uint HRESULT;
ErrorInfo err = new ErrorInfo();
IntPtr ptr = new IntPtr(0);
HRESULT = GetErrorInfo(0, ref ptr);
if (HRESULT == 0)
{
// Guid
IntPtr pAddressGuid = new IntPtr((int)ptr.ToInt32() + 12);
byte[] bGuid = new byte[16];
Marshal.Copy(pAddressGuid, bGuid, 0, 16);
err.m_guid = new Guid(bGuid);
// HelpContext
IntPtr pAddressHelpContext = new IntPtr((int)ptr.ToInt32() + 40);
int[] iHelpContext = new int[1];
Marshal.Copy(pAddressHelpContext, iHelpContext, 0, 1);
err.m_dwHelpContext = (uint)iHelpContext[0];
// Description
IntPtr ppAddressDescription = new IntPtr((int)ptr.ToInt32() + 32);
int[] iAddressDescription = new int[1];
Marshal.Copy(ppAddressDescription, iAddressDescription, 0, 1);
if (iAddressDescription[0] != 0)
{
IntPtr pAddressDescriptionCount = new IntPtr((int)iAddressDescription[0] - 4);
int[] AddressDescriptionCount = new int[1];
Marshal.Copy(pAddressDescriptionCount, AddressDescriptionCount, 0, 1);
IntPtr pAddressDescription = new IntPtr((int)iAddressDescription[0]);
char[] aDescription = new char[AddressDescriptionCount[0]];
Marshal.Copy(pAddressDescription, aDescription, 0, AddressDescriptionCount[0] - 1);
err.m_bstrDescription = new string(aDescription);
}
else
err.m_bstrDescription = null;
// Source
IntPtr ppAddressSource = new IntPtr((int)ptr.ToInt32() + 28);
int[] iAddressSource = new int[1];
Marshal.Copy(ppAddressSource, iAddressSource, 0, 1);
if (iAddressSource[0] != 0)
{
IntPtr pAddressSourceCount = new IntPtr((int)iAddressSource[0] - 4);
int[] AddressSourceCount = new int[1];
Marshal.Copy(pAddressSourceCount, AddressSourceCount, 0, 1);
IntPtr pAddressSource = new IntPtr((int)iAddressSource[0]);
char[] aSource = new char[AddressSourceCount[0]];
Marshal.Copy(pAddressSource, aSource, 0, AddressSourceCount[0] - 1);
err.m_bstrSource = new string(aSource);
}
else
err.m_bstrSource = null;
// HelpFile
IntPtr ppAddressHelpFile = new IntPtr((int)ptr.ToInt32() + 36);
int[] iAddressHelpFile = new int[1];
Marshal.Copy(ppAddressHelpFile, iAddressHelpFile, 0, 1);
if (iAddressHelpFile[0] != 0)
{
IntPtr pAddressHelpFileCount = new IntPtr((int)iAddressHelpFile[0] - 4);
int[] AddressHelpFileCount = new int[1];
Marshal.Copy(pAddressHelpFileCount, AddressHelpFileCount, 0, 1);
IntPtr pAddressHelpFile = new IntPtr((int)iAddressHelpFile[0]);
char[] aHelpFile = new char[AddressHelpFileCount[0]];
Marshal.Copy(pAddressHelpFile, aHelpFile, 0, AddressHelpFileCount[0] - 1);
err.m_bstrHelpFile = new string(aHelpFile);
}
else
err.m_bstrHelpFile = null;
}
error2 = new System.Runtime.InteropServices.COMException(err.m_bstrDescription, error.ErrorCode);
}
else
#endif
{
error2 = error;
}
return error2;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.