| | |
Reading from LPT1
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
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.
I did a few tests and noticed that the program gets stuck on the following line
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)
void main(void) { DWORD rread=0, written=0; //create a handle to LPT1 HANDLE handle = CreateFile( L"LPT1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL); //Check if handle was created if (handle == INVALID_HANDLE_VALUE) { cout << "Invalid handle" << endl; } //open file to store data read from LPT1 to ofstream file_op("D:\\myStreamBuffer.txt", ios:ut); //Read data(exactly 8.192 bytes) from LPT1 (I specified 8.192 because I know how big the file to be printed is) ReadFile(handle, file_op, 8.192, &rread, NULL); //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?) file_op.close(); //close handle to LPT1 CloseHandle(handle); }
I did a few tests and noticed that the program gets stuck on the following line
C++ Syntax (Toggle Plain Text)
ReadFile(handle, file_op, 8.192, &rread, NULL);
Last edited by TriceD; Dec 1st, 2008 at 10:00 am.
•
•
Join Date: May 2008
Posts: 631
Reputation:
Solved Threads: 101
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.
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.
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: May 2008
Posts: 631
Reputation:
Solved Threads: 101
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?
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?
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
Hello Murtan, I'd like first of all to thank you for your co-operation. I really appreciate it. Now on to your questions.
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.
The program runs under windows. He did mention that it was running on Dosbox.
I don't think so, but I could try to find out later, though he isn't around.
No, this is not possible.
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?
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.
•
•
•
•
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 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?
Thanks once again for your co-operation thus far.
•
•
Join Date: May 2008
Posts: 631
Reputation:
Solved Threads: 101
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.
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.
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
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.
Best regards.
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Having trouble with assignement.
- Next Thread: add a string to a queue
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





