分享
 
 
 

Using standard printing from windows

王朝system·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Using standard printing from windows

There are many applications where I don't really want to use the usual windows page print. Instead I want output to go directly to the printer using standard print i/o. It's actually a topic that is hard to find in any of the books on Windows, at least I've never found anything on it. But to my surprise I recently learned that standard (DOS/UNIX) printing is alive and well underneath windows. All we need to do is open a printer port and print to it.

If the printer is directly attached to the computer it's trivial. The method for obtaining a printer port when the printer is on the network isn't hard either.

The example below shows how I use the Windows NET USE command to re-direct LPT1 to a shared printer on an NT Server. The same technique applies for Novell networks with a slightly different syntax.

Try this out by creating a new MFC Form-based project. Put a button on the form and attach this code to it. You can actually print with only 3 lines of code:

FILE *fp = fopen("LPT1", "w");

fprintf(fp,"What's up, Doc?\n");

fclose(fp);

Instant print gratification!!

While the program is open it hogs the printer port. In my shop that isn't a problem but be aware of the effect on your windows spooled output.

*********************************************************

THE CODE

*********************************************************

// the headers for the conventional i/o routines

#include

#include

#include

using namespace std; // makes string and ofstream

// work without std:: qualifier

void CLineprtView::OnButton1()

{

// I could have used a CString instead of the buff[]

// but I wanted to show how this is used with lightweight

// ATL code and STD library

char buff[MAX_BUFF_SIZE];

// My printer is located on another server so I must re-direct the

// printer port. If the printer is directly attached this extra step

// is not needed.

// on my network the printer is published as \\GREEN\hp5annex

// All those back-slashes escape the backslash in the path name

if (PRINTER_IS_REMOTE)

{

system("NET USE LPT1 /d"); // free up the port

system("net use lpt1 \\\\green\\hp5annex");

}

// old fashioned file handle with

// old fashioned open of the printer port

FILE *ptr = fopen("LPT1","w");

// laser printer setup string

sprintf(buff,"\033E\033(s0p4102t1b16.66H\033&l1O");

fprintf(ptr,buff);

// old fashioned print

fprintf(ptr,"Who of late doth make a thimble.\n");

fprintf(ptr,"Is a lower bunk a status symbol??\n");

// old fashioned close

fclose(ptr);

// now the same thing with stream io

ofstream optr("LPT1", ios::out);

string str_text = "Hey Doc, Ain't this a print test from windows\n";

str_text += "with more lines to follow?\n";

optr << str_text << endl;

optr << "Quiet, wabbit. I'm conversing with my muse!!\n";

optr << "That's all folks." << "\f" << flush; // add a formfeed

// the printer connection is still open so close it

optr.close();

// drop the network link

if (PRINTER_IS_REMOTE)

{

system("net use lpt1 /d");

}

}

In practice I get printer path information from the registry on each machine, so the real live code is a little busier than this example, but not much.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有