Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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# 当作为系统运行时,prnadminlib无法在winxp上设置处理器,当域用户_C#_.net_Winapi_Printing - Fatal编程技术网

C# 当作为系统运行时,prnadminlib无法在winxp上设置处理器,当域用户

C# 当作为系统运行时,prnadminlib无法在winxp上设置处理器,当域用户,c#,.net,winapi,printing,C#,.net,Winapi,Printing,我有一个安装打印机驱动程序的可执行文件,然后使用该驱动程序创建打印机。我使用从Server2003资源工具包生成的受管版本的tlbimp 在windows XP上,我必须将打印处理器设置为MS_XPS。默认处理器是WINPRINT。这段代码就是这样做的 static string winxpPrinterProcessor = "MS_XPS"; if (isWinXP() && pPrinter.PrintProcessor != winxpPrinterProcess

我有一个安装打印机驱动程序的可执行文件,然后使用该驱动程序创建打印机。我使用从Server2003资源工具包生成的受管版本的tlbimp

在windows XP上,我必须将打印处理器设置为MS_XPS。默认处理器是WINPRINT。这段代码就是这样做的

  static string winxpPrinterProcessor = "MS_XPS";
  if (isWinXP() && pPrinter.PrintProcessor != winxpPrinterProcessor)
  {
    Console.WriteLine("Oh No, the printer exists, but the processor isn't XPS. Setting now. It's currently " + pPrinter.PrintProcessor);
    pPrinter.PrintProcessor = winxpPrinterProcessor;
    Console.WriteLine("Set the processor to " + winxpPrinterProcessor);
    if (updateOnly)
    {
      pMaster.PrinterSet(pPrinter);
    } else { //else we're adding
      pMaster.PrinterAdd(pPrinter);
    }
当自己运行程序时,通过双击,它可以完美地工作。当作为MSI自定义操作运行时,它不起作用。除了设置打印处理器外,一切正常(安装打印机、驱动程序、设置端口)。Windows只是忽略处理器设置

MSI作为系统用户启动自定义操作(这是一个控制台应用程序)进程。当我手动启动它时,它在我的域管理员帐户下运行

我还应该注意,手动安装打印机也可以正常工作,因为xp从INF文件获取处理器。当使用prnadmin dll时,它会忽略该设置


我度过了一个令人沮丧的早晨。有什么想法吗?或者更好的安装打印机的方法?

好吧,我从来没有弄清楚为什么在这种情况下它会失败,但我确实找到了更好的解决方法。如果有人能回答我最初的问题,我会给你答案

背景:我们选择使用prnadmin包装器做所有事情,因为我们必须创建一个端口,而这似乎是最简单的方法

我们现在不再使用托管代码创建打印机,而是向PrintUI输出命令。我们可以使用PrintUI设置端口,windows XP将在使用PrintUI时选择INF文件中指定的处理器,因此它一举两得

public static string PrinterDriverName = "MyPrinter.INF";
public static string portOutputFileName = "nul:";

/// <summary>
/// Installs a printer and driver from an INF file, setting the 
/// port to portOutputFileName ('nul:' in this case)
/// </summary>
private static void CreatePrinterPrintUI()
{
  string sCommand = String.Format(@"rundll32 printui,PrintUIEntry /if /K /m '{0}' /n '{0}' /r '{2}' /f '{1}'"
    ,PrinterDriverName,Path.Combine(Application.StartupPath,PrinterDriverInf)
    ,portOutputFileName).Replace("'","\""); //it fails on single quotes
  execCmd(sCommand);
}

public static void execCmd(string _sCmd)
{
  try
  {
    System.Diagnostics.ProcessStartInfo p = new ProcessStartInfo("cmd", "/c " + _sCmd);
    p.RedirectStandardOutput = true;
    p.UseShellExecute = false;
    p.CreateNoWindow = true;
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = p;
    proc.Start();
    string sResult = proc.StandardOutput.ReadToEnd();
    Console.WriteLine(sResult);
  }
  catch (Exception e)
  {
    sErr += e.ToString();
  }
}
公共静态字符串PrinterDriverName=“MyPrinter.INF”; 公共静态字符串portOutputFileName=“nul:”; /// ///从INF文件安装打印机和驱动程序,设置 ///端口到端口输出文件名(本例中为nul:) /// 私有静态void CreatePrinterPrintUI() { string sCommand=string.Format(@“rundll32 printui,printuitery/if/K/m'{0}'/n'{0}'/r'{2}'/f'{1}' ,PrinterDriverName,Path.Combine(Application.StartupPath,PrinterDriverInf) ,portOutputFileName)。替换(“'”,“\”);//在单引号上失败 execCmd(sCommand); } 公共静态void execCmd(字符串_sCmd) { 尝试 { System.Diagnostics.ProcessStartInfo p=新的ProcessStartInfo(“cmd”,“c”+_sCmd); p、 重定向标准输出=真; p、 UseShellExecute=false; p、 CreateNoWindow=true; System.Diagnostics.Process proc=新的System.Diagnostics.Process(); proc.StartInfo=p; proc.Start(); 字符串sResult=proc.StandardOutput.ReadToEnd(); 控制台写入线(sResult); } 捕获(例外e) { sErr+=e.ToString(); } }
我想知道PrintUI.dll是否可以做到这一点,如果有任何这方面的提示,我将不胜感激。由于这个怪癖,“托管”代码版本无法使用,这真是令人沮丧。