Home All Groups Group Topic Archive Search About
Author
16 May 2006 6:52 PM
mreisi
Hallo Leute,

ich habe vor kurzem schon einmal das Thema gestartet, wie man den
Standarddrucker unter .Net auslesen kann. Die API-Funktion
GetDefaultPrinter() ist mir dabei über den Weg gelaufen, leider habe
ich keine Ahnung, wie man sie richtig anwendet. Ich komme zwar
ursprünglich aus dem C bzw. C#--Bereich, jedoch lassen sich diese
Kenntnisse in .Net nicht so richtig verarbeiten. Folgende Fehlermeldung
tritt auf, wenn die Funktion versucht in die per Referenz übergebene
String-Variable zu schreiben. (Übrigens in VB.Net sowohl auch C#)

"Es wurde versucht, im geschützten Speicher zu lesen oder zu
schreiben. Dies ist häufig ein
Hinweis darauf, dass anderer Speicher beschädigt ist."

Es muss doch jemanden geben, der helfen kann. :-)

Vielen Dank im Voraus.

Author
16 May 2006 10:04 PM
Herfried K. Wagner [MVP]
"mreisi" <i***@mathiasreis.de> schrieb:
>ich habe vor kurzem schon einmal das Thema gestartet, wie man den
>Standarddrucker unter .Net auslesen kann. Die API-Funktion
>GetDefaultPrinter() ist mir dabei über den Weg gelaufen, leider habe
>ich keine Ahnung, wie man sie richtig anwendet.

Simple test call without error handling:

\\\
Private Declare Auto Function GetDefaultPrinter Lib "winspool.drv" ( _
    ByVal pszBuffer As String, _
    ByRef pcchBuffer As Int32 _
) As Boolean
....
Dim s As String = Space(128)
Dim n As Int32 = s.Length
If GetDefaultPrinter(s, n) Then
    MsgBox(Strings.Left(s, n - 1))
Else
    MsgBox("An error occured!")
End If
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 May 2006 8:33 AM
Cor Ligthert [MVP]
Herfried,

What was the problem?

Cor

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:Oulv9STeGHA.5016@TK2MSFTNGP04.phx.gbl...
> "mreisi" <i***@mathiasreis.de> schrieb:
>>ich habe vor kurzem schon einmal das Thema gestartet, wie man den
>>Standarddrucker unter .Net auslesen kann. Die API-Funktion
>>GetDefaultPrinter() ist mir dabei über den Weg gelaufen, leider habe
>>ich keine Ahnung, wie man sie richtig anwendet.
>
> Simple test call without error handling:
>
> \\\
> Private Declare Auto Function GetDefaultPrinter Lib "winspool.drv" ( _
>    ByVal pszBuffer As String, _
>    ByRef pcchBuffer As Int32 _
> ) As Boolean
> ...
> Dim s As String = Space(128)
> Dim n As Int32 = s.Length
> If GetDefaultPrinter(s, n) Then
>    MsgBox(Strings.Left(s, n - 1))
> Else
>    MsgBox("An error occured!")
> End If
> ///
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
>
Author
17 May 2006 8:36 AM
Cerebrus
A Language problem, he he !  Ich verstehen deutsch nicht gut...
Author
17 May 2006 8:45 AM
Cor Ligthert [MVP]
Cerebrus,

>A Language problem, he he !  Ich verstehen deutsch nicht gut...

I do, but that is no reason to answer it in that way without pointing on the
fact that not everybody does.

(For you misunderstand, I am sure that Herfried was thinking that he was
answering from a German language newsgroup)

:-)

Cor
Author
17 May 2006 1:51 PM
Herfried K. Wagner [MVP]
Cor,

"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
> What was the problem?

The OP's question was how to use the 'GetDefaultPrinter' API function and/or
determine the name of the default printer.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 May 2006 3:15 PM
Cor Ligthert [MVP]
Herfried,

I can easily read the German language, where I have always the problem with
those Data eieren. But it is not common to use that language here.

If you answer a question from an Op would you than not nicely write in the
message if the Op could use the next time not the  German language before
you answer or even give a short translation of the text?

The Op does not know that, you do.

:-)

Cor


Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:O28wMkbeGHA.764@TK2MSFTNGP05.phx.gbl...
> Cor,
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
>> What was the problem?
>
> The OP's question was how to use the 'GetDefaultPrinter' API function
> and/or determine the name of the default printer.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
17 May 2006 4:19 PM
Herfried K. Wagner [MVP]
Addendum:

Extended version (supposed to work on Windows 2000/XP and later):

\\\

' <URL:http://dotnet.mvps.org/meta/terms/License.txt>
Public Class PrinterManager
    Private Declare Auto Function GetDefaultPrinter Lib "winspool.drv" ( _
        ByVal pszBuffer As String, _
        ByRef pcchBuffer As Int32 _
    ) As Boolean

    Private Const ERROR_FILE_NOT_FOUND As Int32 = 2
    Private Const ERROR_INSUFFICIENT_BUFFER As Int32 = 122

    Private Declare Auto Function SetDefaultPrinter_API _
        Lib "winspool.drv" _
        Alias "SetDefaultPrinter" _
    ( _
        ByVal pszPrinter As String _
    ) As Boolean

    Public Shared Sub SetDefaultPrinter(ByVal PrinterName As String)
        If Not SetDefaultPrinter_API(PrinterName) Then
            Throw New Win32Exception
        End If
    End Sub

    Public Shared Function GetDefaultPrinter() As String
        Dim s As String = Space(128)
        Dim n As Int32 = s.Length
        Dim Success As Boolean = GetDefaultPrinter(s, n)
        If Success Then
            Return Strings.Left(s, n - 1)
        Else
            Dim LastError As Integer = Marshal.GetLastWin32Error()
            If LastError = ERROR_FILE_NOT_FOUND Then
                Throw _
                    New Win32Exception( _
                        LastError, _
                        "There is no default printer." _
                    )
            ElseIf LastError = ERROR_INSUFFICIENT_BUFFER Then
                s = Space(n)
                Success = GetDefaultPrinter(s, n)
                If Success Then
                    Return Strings.Left(s, n - 1)
                Else
                    Throw New Win32Exception
                End If
            Else
                Throw New Win32Exception
            End If
        End If
    End Function
End Class
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 May 2006 7:36 PM
Herfried K. Wagner [MVP]
Addendum:

Article:

Getting and setting the system default printer
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=defaultprinter&lang=en>

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