calling unmanaged Windows APi

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

Does any of those Win32 API functions throw Exceptions that I can catch in
my managed code or do they use the GetLastError which cause that I have to
use the Marshal.GetLastWin32Error ?

//Tony
 
Does any of those Win32 API functions throw Exceptions that I can catch in
my managed code or do they use the GetLastError which cause that I have to
use the Marshal.GetLastWin32Error ?

Exceptions are managed, so no, unmanaged code will not throw exceptions. The
closest you'll get is COMException, and that comes from the managed wrapper
around the COM object.
 
Hi!

Does any of those Win32 API functions throw Exceptions that I can catch in
my managed code or do they use the GetLastError which cause that I have to
use the Marshal.GetLastWin32Error ?

//Tony

They will not directly throw an exception - but, I will often write code like
this:

int apiReturn = SomeApiFunction()
if (apiReturn == API_FAILED)
{
throw new System.ComponentModel.Win32Exception (Marshal.GetLastWin32Error);
}
 
Jeff Johnson said:
Exceptions are managed, so no, unmanaged code will not throw exceptions.
The closest you'll get is COMException, and that comes from the managed
wrapper around the COM object.

But you can have unmanaged code that throw exception for example C++ code.
So I disagree with you when you say that Exceptions are managed.
I have written a lot of C++ code and this code is unmanaged but I have used
exception handling for error.

//Tony
 
But you can have unmanaged code that throw exception for example C++ code.
So I disagree with you when you say that Exceptions are managed.
I have written a lot of C++ code and this code is unmanaged but I have
used exception handling for error.

Your C++ code might thrown an "exception" but it does not throw an
"Exception," i.e., a System.Exception.
 
Tony said:
Hi!

Does any of those Win32 API functions throw Exceptions that I can catch in
my managed code or do they use the GetLastError which cause that I have to
use the Marshal.GetLastWin32Error ?

No Win32 functions throw exceptions. For those functions that don't
return HRESULTs, you'll need to use GetLastWin32Error(). For those that
do return HRESULTs, you can use the PreserveSig field of the
DllImportAttribute, setting it to false to have the marshaler convert
HRESULT error codes to exceptions.

Pete
 
Peter Duniho said:
No Win32 functions throw exceptions. For those functions that don't
return HRESULTs, you'll need to use GetLastWin32Error(). For those that
do return HRESULTs, you can use the PreserveSig field of the
DllImportAttribute, setting it to false to have the marshaler convert
HRESULT error codes to exceptions.

Pete

You say
For those that do return HRESULTs, you can use the PreserveSig field of the
DllImportAttribute, setting it to false to have the marshaler convert
HRESULT error codes to exceptions.

Why is it not possible to use the the returned HRESULT ?

//Tony
 
Tony said:
[...]
You say
For those that do return HRESULTs, you can use the PreserveSig field of the
DllImportAttribute, setting it to false to have the marshaler convert
HRESULT error codes to exceptions.

Why is it not possible to use the the returned HRESULT ?

What makes you think it's not? I only said you _can_ set the
PreserveSig field to false, not that you _must_.
 
Back
Top