From: Tony Johansson on
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


From: Jeff Johnson on
"Tony Johansson" <johansson.andersson(a)telia.com> wrote in message
news:OCIYRx75KHA.348(a)TK2MSFTNGP02.phx.gbl...

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


From: Tom Shelton on
On 2010-04-29, Tony Johansson <johansson.andersson(a)telia.com> wrote:
> 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);
}

--
Tom Shelton
From: Tony Johansson on

"Jeff Johnson" <i.get(a)enough.spam> skrev i meddelandet
news:%23i%23MZP85KHA.5016(a)TK2MSFTNGP02.phx.gbl...
> "Tony Johansson" <johansson.andersson(a)telia.com> wrote in message
> news:OCIYRx75KHA.348(a)TK2MSFTNGP02.phx.gbl...
>
>> 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.

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


From: Jeff Johnson on
"Tony Johansson" <johansson.andersson(a)telia.com> wrote in message
news:OZsq9785KHA.5808(a)TK2MSFTNGP02.phx.gbl...

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

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