473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

GetDefaultPrinter

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.

May 16 '06 #1
8 5317
"mreisi" <in**@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/>

May 16 '06 #2
Herfried,

What was the problem?

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:Ou**************@TK2MSFTNGP04.phx.gbl...
"mreisi" <in**@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/>

May 17 '06 #3
A Language problem, he he ! Ich verstehen deutsch nicht gut...

May 17 '06 #4
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
May 17 '06 #5
Cor,

"Cor Ligthert [MVP]" <no************@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/>

May 17 '06 #6
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:O2*************@TK2MSFTNGP05.phx.gbl...
Cor,

"Cor Ligthert [MVP]" <no************@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/>

May 17 '06 #7
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/>
May 17 '06 #8
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/>
May 17 '06 #9

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

Similar topics

3
by: Rob C | last post by:
I am rebuilding a VC++ w/ MFC Project from Visual Studio V6 in Visual Studio .NET 2003. There are 2 errors that I am getting that I do not get when I build using V6. 1) problem with my...
2
by: Willem | last post by:
Hi, I'm trying to get the default printer using WMI. I use the following code: Private Function GetDefaultPrinter() As String Dim objWMIService As Object Dim colInstalledPrinters As Object...
6
by: **Developer** | last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that only the "A" version would work correctly. Anyone have a suggestion of why the "W" version would not work correctly? ...
2
by: Klaus | last post by:
hello ng, can you help me? how i can find out: which printer ist the standard printer? have you a code example ? Thanks for your help.
11
by: pamelafluente | last post by:
I am doing my own PrintDialog, and have placed there a combo with the printer names, as in the PrintDialog provided by VB.NET. Here is the question: how do I open the native windows printer...
4
by: mreisi | last post by:
Hallo Leute, ich versuche seit langem eine Lösung für das Auslesen des Standarddruckers zu finden. WMI, API, System.Drawing.Printing.PrinterSettings habe ich alles schon versucht, leider nicht...
9
by: Mike C# | last post by:
Hi all, Running into a little problem. I wrote a small app on VC++ 7.1 that calls the GetDefaultPrinter() function. Now I have to "downgrade" it to VC++ 6, but every time I compile I get the...
4
by: franchdream | last post by:
DELPHI code: unit test1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type
2
by: Tim Sprout | last post by:
The P/Invoke Interop Assistant (http://www.codeplex.com/clrinterop) generates a signature for GetDefaultPrinter using an uint type for pcchBuffer: public static extern bool GetDefaultPrinter( ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.