Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows打印机枚举。为什么C++和C语言之间存在差异?_C#_C++_Windows_Printing_Network Printers - Fatal编程技术网

C# Windows打印机枚举。为什么C++和C语言之间存在差异?

C# Windows打印机枚举。为什么C++和C语言之间存在差异?,c#,c++,windows,printing,network-printers,C#,C++,Windows,Printing,Network Printers,我正在研究一个C程序,它需要枚举网络上的windows打印队列。因此,基本上我使用P/Invoke调用Winspool.drv dll中定义的EnumPrinters。 当我查询正在运行的工作站上定义的本地打印机时,它可以正常工作,但当涉及到远程打印服务器/打印机时,我遇到了一个问题 让我显示我的C代码的详细信息: 首先是EnumPrinters的P/Invoke定义: [DllImport("winspool.drv", CharSet = CharSet.Unicode, SetL

我正在研究一个C程序,它需要枚举网络上的windows打印队列。因此,基本上我使用P/Invoke调用Winspool.drv dll中定义的EnumPrinters。 当我查询正在运行的工作站上定义的本地打印机时,它可以正常工作,但当涉及到远程打印服务器/打印机时,我遇到了一个问题

让我显示我的C代码的详细信息: 首先是EnumPrinters的P/Invoke定义:

     [DllImport("winspool.drv", CharSet = CharSet.Unicode, SetLastError = true)]
     public static extern bool EnumPrintersW(uint Flags, [MarshalAs(UnmanagedType.LPWStr)] string Name, uint Level, System.IntPtr pPrinterEnum, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned);
当我调用EnumPrintersW时,我首先调用空参数以获得所需的内存量。我将使用Marshal.AllocHGlobal分配响应缓冲区,然后使用更新的参数再次调用EnumPrintersW 查找远程打印服务器的第一个调用序列是:

     bool cc = EnumPrintersW(PRINTER_ENUM_REMOTE, String.Empty, 1, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);
     IntPtr pPrts = Marshal.AllocHGlobal(cbNeeded);
     bool cc = EnumPrintersW(PRINTER_ENUM_REMOTE, String.Empty, 1, pPrts, cbNeeded, ref cbNeeded, ref cReturned);
此序列将返回多个打印机信息结构。其中一个例子是:

 "LanMan Print Services!!\\COMPIX"
对EnumPrintersW的下一个调用序列旨在查找服务器COMPIX上定义的共享打印机,但对EnumPrintersW的第一个调用将返回false。我尝试调用GetLastError,返回5访问被拒绝:

     bool cc = EnumPrintersW(PRINTER_ENUM_NAME, "LanMan Print Services!!\\COMPIX", 1, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);
     // returns false
     // The next lines of code is not called in my real program
     IntPtr pPrts = Marshal.AllocHGlobal(cbNeeded);
     bool cc = EnumPrintersW(PRINTER_ENUM_NAME, "LanMan Print Services!!\\COMPIX", 1, pPrts, cbNeeded, ref cbNeeded, ref cReturned);

棘手的部分是,我有完全相同的代码,用C++编写,这里调用的顺序相同,返回服务器CopyX上的打印机列表,我想。 所以我的问题很简单:我在C代码中做错了什么?由于GetLastError返回的访问被拒绝,我是否需要在托管空间中初始化某些权限

提前谢谢