
Signature
ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
"Andreas Håkansson" <andreas@selfinflicted.org> wrote in message news:uEfLowhdDHA.416@tk2msftngp13.phx.gbl...
I was wondering if someone could verify that my declaration for the
FormatMessage API was correct. When I call it, I don't seem to be
getting any information stored in my buffer, i.e it's always empty.
[DllImport("Kernel32.dll")]
public static extern uint GetLastError();
[DllImport("Kernel32.dll")]
public static extern int FormatMessage
(
uint dwFlags, // source and processing options
IntPtr lpSource, // message source
uint dwMessageId, // message identifier
uint dwLanguageId, // language identifier
StringBuilder lpBuffer, // message buffer
uint nSize, // maximum size of message buffer
IntPtr Arguments // array of message inserts
);
This is how I would call it once I know an error has occured, i.e.
another API (declared with SetLastError=true) has failed.
StringBuilder buffer = new StringBuilder(2000);
int ret = Win32.FormatMessage
(
4096,
IntPtr.Zero,
Win32.GetLastError(),
0,
buffer,
(uint)buffer.Length,
IntPtr.Zero
);
and then display the buffer with buffer.ToString() ..
--
ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
Andreas,
>No one?
Sheesh, have some patience! :-)
> I was wondering if someone could verify that my declaration for the
> FormatMessage API was correct.
Looks correct to me, though I'd use CharSet.Auto.
> When I call it, I don't seem to be
> getting any information stored in my buffer, i.e it's always empty.
>
> [DllImport("Kernel32.dll")]
> public static extern uint GetLastError();
You can't reliably use GetLastError from managed code. User
Marshal.GetLastWin32Error instead.
> int ret = Win32.FormatMessage
> (
> 4096,
4096 eh? Do yourself a favor and use named constants. :-)
BTW, an easier way to retrieve the description of a system error is to
construct a Win32Exception (you don't have to throw it) and read its
Message property.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Andreas H?kansson - 08 Sep 2003 19:41 GMT
Andreas,
>No one?
>>Sheesh, have some patience! :-)
Hehe, time appears soo long when you sit at home and role your thumbs ;)
> I was wondering if someone could verify that my declaration for the
> FormatMessage API was correct.
>> Looks correct to me, though I'd use CharSet.Auto.
Roger that...
> When I call it, I don't seem to be
> getting any information stored in my buffer, i.e it's always empty.
>
> [DllImport("Kernel32.dll")]
> public static extern uint GetLastError();
>> You can't reliably use GetLastError from managed code. User
>> Marshal.GetLastWin32Error instead.
Roger that...
> int ret = Win32.FormatMessage
> (
> 4096,
>> 4096 eh? Do yourself a favor and use named constants. :-)
Hey! What can I say.. it was some dirty testcode to help debug some other
stuff (the GradientFill api) .. of course I use enums like an addict then
working
with Win32 interop ;)
>>BTW, an easier way to retrieve the description of a system error is to
>>construct a Win32Exception (you don't have to throw it) and read its
>>Message property.
Hmm I will read up on this.. sounds like something useful ;)
>> Mattias
Andreas