|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
GetDefaultPrinterHallo 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. "mreisi" <i***@mathiasreis.de> schrieb: Simple test call without error handling:>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. \\\ 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/> 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/> > 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) :-) CorCor,
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: The OP's question was how to use the 'GetDefaultPrinter' API function and/or > What was the problem? 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/> 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. :-) CorShow 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/> 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/> 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/>
Frustrated trying to use "pure" VS.NET to access database properties
VB.NET Windows Service Question (Timing) How Do I Put An End Of Line Character In A TextBox User Software Settings and System Software Settings in Windows Regsitry Export to excel without using office automation VS2003 and VS2005 Side by Side Dynamically Create Timers are Run-Time VB Express - How to access query value on my form? Databinding 2 comboboxes to the same Datasource Wall chart of VB.net class structure? |
|||||||||||||||||||||||