Groups | Blog | Home
all groups > dotnet compact framework > september 2005 >

dotnet compact framework : FormatMessage in NET


tony
9/14/2005 1:04:52 PM
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.



Peter Foot [MVP]
9/14/2005 7:50:56 PM
Take a look at the code in the SDF for the OpenNETCF.Win32.WinAPIException
class this uses FormatMessage - www.opennetcf.org/sdf/ (see the links on
that page to the source code).

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://www.peterfoot.net

[quoted text, click to view]

roni
9/14/2005 9:16:04 PM
hi.

im try to write from csharp with pinvoke the > FormatMessage < api

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdk40/html/cerefformatmessage.asp

but im gettling lost there...

if someone have code sample of call the FormatMessage from charp/vb.net , i
will be very glad to learn from it.


have a nice day.

Peter Foot [MVP]
9/14/2005 10:58:56 PM
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
9/15/2005 12:36:54 AM
hi.
it worked.


you wrote :
"Then you
use PtrToStringUni (you could use the standard Marshal version too)"


i just want you to notice,that first i used yours PtrToStringUni , but
the method cut the message and display only part of it.

i replace with the standard Marshal, and its fine.



thanks for the help.

Peter Foot [MVP]
9/15/2005 9:33:35 AM
Looks like there is a bug in the opennetcf code - the value returned from
FormatMessage is the length in characters and PtrToStringUni requires a
length in bytes, so you'd need to multiply it by 2 for Unicode characters.

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://www.peterfoot.net

[quoted text, click to view]

AddThis Social Bookmark Button