⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 print.c

📁 网络打印机及文件共享 C程序源代码 用于网络通信
💻 C
字号:
 # include <stdio.h>
 # include <dos.h>
 # include <bios.h>

 union  REGS    ipregs;
 union  REGS    opregs;
 struct SREGS   spregs;

struct DoubleWord
{
   unsigned int HiWord;
   unsigned int LoWord;
};
union  LongToWord
{
   long    dw;
   struct  DoubleWord w;
};

int  IntSwap();
long LongSwap();
int GetBinderyObjectID();
int CancelLPTCapture();
int CancelSpecificLPTCapture();
int EndLPTCapture();
int EndSpecificLPTCapture();
int FlushLPTCapture();
int FlushSpecificLPTCapture();
int GetBannerUserName();
int GetDefaultLocalPrinter();
int GetLPTCaptureStatus();
int GetPrinterStatus();
int SetBannerUserName();
int SetCapturePrintQueue();
int SetDefaultLocalPrinter();
int SpecifyCaptureFile();
int StartLPTCapture();
int StartSpecificLPTCapture();

main()
{
char  LPTDevice=0;
char  userName[13];
long  QueueID;
char  driveHandle=3;
char  filePath[255];
int   return_code;
char  ch,userdata[100];
FILE  *handle;
char object_name[48];
int object_type=3;

clrscr();
printf("\n\n\n                      The example for print DOS call\n\n\n");

SetDefaultLocalPrinter(LPTDevice);
LPTDevice=GetDefaultLocalPrinter();
printf("The  default local LPTDevice : LPT%d \n\n",LPTDevice+1);

GetBannerUserName(userName);
printf("The banner username is '%s' now.\n\n",userName);
printf("Input the banner username you want:");
scanf("%s",userName);
SetBannerUserName(userName);

printf("\nYou can print your data to Printer or File, your choice(P/F):");
scanf("\n%c",&ch);
if (ch=='f'||ch=='F')
  {printf("\nInput the filename(8 character):");
   scanf("%s",filePath);
   return_code=SpecifyCaptureFile(driveHandle, filePath);
   if (return_code==0)
     {StartLPTCapture();
      return_code=GetLPTCaptureStatus();
      if (return_code==0xff)
         {printf("\n\nNow you can print your data!\nPlease input:");
          scanf("%s",userdata);
          fprintf(stdprn,userdata);
          printf("\nSuccess,Press any key to quit!");
         }
     }
   else
      printf("\nFailure,Press any key to quit!");
   ch=getch();
   }
else
   {printf("\nInput the name of print queue:");
    scanf("%s",object_name);
    return_code=GetBinderyObjectID(object_name, object_type,&QueueID);
    if (return_code==0)
       {SetCapturePrintQueue(LPTDevice,QueueID);
        StartLPTCapture();
        return_code=GetLPTCaptureStatus();
        if (return_code==0xff)
           {printf("\n\nNow you can print your data!\nPlease input:");
            scanf("%s",userdata);
            fprintf(stdprn,userdata);
            printf("\nYou want to End/Cancel/Flush the capture(E/C/F):");
            scanf("\n%c",&ch);
            if (ch=='e'||ch=='E')
               EndLPTCapture();
            if (ch=='c'||ch=='C')
               CancelLPTCapture();
            if (ch=='f'||ch=='F')
               FlushLPTCapture();
            printf("\nSuccess,Press any key to quit!");
           }
       }
   else
      printf("\nFailure,Press any key to quit!");
   ch=getch();
   }

return_code=GetLPTCaptureStatus();
if (return_code==0xff)
   EndLPTCapture();

}

int IntSwap(unsigned int SwapInt)
{

   unsigned int mask1,mask2;

   mask1 = SwapInt << 8;
   mask2 = SwapInt >> 8;
   return (mask1 | mask2);

}

long LongSwap(long SwapLong)
{

   unsigned int mask1,mask2;
   union LongToWord tmp;

   tmp.dw = SwapLong;
   mask1 = IntSwap(tmp.w.HiWord);
   mask2 = IntSwap(tmp.w.LoWord);
   tmp.w.HiWord = mask1;
   tmp.w.LoWord = mask2;
   return (long)(tmp.dw);
}

int GetBinderyObjectID(objectName, objectType, objectID)
char *objectName;
int   objectType;
long *objectID;
{

   char    requestBuf[53], replyBuf[56];
   int     ccode, len;

   requestBuf[2] = 53;
   *((int *)(requestBuf + 3)) = IntSwap(objectType);
   len = strlen(objectName);
   requestBuf[5] = len;
   memmove(requestBuf + 6, objectName, len);
   *((int *)requestBuf) = len + 4;
   *((int *)replyBuf) = 54;

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.x.si = FP_OFF(requestBuf);
   ipregs.x.di = FP_OFF(replyBuf);
   ipregs.h.ah = 0xE3;
   intdosx(&ipregs,&opregs,&spregs);

   if (opregs.h.al)
      return(opregs.h.al);

   *objectID = LongSwap(*((long *)(replyBuf + 2)));

   return (0);

}


