1 Replies - 504 Views - Last Post: 22 August 2009 - 07:09 AM Rate Topic: -----

#1 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Can Not Print Document

Post icon  Posted 21 August 2009 - 08:03 PM

I have an HP injet printer attached to USB port, and I'm trying to figure out how to print something on it. Everything in my program seems to work ok but nothing is printed. Any ideas why? (os = Vista Home, compiler = vc++ 2008 Express)

#include <windows.h>
#include <iostream>

int main(int argc, char* argv[])
{
	HKEY hKey = 0;
	char printerName[255] = {0};
	// Get the name of the default printer
	int s = RegOpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows",&hKey);
	if( s == ERROR_SUCCESS)
	{
		DWORD dwSize = sizeof(printerName);
		DWORD dwType = REG_SZ;
		s = RegQueryValueEx(hKey,"Device", 0, &dwType, (BYTE*)printerName, &dwSize);
		if( s != ERROR_SUCCESS)
		{
			std::cout << "Failed\n";
			return 1;
		}
		else
		{
			std::cout << printerName << "\n";
			char* p = strchr(printerName,',');
			if(p) *p = 0;
		}
		RegCloseKey(hKey);
	}
	else
	{
		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,s,0,printerName,sizeof(printerName),0);
		std::cout << printerName << "\n";
		return 1;
	}
	HANDLE hPrinter = 0;
	DOC_INFO_1 info;
	memset(&info,0,sizeof(info));
	info.pDocName = "Test Document";
	// Open the printer
	BOOL b = OpenPrinter(printerName, &hPrinter, 0);
	if(b == 0)
	{
		std::cout << "OpenPrinter() failed\n";
		return 1;
	}
	DWORD dwPrintJob = StartDocPrinter(hPrinter,1,(BYTE*)&info);
	std::cout << "dwPrintJob = " << dwPrintJob << "\n";
	b = StartPagePrinter(hPrinter);
	if(b == 0)
	{
		std::cout << "StartPagePrinter() failed\n";
		return 1;
	}
	char *buf = "Now is the time for all good men to come to the aid of their country";
	DWORD dwWritten = 0;
	WritePrinter(hPrinter,buf, strlen(buf), &dwWritten);
	std::cout << "dwWritten = " << dwWritten << "\n";
	EndPagePrinter(hPrinter);
	EndDocPrinter(hPrinter);
	ClosePrinter(hPrinter);

	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Can Not Print Document

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Can Not Print Document

Posted 22 August 2009 - 07:09 AM

Geez, looks OK to me. Another Vista bonus?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1