Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printer Select -- Reply To Email 2

Status
Not open for further replies.

CassandraR

Programmer
Aug 10, 2001
346
CA
To Brian D., who emailed the following message to me:

Brian said:
Thanks for your helpful printing examples. I'm having trouble manipulating printer settings for a network printer. Are there any specific errors that will be generated by the API calls that I can use to determine if a printer property change call has failed? For privileges??

Thanks,
Brian

Brian:

Sorry that I won't be of much help. The "Printer Select" project (previously copied to the FAQs) is a project that I have not worked on in two years. Things have faded from memory in that time.

Another problem is that I did not specifically address the issues of dealing with networked printers.

I would suggest getting familiar with API functions such as GetLastError (see AllApi website entry as errors occurring within the printer API functions are likely to set the error entries. For example, looking at GetPrinter ( and SetPrinter ( API calls shows the following information:

Microsoft said:
Return Values

If the function succeeds, the return value is nonzero.[NL][NL]If the function fails, the return value is zero. To get extended error information, call GetLastError.

I believe that you will find the same for most, if not all, printer API calls. Check the examples on both the AllAPI and the MSDN (MS) websites.

Sorry that I cannot be of much help, but do hope you can find the answers easily and quickly.

By the way, I tried to reply via email but my email to you bounced back. Have you changed your email address, as listed on Tek Tips?

Cassie

Cassie
PIII-500Mhz-128MB
Win98SE-VB6SP5
 
I would like to mention a strange behaviour of GetLastError function. If you are accessing API calls from Visual Basic, then GetLastError function will never help you. No matter preceding API function call succeeds or fails, GetLastError, in VB, always returns 0.

This odd behaviour is due to the way VB handles API error reporting. In VB, you don't need to use the GetLastError function, VB has the Err.LastDllError property which reports the same thing, correctly. Moreover, VB has internal mechanism which makes it impossible to access the last error code using the GetLastError function.

Whenever an API function is called from your code, VB takes following steps:
1. Internally calls the SetLastError function to reset the last error value to 0.
2. Executes the API function.
3. Internally calls the GetLastError function to get the last error value and stores it in Err.LastDllError propery.

The reason for taking steps 1 and 3 is that there is only one, global last error code value for whole thread. The GetLastError function always returns the error value from the last API function call, and it is not necessary that last API call is made by your code. It could be any function call that could have been made by VB itself.

Most of the code we write in VB is actually translated into API function calls. For example the MsgBox function is translated in MessageBox API function. These API functions called internally by VB constantly overwrite the last error code. If the LastDllError property had not been implemented in this way, it would have been impossible to get the error value associated with your own API function call, using GetLastError function.

Therefore, steps 1 and 3 above ensure that the error code reported to you is specifically related to your own function call, no matter the global error code is overwritten by other API functions called by underlying VB code.

So when you call GetLastError function from VB, the error code is set to 0 in step 1. Thats why GetLastError function always returns 0.

See the following code which explains it more clearly.
___
[tt]
Option Explicit
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Sub Form_Load()
'using get last error
Debug.Print GetWindowTextLength(hwnd) 'good
Debug.Print GetLastError '=0=no error

Debug.Print GetWindowTextLength(1) 'bad (invalid handle)
Debug.Print GetLastError '=0=no error

'using Err.LastDllError
Debug.Print GetWindowTextLength(hwnd) 'good
Debug.Print Err.LastDllError '=0=no error

Debug.Print GetWindowTextLength(1) 'bad (invalid handle)
Debug.Print Err.LastDllError '=1400=ERROR_INVALID_WINDOW_HANDLE
End Sub[/tt]
___

See also thread222-475113 for a previous reference.
 
Man, how I like forums!

Thanks, Hypetia!!! [2thumbsup]

Cassie




Cassie
PIII-500Mhz-128MB
Win98SE-VB6SP5
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top