int CancelLPTCapture()
{

   ipregs.h.ah = 0xDF;
   ipregs.h.dl = 0x02;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int CancelSpecificLPTCapture(LPTDevice)
char    LPTDevice;
{

   ipregs.h.ah = 0xDF;
   ipregs.h.dl = 0x06;
   ipregs.h.dh = LPTDevice;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int EndLPTCapture()
{

    ipregs.h.ah = 0xDF;
    ipregs.h.dl = 0x01;
    intdosx(&ipregs,&opregs,&spregs);

    return (opregs.h.al);
}

int EndSpecificLPTCapture(LPTDevice)
char    LPTDevice;
{

   ipregs.h.ah = 0xDF;
   ipregs.h.dl = 0x05;
   ipregs.h.dh = LPTDevice;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);

}

int FlushLPTCapture()
{

   ipregs.h.ah = 0xDF;
   ipregs.h.dl = 0x03;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int FlushSpecificLPTCapture(LPTDevice)
char    LPTDevice;
{

    ipregs.h.ah = 0xDF;
    ipregs.h.dl = 0x07;
    ipregs.h.dh = LPTDevice;
    intdosx(&ipregs,&opregs,&spregs);

    return (opregs.h.al);
}

int GetBannerUserName(userName)
char *userName;
{

   ipregs.h.ah = 0xB8;
   ipregs.h.al = 0x08;
   spregs.es   = FP_SEG(userName);
   ipregs.x.bx = FP_OFF(userName);
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int GetDefaultLocalPrinter()
{

   ipregs.h.ah = 0xB8;
   ipregs.h.al = 0x04;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.dh);
}

int GetLPTCaptureStatus()
{

   ipregs.h.ah = 0xF0;
   ipregs.h.al = 0x03;
   intdosx(&ipregs,&opregs,&spregs);

   if (opregs.h.al)
      return(opregs.h.ah);
}

int GetPrinterStatus(printerNumber, printerHalted, printerOffLine, formType,
                     targetPrinter)
int   printerNumber;
int   *formType;
int   *targetPrinter;
char  *printerHalted;
char *printerOffLine;
{

   char    requestBuf[4];
   char    replyBuf[6];
   int     ccode;

   requestBuf[2] = 6;
   requestBuf[3] = (char)((char)printerNumber);
   *((int *)requestBuf) = 2;

   *((int *)replyBuf) = 4;

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.x.si = FP_OFF(requestBuf);
   ipregs.x.di = FP_OFF(replyBuf);
   ipregs.h.ah = 0xE0;

   intdosx(&ipregs,&opregs,&spregs);

   if (opregs.h.al)
   {
      if (printerHalted)
          *printerHalted = replyBuf[2];

      if (printerOffLine)
          *printerOffLine = replyBuf[3];

      if (formType)
          *formType = replyBuf[4];

      if (targetPrinter)
           *targetPrinter = replyBuf[5];
   }

   return (opregs.h.al);
}

int SetBannerUserName(userName)
char *userName;
{

   ipregs.h.ah = 0xB8;
   ipregs.h.al = 0x09;
   spregs.es   = FP_SEG(userName);
   ipregs.x.bx = FP_OFF(userName);
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

 int SetCapturePrintQueue(LPTDevice, QueueID)
 char    LPTDevice;
 long    QueueID;
 {
   union LongToWord tmp;

   ipregs.h.ah = 0xB8;
   ipregs.h.al = 0x06;
   ipregs.h.dh = LPTDevice;
   tmp.dw = QueueID;
   ipregs.x.bx = IntSwap(tmp.w.HiWord);
   ipregs.x.cx = IntSwap(tmp.w.LoWord);
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
 }

 int SetDefaultLocalPrinter(LPTDevice)
 char    LPTDevice;
 {

   ipregs.h.ah = 0xB8;
   ipregs.h.al = 0x05;
   ipregs.h.dh = LPTDevice;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
 }

int SpecifyCaptureFile(driveHandle, filePath)
char driveHandle;
char *filePath;
{
   char    len;
   char    requestBuf[261], replyBuf[2];

   len = (char)strlen(filePath);
   *((int *)requestBuf) = 3 + len;
   requestBuf[2] = 9;
   requestBuf[3] = driveHandle;
   requestBuf[4] = len;
   memcpy(requestBuf + 5, filePath, (int)len);
   *((int *)replyBuf) = 0;

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.x.si = FP_OFF(requestBuf);
   ipregs.x.di = FP_OFF(replyBuf);
   ipregs.h.ah = 0xE0;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int StartLPTCapture()
{

   ipregs.h.ah = 0xDF;
   ipregs.h.dl = 0x00;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int StartSpecificLPTCapture(LPTDevice)
{

   ipregs.h.ah = 0xDF;
   ipregs.h.dl = 0x04;
   ipregs.h.dh = LPTDevice;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -