Login | Register
 
  Books Written by Brian Nantz  
  Links  
 
  P/Invoke GetLastError  
Location: BlogsRight To Left    
Posted by: host 8/10/2003

Luckily the WinIO Parallel Port API (http://www.internals.com/) didn’t really use pointers or structures and was easily wrapped in C#.  I also like the consistent interface that worked on everything from Win98 to WinXP!  Probably the handiest trick I came across in using P/Invoke was in retrieving Win32 errors.  Instead of trying to import GetLastError and FormatMessage (which requires the use of pointers and the unsafe compiler directive), there is a much simpler way.  Check out this one line treasure:

 

Win32Exception w32e = new Win32Exception(Marshal.GetLastWin32Error());

 

Then you can just throw another exception:

 

throw(new ApplicationException(w32e.Message));

 

and let the someone else handle it!

 

Permalink |  Trackback
  Right To Left  
  Right To Left  
You must be logged in and have permission to create or edit a blog.
  Right To Left  
  Right To Left  
Copyright 2005 by Nantz Consulting & Software LLC