none
关于网络打印的问题 RRS feed

  • 问题

  • 需求如下:

    1.点击网络打印时,生成待打印文本TXT(已实现)

    2.自动搜索当前局域网内打印机

    3.选择打印机后,打印生成的文本

    注:WINFORM程序,在本地通过COPY命令打印成功

    谢谢!!!

    2010年5月15日 12:43

答案

  • 你好 參考下 Netapi32.dll

    http://www.webtropy.com/articles/art9-2.asp?lib=netapi32.dll

    using System;
       
    using System.Runtime.InteropServices;
       
    using System.Security;
       
    sealed class Tester
        {
            [DllImport(
    "Netapi32", CharSet = CharSet.Auto, SetLastError = true),
            SuppressUnmanagedCodeSecurityAttribute]
           
    static extern int NetServerEnum(
           
    string ServerNane, // must be null
            int dwLevel,
           
    ref IntPtr pBuf,
           
    int dwPrefMaxLen,
           
    out int dwEntriesRead,
           
    out int dwTotalEntries,
           
    int dwServerType,
           
    string domain, // null for login domain
            out int dwResumeHandle
            );
            [DllImport(
    "Netapi32", SetLastError = true),
            SuppressUnmanagedCodeSecurityAttribute]
           
    static extern int NetApiBufferFree(
            IntPtr pBuf);

            [StructLayout(LayoutKind.Sequential)]
           
    struct _SERVER_INFO_100
            {
               
    internal int sv100_platform_id;
                [MarshalAs(UnmanagedType.LPWStr)]
               
    internal string sv100_name;
            }
           
    static void Main()
            {
               
    const int MAX_PREFERRED_LENGTH = -1;
               
    int SV_TYPE_WORKSTATION = 1;
               
    int SV_TYPE_SERVER = 2;
                IntPtr buffer
    = IntPtr.Zero;
                IntPtr tmpBuffer
    = IntPtr.Zero;
               
    int entriesRead = 0;
               
    int totalEntries = 0;
               
    int resHandle = 0;
               
    int sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
               
    try
                {
                   
    int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH,
                   
    out entriesRead,
                   
    out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
                   
    if (ret == 0)
                    {
                       
    for (int i = 0; i < totalEntries; i++)
                        {
                            tmpBuffer
    = new IntPtr((int)buffer + (i * sizeofINFO));
                            _SERVER_INFO_100 svrInfo
    = (_SERVER_INFO_100)
                            Marshal.PtrToStructure(tmpBuffer,
    typeof(_SERVER_INFO_100));
                            Console.WriteLine(svrInfo.sv100_name);
                        }
                    }
                }
               
    finally
                {
                    NetApiBufferFree(buffer);
                }
            }
        }


    I see you~http://hi.baidu.com/1987raymondMy Blog~~~
    • 已建议为答案 stoneca 2010年5月17日 1:52
    • 已标记为答案 Jing0Moderator 2010年5月22日 8:59
    2010年5月16日 10:45
    版主
  • 需求有问题,局域网打印机一般不对用户开放。你的用户是登录到域或者或者管理员启用匿名用户访问?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    2010年5月15日 20:21
    版主
  • ------------------------------------------------------------------------------------------------------

    1 枚举本机所有打印机,包括网络打印机

         foreach (string s in System.Drawing.Printing.PrinterSettings.InstalledPrinters)1
                {
                    this.comboBox1.Items.Add(s);
                }

     

    -----------------------------------------------------------------------------------------------------

    2 下面是我在网上看到一别人写的方法:(获取打印机和打印状态)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.DirectoryServices;
    using System.Runtime.InteropServices;
    using ActiveDs;   //   Import   activeds.tlb   interop   assembly

    namespace GetNetPrint
    {
        [Flags]
        enum PrinterStatus
        {
            Paused = 1,
            DeletePending = 2,
            Error = 3,
            PaperJam = 4,
            PaperOut = 5
        } 

        class Program
        {
            static void Main(string[] args)
            {
                DirectoryEntry printer = new DirectoryEntry("WinNT://servername/printqueuename", null, null, AuthenticationTypes.ServerBind);
                Console.WriteLine(printer.Path);
                PropertyCollection pcoll = printer.Properties;
                try
                {
                    foreach (string sc in pcoll.PropertyNames)
                    {
                        Console.WriteLine(sc + ":\t" + (pcoll[sc])[0].ToString());
                    } Console.WriteLine("---------------------------------");
                }
                catch (COMException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                //   Use   the   PrintqueueOperations   interface   to   contol   the   printer   Queue 
                IADsPrintQueueOperations po = printer.NativeObject as IADsPrintQueueOperations;
                if (po != null)
                {
                    if (po.Status == (int)PrinterStatus.Paused)   //   If   paused   resume   else   pause   printer 
                        po.Resume();
                    else
                        po.Pause();
                    //   Use   the   IADsPrintJob   to   control   individual   printjobs 
                    foreach (IADsPrintJob pj in po.PrintJobs())
                    {
                        Console.WriteLine("{0}   -   {1}", pj.Description, pj.Name);
                        IADsPrintJobOperations pjo = pj as IADsPrintJobOperations;
                        Console.WriteLine(pjo.Name);
                        //   Use   IADsPrintJob.Name   as   arg.   to   remove   the   job   from   the   queue 
                        po.PrintJobs().Remove(pj.Name);
                    }
                }
                printer.Dispose();
            }
        }  
    }

    • 已建议为答案 stoneca 2010年5月18日 3:34
    • 已标记为答案 Jing0Moderator 2010年5月22日 8:59
    2010年5月17日 7:06

全部回复

  • 需求有问题,局域网打印机一般不对用户开放。你的用户是登录到域或者或者管理员启用匿名用户访问?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    2010年5月15日 20:21
    版主
  • 你好 參考下 Netapi32.dll

    http://www.webtropy.com/articles/art9-2.asp?lib=netapi32.dll

    using System;
       
    using System.Runtime.InteropServices;
       
    using System.Security;
       
    sealed class Tester
        {
            [DllImport(
    "Netapi32", CharSet = CharSet.Auto, SetLastError = true),
            SuppressUnmanagedCodeSecurityAttribute]
           
    static extern int NetServerEnum(
           
    string ServerNane, // must be null
            int dwLevel,
           
    ref IntPtr pBuf,
           
    int dwPrefMaxLen,
           
    out int dwEntriesRead,
           
    out int dwTotalEntries,
           
    int dwServerType,
           
    string domain, // null for login domain
            out int dwResumeHandle
            );
            [DllImport(
    "Netapi32", SetLastError = true),
            SuppressUnmanagedCodeSecurityAttribute]
           
    static extern int NetApiBufferFree(
            IntPtr pBuf);

            [StructLayout(LayoutKind.Sequential)]
           
    struct _SERVER_INFO_100
            {
               
    internal int sv100_platform_id;
                [MarshalAs(UnmanagedType.LPWStr)]
               
    internal string sv100_name;
            }
           
    static void Main()
            {
               
    const int MAX_PREFERRED_LENGTH = -1;
               
    int SV_TYPE_WORKSTATION = 1;
               
    int SV_TYPE_SERVER = 2;
                IntPtr buffer
    = IntPtr.Zero;
                IntPtr tmpBuffer
    = IntPtr.Zero;
               
    int entriesRead = 0;
               
    int totalEntries = 0;
               
    int resHandle = 0;
               
    int sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
               
    try
                {
                   
    int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH,
                   
    out entriesRead,
                   
    out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
                   
    if (ret == 0)
                    {
                       
    for (int i = 0; i < totalEntries; i++)
                        {
                            tmpBuffer
    = new IntPtr((int)buffer + (i * sizeofINFO));
                            _SERVER_INFO_100 svrInfo
    = (_SERVER_INFO_100)
                            Marshal.PtrToStructure(tmpBuffer,
    typeof(_SERVER_INFO_100));
                            Console.WriteLine(svrInfo.sv100_name);
                        }
                    }
                }
               
    finally
                {
                    NetApiBufferFree(buffer);
                }
            }
        }


    I see you~http://hi.baidu.com/1987raymondMy Blog~~~
    • 已建议为答案 stoneca 2010年5月17日 1:52
    • 已标记为答案 Jing0Moderator 2010年5月22日 8:59
    2010年5月16日 10:45
    版主
  • Raymond Tang 的方法枚举了网上邻居的内容。 刘汉威 是想枚举网络打印机(以匿名用户)。
    2010年5月17日 2:13
  • ------------------------------------------------------------------------------------------------------

    1 枚举本机所有打印机,包括网络打印机

         foreach (string s in System.Drawing.Printing.PrinterSettings.InstalledPrinters)1
                {
                    this.comboBox1.Items.Add(s);
                }

     

    -----------------------------------------------------------------------------------------------------

    2 下面是我在网上看到一别人写的方法:(获取打印机和打印状态)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.DirectoryServices;
    using System.Runtime.InteropServices;
    using ActiveDs;   //   Import   activeds.tlb   interop   assembly

    namespace GetNetPrint
    {
        [Flags]
        enum PrinterStatus
        {
            Paused = 1,
            DeletePending = 2,
            Error = 3,
            PaperJam = 4,
            PaperOut = 5
        } 

        class Program
        {
            static void Main(string[] args)
            {
                DirectoryEntry printer = new DirectoryEntry("WinNT://servername/printqueuename", null, null, AuthenticationTypes.ServerBind);
                Console.WriteLine(printer.Path);
                PropertyCollection pcoll = printer.Properties;
                try
                {
                    foreach (string sc in pcoll.PropertyNames)
                    {
                        Console.WriteLine(sc + ":\t" + (pcoll[sc])[0].ToString());
                    } Console.WriteLine("---------------------------------");
                }
                catch (COMException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                //   Use   the   PrintqueueOperations   interface   to   contol   the   printer   Queue 
                IADsPrintQueueOperations po = printer.NativeObject as IADsPrintQueueOperations;
                if (po != null)
                {
                    if (po.Status == (int)PrinterStatus.Paused)   //   If   paused   resume   else   pause   printer 
                        po.Resume();
                    else
                        po.Pause();
                    //   Use   the   IADsPrintJob   to   control   individual   printjobs 
                    foreach (IADsPrintJob pj in po.PrintJobs())
                    {
                        Console.WriteLine("{0}   -   {1}", pj.Description, pj.Name);
                        IADsPrintJobOperations pjo = pj as IADsPrintJobOperations;
                        Console.WriteLine(pjo.Name);
                        //   Use   IADsPrintJob.Name   as   arg.   to   remove   the   job   from   the   queue 
                        po.PrintJobs().Remove(pj.Name);
                    }
                }
                printer.Dispose();
            }
        }  
    }

    • 已建议为答案 stoneca 2010年5月18日 3:34
    • 已标记为答案 Jing0Moderator 2010年5月22日 8:59
    2010年5月17日 7:06