15,917,968 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Tom6012 (Top 3 by date)
Tom6012
21-Jul-14 23:17pm
View
Hi Duncan,
Yes, Win7 work well!
But Win 8 have system error 6 found in disable printer rountine.
As checking SetPrinter return False, and Marshal.GetLastWin32Error() return 6.
Portion of disable printer routine.
static unsafe void CSDisablePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0,
(byte*)PRINTER_STATUS_OFFLINE, PRINTER_CONTROL_SET_STATUS))
{
throw new ApplicationException("Cannot disable printer " + Marshal.GetLastWin32Error());
}
List all of code as below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; // DllImport
// rference page http://support.microsoft.com/kb/315720/zh-tw
namespace PrintAPI
{
public partial class Form1 : Form
{
unsafe struct PRINTER_DEFAULTS
{
public void* pDatatype; // LPTSTR
public void* pDevMode; // LPDEVMODE
public uint DesiredAccess; // ACCESS_MASK
};
[DllImport("kernel32", SetLastError = true)]
static extern int GetLastError();
[DllImport("WinSpool.drv", SetLastError = true)]
static extern unsafe bool OpenPrinter
(string pPrinterName, int* phPrinter, void* pDefault);
[DllImport("WinSpool.drv", SetLastError = true)]
static extern bool ClosePrinter(int hPrinter);
[DllImport("WinSpool.drv", SetLastError = true)]
static extern unsafe bool SetPrinter(int hPrinter, uint Level, void* pPrinter, uint Command);
const uint PRINTER_ACCESS_ADMINISTER = 0x00000004;
const uint PRINTER_STATUS_OFFLINE = 0x00000080;
const uint PRINTER_CONTROL_PAUSE = 1;
const uint PRINTER_CONTROL_RESUME = 2;
const uint PRINTER_CONTROL_PURGE = 3;
const uint PRINTER_CONTROL_SET_STATUS = 4;
// Open a printer for administration operations.
static unsafe int CSOpenPrinter(string printerName)
{
bool bResult;
int hPrinter;
PRINTER_DEFAULTS pd;
pd.pDatatype = null;
pd.pDevMode = null;
pd.DesiredAccess = PRINTER_ACCESS_ADMINISTER;
bResult = OpenPrinter(printerName, &hPrinter, &pd);
if (!bResult)
{
throw new ApplicationException("Cannot open printer '" + printerName + "' " + Marshal.GetLastWin32Error());
}
return hPrinter;
}
// Close the printer.
static void CSClosePrinter(int hPrinter)
{
if (!ClosePrinter(hPrinter))
{
throw new ApplicationException("Cannot close printer " + Marshal.GetLastWin32Error());
}
}
// Pause printer.
static unsafe void CSPausePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_PAUSE))
{
throw new ApplicationException("Cannot pause printer " + Marshal.GetLastWin32Error());
}
}
// Resume printer.
static unsafe void CSResumePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_RESUME))
{
throw new ApplicationException("Cannot resume printer " + Marshal.GetLastWin32Error());
}
}
// Disable printer (offline).
static unsafe void CSDisablePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0,
(byte*)PRINTER_STATUS_OFFLINE, PRINTER_CONTROL_SET_STATUS))
{
throw new ApplicationException("Cannot disable printer " + Marshal.GetLastWin32Error());
}
}
// Enable printer.
static unsafe void CSEnablePrinter(int hPrinter)
{
if (!SetPr
Tom6012
17-Jul-14 6:54am
View
It looks not support Win7/8.
Tom6012
16-Jul-14 3:06am
View
Any idea to set printer as offline by C#?