I have a problem using a third-party component in Delphi 2006 (also Delphi 7), in which I get an "Unspecified Error" when executing a function call to that component. Do you have example code that utilises GetLastError and FormatMessage in Delphi, that would allow me to access more information about the error ? TIA :)
相关问题
- Is there a Delphi 5 component that can handle .png
- the application was unable to start correctly 0xc0
- Is there a way to install Delphi 2010 on Windows 2
- Is TWebBrowser dependant on IE version?
- Handle button click in another application
相关文章
- Why windows 64 still makes use of user32.dll etc?
- <link> onerror do not work in IE
- Best way to implement MVVM bindings (View <-> V
- Can WM_NEXTDLGCTL be used with non-dialog windows?
- Windows EventLog: How fast are operations with it?
- Could not find default endpoint element that refer
- How to force Delphi compiler to display all hints
- Coloring cell background on firemonkey stringgrid
While DR is correct, there is a problem with this approach: It does not allow you to specify the context in which the error occurred. Ever seen the error "An API function failed." whithout being any wiser which function it was and where it happended?
That's why I wrote the RaiseLastOsErrorEx and Win32CheckEx functions:
(They are part of unit u_dzMiscUtils of my dzLib library available here: https://sourceforge.net/p/dzlib/code/HEAD/tree/dzlib/trunk/src/u_dzOsUtils.pas
There is an integrated helper function in Delphi:
SysErrorMessage
. It's essentially a wrapper toFormatMessage
, but much simpler to use in your case. Just provide the error code you need a textual description for.For example you can use this to display the last error:
If you want to raise an exception with this message, it's even simpler:
Important: Make sure that there is no additional API call between the failing function and your call of
GetLastError
, otherwise the last error will be reset.