设为首页  
联系我们  
加入收藏  
网页制作 冲浪宝典 图形图像 操作系统 软件教学 编程开发 认证考试 安全技术 站长专区 文学驿站 娱乐天地 游戏天地 办公软件
文章搜索
您的位置: 首页 >> 文章首页 >> 编程开发 >> 其他开发语言 >> Using standard printing from windows
精品推荐
其他开发语言点击TOP10
·数字小键盘指法练习
·用C语言编通讯录程序(初学者级别的)
·Modem 常用AT指令集
·单片机模拟I2C总线及24C02(I2C EEPROM)读写实例(源代码)
·C++经典电子书下载
·Thinking in C++ 简体中文第二版
·debug和release的区别
·error LNK2001: unresolved external symbol __ftol2 错误解决
·C库函数手册
·一个简单的C语言编译器
编程开发点击TOP10
·数字小键盘指法练习
·ASP.NET 程序中常用的三十三种代码
·用C语言编通讯录程序(初学者级别的)
·我写的Java学生成绩管理系统源代码
·CHK文件恢复工具
·Modem 常用AT指令集
·java笔试题
·异常java.sql.SQLException: Io exception:The Network Adapter could not establish connection
·单片机模拟I2C总线及24C02(I2C EEPROM)读写实例(源代码)
·C++经典电子书下载
精选专题

Using standard printing from windows

作者: 来源:网络文章 时间:2005-12-16 23:42:46

Word-BREAK: break-all" width="100%">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.


Using standard printing from windows 相关文章:
Using standard printing from windows 相关软件:
特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
转载请注明来源:http://www.xgdown.com