473,472 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

GetLastWin32Error questions

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


Aug 8 '07 #1
10 2140
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" <ac**********@a-znet.comwrote in message
news:O2*************@TK2MSFTNGP05.phx.gbl...
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


Aug 8 '07 #2
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!


"Michael Phillips, Jr." <mp*********@nospam.jun0.c0mwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>What does the last paragraph mean?

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

What Declare are they taking about?
>System.Runtime.InteropServices.DllImportAttribu te 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" <ac**********@a-znet.comwrote in message
news:O2*************@TK2MSFTNGP05.phx.gbl...
>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.DllImportAttribu te 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



Aug 8 '07 #3
" active" <ac**********@a-znet.comschrieb
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

Aug 8 '07 #4
>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
Aug 8 '07 #5
>>
>You can only use this method to obtain error codes if you apply the
System.Runtime.InteropServices.DllImportAttribu te 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" ()

>>
What Declare are they taking about?

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
Aug 8 '07 #6
So it's not VB vs c#, it's vb's Declare vs vb or C# DllImportAttribute
thanks
>
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

Aug 8 '07 #7
" active" <ac**********@a-znet.comschrieb
>
What Declare are they taking about?
Declare Sub bla Lib "kernel32.dll" ()

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
Aug 8 '07 #8
" active" <ac**********@a-znet.comschrieb:
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", ...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 8 '07 #9
thanks

"Armin Zingler" <az*******@freenet.dewrote in message
news:u8**************@TK2MSFTNGP05.phx.gbl...
>" active" <ac**********@a-znet.comschrieb
>
What Declare are they taking about?

Declare Sub bla Lib "kernel32.dll" ()


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

Aug 8 '07 #10
thanks

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>" active" <ac**********@a-znet.comschrieb:
>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", ...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 8 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
1
by: [Joe] | last post by:
Is there any way to get a text description of Last Error ?
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
4
by: Larry Smith | last post by:
Hi there, Does anyone know if this (example) is safe: internal static extern int GetWindowRect(IntPtr hWnd, ref RECT rect); int rc = GetWindowRect(hWnd, ref rect); if (rc == 0)
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.