WritePrinter api function

Hi all,

i'm trying to write directly to the port, but in Delphi i could not found the answer to this (begindoc, enddoc is not what i need).
I need to print to the printer line by line - without spooler.
I think that
OpenPrinter
WritePrinter
ClosePrinter set of commands could solve my problem, but i have problem with implementing WritePrinter function.
Could you please send me an example of it? Or some other solution for printing line by line of text directly to the printer?
Thanks
                        Robert
LVL 1
rpetruniAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MadshiConnect With a Mentor Commented:
Well, if Raymond's code works fine now, you should use it!

Anyway, this one should work, too:

PrintSomething(str: string);
var lpt : dword;
    dw1 : dword;
begin
  lpt := CreateFile('LPT1:', GENERIC_WRITE or GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0);
  WriteFile(lpt, pchar(str)^, Length(str), dw1, nil);
  CloseHandle(lpt);
end;

But you should note, that with using CreateFile/WriteFile there is no page feed sended. So when you're done with printing you have to tell the printer manually to throw the paper out.
0
 
rwilson032697Commented:
Does this help?

uses Printers;
var  
MyFile: TextFile;

begin
  AssignPrn(MyFile);
  Rewrite(MyFile);
  Printer.Canvas.Font.Name := 'Courier New';
  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.Font.PixelsPerInch:=
    GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY);
  Writeln(MyFile, 'Print this text');
  System.CloseFile(MyFile);
end;

Cheers,

Raymond.
0
 
rpetruniAuthor Commented:
Yes, but......

i would like that head of printer wait until i say got to the next line. because with code like this, i could print some text, after it the head will make few line of space, i need it to stay where the text ends, and then to tell it "go on to next line".
Is this possible?
                        Robert
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
MadshiCommented:
Use Write instead of WriteLn...   :-)
0
 
rpetruniAuthor Commented:
Yes, Madshi, i know that...
You probably don' understand what i want (or i don't know how to explain it :-) )...
The point is - if you have some kind of monitoring software, and you want your program to write line by line of text to the printer, than you don't want to print a line, and have form feed after it ....
If i use code with
BeginDoc
EndDoc
The printer will not do anything until i call EndDoc. If i call enddoc i wil have form feed which i don't wan't to have.
If i use code presented by rwilson, i will have form feed again.
The point is that all those printing things use spooler - i dont want to spool anything - i yust want to send it to port, and wait for new package to come.
So i neeed something straight as this:
begin
     use some printer
     print some line to it
// The line is actualy printed on a printer - not spooled !!!
     goto next line
     print some other line
// The line is printed
etc. etc.
     close printer...
end;
Is this possible?
I was thinking that with
OpenPrinter
WritePrinter
ClosePrinter
i could do something like this...
Thanks

0
 
lsaeCommented:
listening
0
 
rpetruniAuthor Commented:
Ok, i found something that could be done with Raw.drv driver, wich could be downloaded from Microsoft site. Did somebody use it?
As i understand, there is impossible to do what i want without this kind of drivers, because windows is always using spooler. This driver is doinng his yob without spooler (you loose GDI functionality of printing procces, but in my case i don' need it :-) )
So, once again did somebody use this driver?
                     Robert
0
 
MadshiCommented:
Okay, have you tried using CreateFile for "LPT1:", then WriteFile to the handle you got from CreateFile?

Regards, Madshi.
0
 
333Commented:
you can try to disable spooling in Settings->Printers->Properties->Spool settings.

A.
0
 
rpetruniAuthor Commented:
Madshi - i will try it ...
333 - i could do that, but form feed is my problem all the time - and form feed wil not be disabled if i disable spooling :-(
0
 
rpetruniAuthor Commented:
madshi....
i try out this code, and nothing happend :-(
what am i missing?

procedure TForm1.Button4Click(Sender: TObject);
var
     hand : THandle;
     pisem : String;
     aa : TOverlapped;
begin
     Pisem:='djfewif';
     hand := CreateFile(PChar('\\Ljerka\Epson'), GENERIC_WRITE, 0, nil, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
     WriteFileEx(hand, @pisem, sizeof(pisem), aa, nil);
end;

                  Robert
0
 
333Commented:
use WriteFile instead of WriteFileEx.
also
WriteFileEx(hand, @pisem, sizeof(pisem), aa, nil);
pass nil instead of aa.
0
 
333Commented:
more info from help:
"If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the write operation starts at the current file position and WriteFile does not return until the operation has been completed."
you don't need overlapped write, so don't use it.

A.
0
 
MadshiCommented:
Yes, 333 is right. Use WriteFile and give in "nil" for overlapped.

Another thing: You didn't talk about a net printer yet. My suggestion was thought for a local installed printer. I don't know whether you can open a net printer with CreateFile. Please check the return value of CreateFile:

if hand = INVALID_HANDLE_VALUE then
  ShowMessage(IntToStr(GetLastError));

If all else fails, you have to map a LPT port to the net printer (WNetAddConnection2).

Regards, Madshi.
0
 
rpetruniAuthor Commented:
ok, so code now looks like this:
procedure TForm1.Button4Click(Sender: TObject);
var
     hand : THandle;
     pisem : String;
     num : Integer;
begin
     Pisem:='djfewif';
     hand := CreateFile(PChar('LPT1:'), GENERIC_WRITE, 0, nil, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
     if hand = INVALID_HANDLE_VALUE then
  ShowMessage(IntToStr(GetLastError));
   WriteFile(hand, pisem, sizeof(pisem), num, nil);

Printer is local printer connected to LPT1:, with generic/text only driver.
Again - nothing is printed....
??
Thanks
                              Robert
end;
0
 
rpetruniAuthor Commented:
!!! but on this configuration code presentet by rwilson is working fine!!!
What't the point?
0
 
rpetruniAuthor Commented:
Thanks, Madshi - this is what i need, and it works - my code does not - it's almoust the same - this is what i don't understend, but...
Thanks, you have 50 pts bonus ...
0
 
MadshiCommented:
Thank you...   :-)

(Probably it was the CREATE_NEW in your sources, but I'm not sure)
0
 
rpetruniAuthor Commented:
no, no, i think it was hand - defined as THandle, you are using lpt DEFINED AS dword !!!???...
Because in your code i'v tryed out CREATE_NEW, OPEN_EXISTING, etc. etc. and it was working....
               Robert
0
 
MadshiCommented:
THandle is nothing but a dword. That is definetely not the reason.
But in the end it doesn't matter...  :-)
0
 
Wim ten BrinkSelf-employed developerCommented:
Maybe the FILE_FLAG_WRITE_THROUGH flag solved this problem? :D
0
All Courses

From novice to tech pro — start learning today.