calling unmanaged Windows APi

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
 
J

Jeff Johnson

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.
 
T

Tom Shelton

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);
}
 
T

Tony Johansson

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
 
J

Jeff Johnson

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.
 
P

Peter Duniho

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
 
T

Tony Johansson

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
 
P

Peter Duniho

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_.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top