943,862 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3049
  • C++ RSS
Dec 1st, 2008
0

Reading from LPT1

Expand Post »
Hi all,

I'm fairly new to programming as a whole and so I would be grateful if you can help me out.

I'm trying to write a program in VC++ or C++ that would intercept print jobs from another program and save this information in another file(to be formatted later). My idea was to read all the data being sent to LPT1 and save it in a text file.

I've tried writing a program which, according to my limited programming experience, should work(but doesn't). I just ran the program while printing from another text file and the program didn't do anything. It just ran forever.

Can any of you please help me locate and perhaps correct the faults in my program? I'd be very grateful for any help whatsoever.

Below is my code. Thanks.

C++ Syntax (Toggle Plain Text)
  1. void main(void)
  2. {
  3.  
  4. DWORD rread=0, written=0;
  5.  
  6. //create a handle to LPT1
  7. HANDLE handle = CreateFile( L"LPT1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
  8.  
  9. //Check if handle was created
  10. if (handle == INVALID_HANDLE_VALUE)
  11. {
  12. cout << "Invalid handle" << endl;
  13. }
  14.  
  15. //open file to store data read from LPT1 to
  16. ofstream file_op("D:\\myStreamBuffer.txt", ios:ut);
  17.  
  18.  
  19. //Read data(exactly 8.192 bytes) from LPT1 (I specified 8.192 because I know how big the file to be printed is)
  20. ReadFile(handle, file_op, 8.192, &rread, NULL);
  21.  
  22. //close the file. (I suspect this could potentially be the source of the //problem. perhaps I have to do a special command in order for //what was read to be stored into the file?)
  23. file_op.close();
  24.  
  25. //close handle to LPT1
  26. CloseHandle(handle);
  27.  
  28.  
  29. }

I did a few tests and noticed that the program gets stuck on the following line
C++ Syntax (Toggle Plain Text)
  1. ReadFile(handle, file_op, 8.192, &rread, NULL);
Last edited by TriceD; Dec 1st, 2008 at 10:00 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Dec 1st, 2008
1

Re: Reading from LPT1

LPT1 is a device, you are unable to open it to read what someone else has written (or will write) to it, it just doesn't work that way.

To do what you are proposing, you would need to somehow 'get between' the other application and the output device. You could then log the characters the other application sends and forward them to the device.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 2nd, 2008
0

Re: Reading from LPT1

Click to Expand / Collapse  Quote originally posted by Murtan ...
LPT1 is a device, you are unable to open it to read what someone else has written (or will write) to it, it just doesn't work that way.
Ok, thanks, so I have to stop trying what I've been trying to do so far.

Quote ...
To do what you are proposing, you would need to somehow 'get between' the other application and the output device. You could then log the characters the other application sends and forward them to the device.
Do you have any suggestions on how I could do this? or at least point me in a direction which I could look further into myself? Any help would be very appreciated. Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Dec 2nd, 2008
0

Re: Reading from LPT1

There are several possibilities for 'getting between'.

In the end, you will end up providing an interface to the program that must act as it expects the 'LPT' to act.

To better understand the problem:

Does the program think that a printer is attached to the LPT or is it some other device?

What Operating System does the program run under?

Does the program have any configuration to select the LPT or printer?

I doubt this is an option, but is it possible for you to modify and re-build the program?

The source code you included appears to be for Windows. Almost all Windows applications allow you to select the printer to print to.

The stream of bytes that Windows sends to the printer very rarely reflects the characters that are being printed. For example, if you put "Hello, World" in notepad and print it, the resulting byte stream to the printer will far exceed the 12 characters you typed.

Are you trying to catch the byte-stream or the characters?
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 3rd, 2008
0

Re: Reading from LPT1

Hello Murtan, I'd like first of all to thank you for your co-operation. I really appreciate it. Now on to your questions.

Click to Expand / Collapse  Quote originally posted by Murtan ...

Does the program think that a printer is attached to the LPT or is it some other device?
I'm not too sure, but I think the program does think a printer is attached to the LPT. It might seem weird that I say "I think", but the thing is, I'm supposed to do this for an internship and I haven't seen the program myself. My boss just asked me to write a program that would intercept information going to LPT1 and store it somewhere else, in, say, an xml file. He also seems to be intentionally vague about this.

Quote ...
What Operating System does the program run under?
The program runs under windows. He did mention that it was running on Dosbox.

Quote ...
Does the program have any configuration to select the LPT or printer?
I don't think so, but I could try to find out later, though he isn't around.

Quote ...
I doubt this is an option, but is it possible for you to modify and re-build the program?
No, this is not possible.


Quote ...
The stream of bytes that Windows sends to the printer very rarely reflects the characters that are being printed. For example, if you put "Hello, World" in notepad and print it, the resulting byte stream to the printer will far exceed the 12 characters you typed.
I'd imagine though, that that stream of bytes would also contain the information(say the characters) being printed, right? Such that if one could get the stream of bytes, then theoretically, one could get the information being printed, right?

Quote ...
Are you trying to catch the byte-stream or the characters?
Optimally, I'd like to catch the characters but at this point, I'd be more than ecstatic if I could get just the byte-stream.

Thanks once again for your co-operation thus far.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Dec 3rd, 2008
0

Re: Reading from LPT1

If the program you are trying to intercept is a DOSBOX program (which I read to mean a program written before windows, but that will be run under windows) that does help clarify what you will need to do.

Because it is a DOSBOX program, it probably will just send the characters to the printer. There may be few control codes mixed in, but it should be fairly easy to filter them.

There were a couple of DOS TSR based programs that would intercept calls to the printer and direct them to a file (See for example PRN2FILE, source code if you can find it)

These programs no-longer work under the modern windows interaface as they worked using a DOS concept calls TSR (Terminate Stay Resident) which allowed them to remain in memory to handle the calls, but that is no-longer available with Windows.

Can you have your program 'launch' or 'start' the other program?

If so, you might be able to 'pre-setup' its runtime environment such that when it goes to print, your 'wrapper' program gets the data instead. I know that being the 'launcher' of an application can give you additional rights releated to the application when it runs.

I am unfortunately 'stuck' at this point as I can't find a resource to direct you to for more information, but don't have any more time to spend right now either.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 8th, 2008
0

Re: Reading from LPT1

Click to Expand / Collapse  Quote originally posted by Murtan ...

There were a couple of DOS TSR based programs that would intercept calls to the printer and direct them to a file (See for example PRN2FILE, source code if you can find it)
Unfortunately, I could only find the ASM source of PRN2FILE and have no idea how to go about using that to implement something similar in c++. Thanks for this suggestion though. In the worst case, I could just use PRN2FILE instead of doing everything by myself.

Quote ...
These programs no-longer work under the modern windows interaface as they worked using a DOS concept calls TSR (Terminate Stay Resident) which allowed them to remain in memory to handle the calls, but that is no-longer available with Windows.
Oh wait, do you mean PRN2FILE wouldn't work in windows anymore? I hope not.

Quote ...
Can you have your program 'launch' or 'start' the other program?
I should be able to do this, considering that my boss claims to have the exe.

Quote ...
If so, you might be able to 'pre-setup' its runtime environment such that when it goes to print, your 'wrapper' program gets the data instead. I know that being the 'launcher' of an application can give you additional rights releated to the application when it runs.
This sounds very interesting and I'd really like to look more into this possibility.

Quote ...
I am unfortunately 'stuck' at this point as I can't find a resource to direct you to for more information, but don't have any more time to spend right now either.
Quite frankly, you've been of a lot of help already, and I appreciate it a lot. By "don't have any more time to spend right now either.", did you mean generally, or just on the day when you wrote this? If you meant just on that day, then I'd be more than grateful, if you could tell me generally(and if I'm lucky, even specifically) how I would go about with your idea above of pre-setting the runtime enviroment. If you meant you don't have time anymore to look into this, then I completely understand and you can disregard my last request.

Best regards.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Dec 11th, 2008
0

Re: Reading from LPT1

Hi Murtan and to anyone who might be interested,

just wanted to mention, that after your help and lots of help from 2 other posters on another forum, I've been able to find a workaround this problem.

By mapping lpt1 to a text file, everything the dos program prints is redirected to the text file. What I'll do now is make my program just monitor that text file for changes and whenever there is a change in the file(i.e the dos program has printed to the file), my program will read in what was printed, parse it to the required format and print it out.

Thanks once again for all your help... now I'm off to implementing the parsing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Having trouble with assignement.
Next Thread in C++ Forum Timeline: add a string to a queue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC