Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2003

Tip: Looking for answers? Try searching our database.

FormatMessage

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andreas H?kansson - 08 Sep 2003 16:16 GMT
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() ..

Signature

ANDREAS H?KANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org

Andreas Håkansson - 08 Sep 2003 17:40 GMT
No one?

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

Mattias Sj?gren - 08 Sep 2003 19:22 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.