unit PrtRaw;
interface
uses
WinTypes;
{$IFDEF WIN32}
type SpoolInt = DWORD;
{$else
}
type SpoolInt = integer;
{$ENDIF}
function StartRawPrintJob(PrinterName : pChar;
Port : pChar;
do
cName : pChar) : THandle;
function StartRawPrintPage(hPrn : THandle) : integer;
function PrintRawData(hPrn : THandle;
Buffer : pointer;
NumBytes : SpoolInt) : integer;
function EndRawPrintPage(hPrn : THandle) : integer;
function EndRawPrintJob(hPrn : THandle) : integer;
implementation
uses
WinProcs,
{$IFDEF WIN32}
WinSpool;
{$else
}
Print;
{$ENDIF}
function StartRawPrintJob(PrinterName : pChar;
Port : pChar;
do
cName : pChar) : THandle;
{$IFDEF WIN32}
var
hPrn : THandle;
do
cInfo1 : TDocInfo1;
{$ENDIF}
begin
{$IFDEF WIN32}
if (OpenPrinter(PChar(PrinterName),
hPrn,
nil) = FALSE) then
begin
Result := THandle(-1);
Exit;
end;
do
cInfo1.pDocName :=do
cName;
do
cInfo1.pOutputFile := Port;
do
cInfo1.pDataType := 'RAW';
if (StartDocPrinter(hPrn,
1,
@DocInfo1) = 0) then
begin
Result := THandle(-1);
exit;
end;
Result := hPrn;
{$else
}
result := OpenJob(Port,
do
cName,
0);
{$ENDIF}
end;
function StartRawPrintPage(hPrn : THandle) : integer;
begin
{$IFDEF WIN32}
if (StartPagePrinter(hPrn) = FALSE) then
begin
Result := -1;
exit;
end;
result := 1;
{$else
}
result := StartSpoolPage(hPrn);
{$ENDIF}
end;
function PrintRawData(hPrn : THandle;
Buffer : pointer;
NumBytes : SpoolInt) : integer;
{$IFDEF WIN32}
var
BytesWritten : DWORD;
{$ENDIF}
begin
if (NumBytes = 0) then
begin
Result := 1;
exit;
end;
{$IFDEF WIN32}
if (WritePrinter(hPrn,
Buffer,
NumBytes,
BytesWritten) = FALSE) then
begin
Result := -1;
exit;
end;
if (NumBytes <> BytesWritten) then
begin
Result := -1;
exit;
end;
Result := 1;
{$else
}
result := WriteSpool(hPrn,
Buffer,
NumBytes);
{$ENDIF}
end;
function EndRawPrintPage(hPrn : THandle) : integer;
begin
{$IFDEF WIN32}
if (EndPagePrinter(hPrn) = FALSE) then
begin
Result := -1;
exit;
end;
Result := 1;
{$else
}
result := EndSpoolPage(hPrn);
{$ENDIF}
end;
function EndRawPrintJob(hPrn : THandle) : integer;
begin
{$IFDEF WIN32}
if (EndDocPrinter(hPrn) = FALSE) then
begin
Result := -1;
exit;
end;
if (ClosePrinter(hPrn) = FALSE) then
begin
Result := -1;
exit;
end;
Result := 1;
{$else
}
result := CloseJob(hPrn);
{$ENDIF}
end;
end.
{Example of use:}
uses
PrtRaw;
procedure TForm1.Button1Click(Sender: TObject);
var
hPrn : THandle;
Data : array[0..31] of char;
begin
hPrn := StartRawPrintJob('Generic / Text Only',
'LPT1:',
'Mydo
cument');
if (integer(hPrn) < 0) then
begin
ShowMessage('StartRawPrintJob Failed');
exit;
end;
if (StartRawPrintPage(hPrn) < 0) then
begin
ShowMessage('StartRawPrintPage Failed');
EndRawPrintJob(hPrn);
exit;
end;
Data[0] := 'P';
Data[1] := 'a';
Data[2] := 'g';
Data[3] := 'e';
Data[4] := #32;
Data[5] := '1';
Data[6] := #13;
Data[7] := #10;
if (PrintRawData(hPrn,
@data,
8) < 0) then
begin
ShowMessage('PrintRawData Failed');
EndRawPrintPage(hPrn);
EndRawPrintJob(hPrn);
exit;
end;
if (EndRawPrintPage(hPrn) < 0) then
begin
ShowMessage('EndRawPrintPage Failed');
EndRawPrintJob(hPrn);
exit;
end;
if (StartRawPrintPage(hPrn) < 0) then
begin
ShowMessage('StartRawPrintPage Failed');
EndRawPrintJob(hPrn);
exit;
end;
Data[0] := 'P';
Data[1] := 'a';
Data[2] := 'g';
Data[3] := 'e';
Data[4] := #32;
Data[5] := '2';
Data[6] := #13;
Data[7] := #10;
if (PrintRawData(hPrn,
@data,
8) < 0) then
begin
ShowMessage('PrintRawData Failed');
EndRawPrintPage(hPrn);
EndRawPrintJob(hPrn);
exit;
end;
if (EndRawPrintPage(hPrn) < 0) then
begin
ShowMessage('EndRawPrintPage Failed');
EndRawPrintJob(hPrn);
exit;
end;
if (EndRawPrintJob(hPrn) < 0) then
begin
ShowMessage('EndRawPrintJob Failed');
exit;
end;
end;
end.