NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Retrieving Text from Win32 SDK's GetLastError() in C#

Thursday, May 21st, 2009

Using C# with the Win32 SDK is like taking something that is bad and making it horrible. That is not to say it isn't manageable and isn't very useful, but it is nonetheless frustrating at the lack of information when an error occurs.

I figured I would quickly post a snippet that you can use in C# to get an actual textual error back instead of just the Windows error code that is returned by GetLastError.

You can cast the error into a textual string by doing the following in C#:

string errorMsg = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message;

This will then return an actual string of the error that is occurring.

Just a quick note as well: These error messages are not always useful. Often times they are not very descriptive, but at least you don't have to reference the error code number against a long list to find out what is going on.

You could also do the same thing with PInvoke by calling the FormatMessage function, but I am lazy and keep as much in C# as I can.

PInvokin' Tom Out.

Tags

CSharp CPP PInvoke Win32

Related Blogs

Creating an Organizational Chart using C#: Part 1
Programatically Retrieving an Assembly's PublicKeyToken through a PowerShell CmdLet
Fixing "Error in loading DLL" in SharePoint
Retrieving data from SharePoint SOAP Requests using LINQ
Creating a dynamic SharePoint settings DropDown using a ToolPart

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations