Well the key method is:-
internal static string GetErrorMessage(int ErrNo)
{
IntPtr pBuffer;
int nLen = Win32.Core.FormatMessage(Core.FormatMessageFlags.FromSystem |
Core.FormatMessageFlags.AllocateBuffer, 0, ErrNo, 0, out pBuffer, 0, null);
if ( nLen == 0 )//Failed
{
return string.Format("Error {0} (0x{0:X})", ErrNo);
}
string sMsg = MarshalEx.PtrToStringUni(pBuffer, 0, nLen);
MarshalEx.FreeHGlobal(pBuffer);
return sMsg;
}
You pass the FromSystem and AllocateBuffer flags and the function will
return a pointer in pBuffer which points to the allocated string. Then you
use PtrToStringUni (you could use the standard Marshal version too) to get a
managed string from that buffer. Then you free the buffer -
MarshalEx.FreeHGlobal wraps the LocalFree API.
Peter
--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com |
http://www.peterfoot.net [DllImport("coredll.dll", EntryPoint="FormatMessageW", SetLastError=false)]
internal static extern int FormatMessage(FormatMessageFlags dwFlags, int
lpSource, int dwMessageId, int dwLanguageId, out IntPtr lpBuffer, int nSize,
int[] Arguments );
[Flags]
public enum FormatMessageFlags : int
{
/// <summary>
/// The function allocates a buffer large enough to hold the formatted
message, and places a pointer to the allocated buffer at the address
specified by lpBuffer.
/// </summary>
AllocateBuffer = 0x00000100,
/// <summary>
/// Insert sequences in the message definition are to be ignored and
passed through to the output buffer unchanged.
/// </summary>
IgnoreInserts = 0x00000200,
/// <summary>
/// Specifies that lpSource is a pointer to a null-terminated message
definition.
/// </summary>
FromString = 0x00000400,
/// <summary>
/// Specifies that lpSource is a module handle containing the
message-table resource(s) to search.
/// </summary>
FromHModule = 0x00000800,
/// <summary>
/// Specifies that the function should search the system message-table
resource(s) for the requested message.
/// </summary>
FromSystem = 0x00001000,
/// <summary>
/// Specifies that the Arguments parameter is not a va_list structure,
but instead is just a pointer to an array of 32-bit values that represent
the arguments.
/// </summary>
ArgumentArray = 0x00002000,
/// <summary>
/// Use the <b>MaxWidthMask</b> constant and bitwise Boolean operations
to set and retrieve this maximum width value.
/// </summary>
MaxWidthMask = 0x000000FF,
}
[quoted text, click to view] "tony" <nospa@devdex.com> wrote in message
news:OgpfSeWuFHA.2540@TK2MSFTNGP09.phx.gbl...
> thanks for the link,but..
>
> too complicated to understand how to extract the FormatMessage.
>
> cause there is 3-2 classes involved there.
>
>
>
> if someone have somthing simple that just use the
> FormatMessage from csharp.. (in compact framework)..
>
> it will be nice to see.
>
> have a nice day.
>
>
>
> *** Sent via Developersdex
http://www.developersdex.com ***