Unfortunately, Bytamin-C is no longer there. As a matter of fact,
I saved the page long time ago, but I haven't tested it.
So here is the /untested/ original:
<quote>
Below is a quick sample of how to send data directly to the
printer. Remember to include winspool.h to your file.
NB: If you printer is directly connected to your computer
(LPT1-LPT3) or is a mapped networkprinter in which case you can
use UNC paths to the printer (eg. "\\MainServer\MatrixPrinter"
which in C would be "\\\\MainServer\\MatrixPrinter") you can
just use simple file functions eg. fopen("lpt1", "wt") or
fopen("\\\\MainServer\\MatrixPrinter", "wt"). I hope this helps
those of you in despair ;)
Regards, Jeppe.
// szBuffer is the buffer which holds the data we wanna send to the printer
bool Print(char* szPrinterName, char* szBuffer)
{
HANDLE hPrinter; // Printer handle
int iLen = strlen( szBuffer );// Length of the data to be sent
DWORD dwWritten; // How much was written to the printer
// Open printer and get a handle to it (which is saved in hPrinter)
if ( OpenPrinter( szPrinterName, &hPrinter, NULL ) )
{
// Doc info
DOC_INFO_1 Info;
memset( &Info, 0, sizeof( Info ) );
Info.pDocName = "My PrintJob"; // Name of the Doc (which is shown in
the PrintManager)
Info.pOutputFile = NULL; // We're not going to write to a file so we
set it to NULL
Info.pDatatype = "RAW"; // Data are sent in RAW format (RAW and EMF
are supported by both
// Win9x and Win NT). Win NT also supports TEXT.
// Start doc
if( !StartDocPrinter( hPrinter, 1, (LPBYTE)&Info ) )
return false;
// Tell the printer we're starting on a new page
StartPagePrinter( hPrinter ); // Write data to the printer
if( !WritePrinter( hPrinter, szBuffer, iLen, &dwWritten ) )
{
// An error happened while sending data to the printer.
// Use LastError to figure out what went wrong.
}
// Tell the printer we're done writing this page
EndPagePrinter( hPrinter );
// End the document
if( !EndDocPrinter( hPrinter ) )
return false;
// Close the printer
ClosePrinter( hPrinter );
return true;
}
else
{
return false;
}
}
</quote>
--
Best Regards,
Vladimir Stefanovic
"FlyGuy" <Manh...@mstarmetro.net> wrote in message
news:4490501d$1...@newsgroups.borland.com...
"Rudy Velthuis [TeamB]" <newsg...@rvelthuis.de> wrote in message
news:xn0enhpztupb5...@www.teamb.com...
> At 20:46:53, 14.06.2006, Vladimir Stefanovic wrote:
>
>> http://www.bytamin-c.com/Tips%20&%20Tricks/directprint.htm
>
> http://web.archive.org/web/20030105010101/http://www.bytamin-c.com/Tips+&+
> Tricks/directprint.htm
>
> or: http://tinyurl.com/h9nb5
>
> --
> Rudy Velthuis [TeamB] http://rvelthuis.de/
>
> "I was playing poker the other night... with Tarot cards.
> I got a full house and 4 people died." -- Steven Wright
Search the archives:
http://www.tamaracka.com/search.htm
You're looking for GetPrinter and SetPrinter.
~ JD
#include <winspool.h>
char deviceName[128];
char driverName[128];
char portName[128];
unsigned int hDeviceMode; // handle to DEVMODE struct in memory
DEVMODE* pDevMode; // pointer to DEVMODE struct
Prntr->GetPrinterA(deviceName, driverName, portName, hDeviceMode);
pDevMode = (DEVMODE*)GlobalLock((void*)hDeviceMode);
if (pDevMode != NULL)
{
pDevMode->dmFields = pDevMode->dmFields || DM_DEFAULTSOURCE;
pDevMode->dmDefaultSource = DMBIN_MANUAL;
// pDevMode->dmFields = pDevMode->dmFields || DM_ORIENTATION;
// pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
pDevMode->dmFields = pDevMode->dmFields || DM_PAPERLENGTH ||
DM_PAPERWIDTH;
pDevMode->dmPaperLength = 1778; // 1778 = 7" in tenths of mm
pDevMode->dmPaperWidth = 2159; // 2159 = 8.5" in tenth of mm
}
GlobalUnlock((void*)hDeviceMode);
Prntr->BeginDoc(); // start printer doc
Can anyone tell me what I might be doing wrong? I'm testing on an HP
LaserJet 1200 printer. Is it possible that these other things can't be
changed for this type of printer? Thanks!!!
"JD" <nos...@nospam.com> wrote in message
news:4492...@newsgroups.borland.com...
A couple of things:
> char deviceName[128];
> char driverName[128];
> char portName[128];
These should be 256 in length and initialized:
char deviceName[ 256 ] = { 0 };
char driverName[ 256 ] = { 0 };
char portName [ 256 ] = { 0 };
> unsigned int hDeviceMode;
While it most likely makes no difference, this is technically
a THandle:
THandle hDeviceMode;
> DEVMODE* pDevMode; // pointer to DEVMODE struct
>
> Prntr->GetPrinterA(deviceName, driverName, portName, hDeviceMode);
I'm uncertain about GetPrinterA so I would suggest that you
use GetPrinter. However, there is a known issue with TPrinter
in that it's Handle is not always valid so you need to insert
a line above the call to GetPrinter:
Printer()->PrinterIndex = x;
where x is a valid printer index. It's been more than a year
since I tested this and my notes are no longer clear. It looks
like x can not be -1 which under other conditions is a valid
value to restore the default printer. I also suspect that you
may have to use a non-default printer. You can just install a
generic printer which won't require and disks and then use the
control panel to change the default to the generic.
> pDevMode = (DEVMODE*)GlobalLock((void*)hDeviceMode);
I'm confident that this was the problem because the call to
GetPrinter was returning a bad hDeviceMode. Actually, not bad
but rather incorrect.
BTW : You really should be using C++ casting here.
> GlobalUnlock((void*)hDeviceMode);
I'm reasonably sure that you need to call SetPrinter after you
unlock but in my sample, the ADeviceMode parameter is set to
NULL. This causes concern for me but you should be able to
figure it as long as you're aware of it.
> I'm testing on an HP LaserJet 1200 printer.
You said it was a dot matrix printer!!! Big difference!
> Is it possible that these other things can't be changed for
> this type of printer?
I know that you can accomplish what you want. I just don't
know for sure how to do it.
~ JD