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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenPrinter API

Status
Not open for further replies.

Deleco

Programmer
Feb 25, 2002
109
GB
Hi,

Has anyone used the OpenPrinter API on a network printer connected to a windows 2000 server.

I can get the API to work on a local printer and can access the information needed (page size of the printer driver)

But when i try to do the same thing on a network printer the OpenPrinter API call returns 0 (zero).

I am using windows XP in case that matters.

Can anyone help.

Deleco
 
In the PRINTER_DEFAULTS structure, what are you setting DesiredAccess to and is this compatible with your permissions to the printer?

Have you called GetLastError to retrieve the error? This is the function I use:
Code:
Public Declare Function GetLastError Lib "kernel32.dll" () As Long

Public Declare Function FormatMessage Lib "Kernel32" Alias "FormatMessageA" _
(ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long

Public Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Public Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Public Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Public Const FORMAT_MESSAGE_FROM_STRING = &H400
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Public Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
'___________________________________

Public Function fGetLastWinError(ByRef lngErrCode As Long, _
ByRef strErrMsg As String) As Boolean
    
    '--- Gets the error code & msg raised by a failed API call and places
    '     them in the passed in variables
    
    '--- Parameters
    '     [In/Out]
    '     lngErrCode: receives the Windows error number
    '     strErrMsg: receives the Windows error message
    
    '--- Return Value
    '     returns True if the error code was returned from Windows else False
    
    Dim plngRtn As Long             'Return value
    Dim plngBuffLen As Long       'Error msg buffer
    Dim pstrRtn As String           'Return value
    
    lngErrCode = 0
    strErrMsg = vbNullString
    'Get the last error code from Windows and store it
    plngRtn = GetLastError()
    If Not plngRtn = 0 Then
        lngErrCode = plngRtn
        'Get the error text from Windows
        pstrRtn = Space$(256)
        plngBuffLen = 256
        plngRtn = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULLPTR, _
        lngErrCode, NULLPTR, pstrRtn, plngBuffLen, NULLPTR)
        If Not plngRtn = 0 Then
            strErrMsg = Left$(pstrRtn, plngRtn)
        End If
        fGetLastWinError = True
    Else
        GoTo ExitFail
    End If
    
    Exit Function
    
ExitFail:
    lngErrCode = 0
    strErrMsg = vbNullString
    fGetLastWinError = False
    
End Function

Paul Bent
Northwind IT Systems
 
Hi Thanks for the Advice..

I had the desired access set to PRINTER_NORMAL_ACCESS i then changed this to PRINTER_ACCESS_USE and it appears to work fine.



Deleco
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top