GetLastWin32Error questions

  • Thread starter Thread starter active
  • Start date Start date
A

active

GetLastWin32Error exposes the Win32 GetLastError API method from
Kernel32.DLL. This method exists because it is not safe to make a direct
platform invoke call to GetLastError to obtain this information. If you want
to access this error code, you must call GetLastWin32Error rather than
writing your own platform invoke definition for GetLastError and calling it.
The common language runtime can make internal calls to APIs that overwrite
the operating system maintained GetLastError.

You can only use this method to obtain error codes if you apply the
System.Runtime.InteropServices.DllImportAttribute to the method signature
and set the SetLastError field to true. The process for this varies
depending upon the source language used: C# and C++ are false by default,
but the Declare statement in Visual Basic is true. For additional
information about the GetLastError and SetLastError Win32 API methods, see
the MSDN Library.



What does the last paragraph mean?

Especially the "...only...if you apply the ..." part

What Declare are they taking about?



I'd like to messagebox only if there is an error.

How can I test for success/failure?





Thanks a lot
 
What does the last paragraph mean?
Especially the "...only...if you apply the ..." part

What Declare are they taking about?
System.Runtime.InteropServices.DllImportAttribute to the method signature
and set the SetLastError field to true.

Below is an example of what the last paragraph is talking about:

<DllImport("ole32.dll", EntryPoint:="CLSIDFromString", _
SetLastError:=True, CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function CLSIDFromString( _
<MarshalAs(UnmanagedType.LPWStr)> ByVal pwcsName As String, _
ByRef pclsid As Guid) As Integer
End Function
 
active said:
GetLastWin32Error exposes the Win32 GetLastError API method from
Kernel32.DLL. This method exists because it is not safe to make a
direct platform invoke call to GetLastError to obtain this
information. If you want to access this error code, you must call
GetLastWin32Error rather than writing your own platform invoke
definition for GetLastError and calling it. The common language
runtime can make internal calls to APIs that overwrite the operating
system maintained GetLastError.

You can only use this method to obtain error codes if you apply the
System.Runtime.InteropServices.DllImportAttribute to the method
signature and set the SetLastError field to true. The process for
this varies depending upon the source language used: C# and C++ are
false by default, but the Declare statement in Visual Basic is true.
For additional
information about the GetLastError and SetLastError Win32 API
methods, see the MSDN Library.



What does the last paragraph mean?

Especially the "...only...if you apply the ..." part

What Declare are they taking about?

Declare Sub bla Lib "kernel32.dll" ()


The paragraph says that calling GetLastWin32Error only makse sense if the
API function called is declared using the DllImportAttribute while
specifying SetLastError=True. It depends on the language how this can be
done. In VB, you have two ways to declare an external function:

- Using the DllImport attribute
- Using the Declare statement (as shown above)

It says that, using the Declare statment, the SetLastError field is set to
True by default, so you don't have to (or can't) explicitly set it to true.

I'd like to messagebox only if there is an error.

How can I test for success/failure?

If you used the Declare statement for declaring the function called, simply
call GetLastWin32Error afterwards. Nothing else has to be done.

If you used the DllImportAtribte for declaring the function, you must set
SetLastError = True in the declaration:

<DllImportAttribute("kernel32.dll", setlasterror:=True)> _
Shared Sub bla()
End Sub


Armin
 
I think I've got it.
Since I'm using Declares I need not do it.
But if I do a DllImport in C# I must do it if I want to use
GetLastWin32Error after calling that sub.
Right?

But what about sending to a messagebox only if there is an error.

How can I test for success/failure?

Is not zero always an error and zero return success?


As always, thanks for the help!
 
Since I'm using Declares I need not do it.
But if I do a DllImport in C# I must do it if I want to use
GetLastWin32Error after calling that sub.
Right?
Yes.

But what about sending to a messagebox only if there is an error.

How can I test for success/failure?

The functions themselves return success or failure.
GetLastWin32Error allows you to create a human readable message that
represents the exact cause of the error.
FormatMessage is available for this purpose. Use it with p-invoke.
Is not zero always an error and zero return success?

No. The MSDN documentation states explicitly what signifies success or
failure for each function.
 
How can I test for success/failure?

Read the method's documentation - it should talk about the return
values there. The easiest way is to do a search for the function's
name on msdn.com or possibly pinvoke.net.

Thanks,

Seth Rowe
 
active said:
But what about sending to a messagebox only if there is an error.

How can I test for success/failure?

Is not zero always an error and zero return success?

You can read it in the documentation of the called API function.

Often you can read:
"If the function fails, the return value is 0 (zero). To get extended error
information, call GetLastError."


Armin
 
The functions themselves return success or failure.

I was thinking that if I called GetLastWin32Error when there was no error
the return might indicate that fact.

thanks
 
I was thinking that if I called GetLastWin32Error when there was no error
the return might indicate that fact.

thanks
 
Declare Sub bla Lib "kernel32.dll" ()



Declare Sub bla Lib "kernel32.dll" ()

I was trying to turn the "SetLastError field to true" into a statement about
GetLastWin32Error. Not too bright!


How about your example above.
"kernel32.dll" is just an example?
GetLastWin32Error applies to other dll's, right?


Thanks
 
Armin Zingler said:
You can read it in the documentation of the called API function.

Often you can read:
"If the function fails, the return value is 0 (zero). To get extended
error information, call GetLastError."


Armin

I was thinking that if I called GetLastWin32Error when there was no error
the return might indicate that fact.

thanks

thanks
 
active said:
I was trying to turn the "SetLastError field to true" into a
statement about GetLastWin32Error. Not too bright!

Sorry, I don't understand.
How about your example above.
"kernel32.dll" is just an example?
GetLastWin32Error applies to other dll's, right?

Right.


Armin
 
active said:
How about your example above.
"kernel32.dll" is just an example?
GetLastWin32Error applies to other dll's, right?

Yes, to all DLLs setting Win32 error codes, such as the system DLLs
"kernel32.dll", "gdi32.dll", ...
 
Back
Top