0

I am trying to install a virtual printer (for all versions of Windows) with a Windows desktop application. I want this printer to be available in the drop down list in the print dialog (especially from Office).

I am trying with this code:

public static void installPrinter(string printerName)
{
    string arg;

    arg = "printui.dll , PrintUIEntry /if /b " + "\"" + printerName + "\"" + @" /f C:\Windows\inf\ntprint.inf /r " + "\"" + "lpt1:" + "\"" + " /m " + "\"" + "Brother DCP-116C" + "\""; //initial args
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = "rundll32.exe";
    p.Arguments = arg;
    p.WindowStyle = ProcessWindowStyle.Hidden;

    try
    {
        Process.Start(p);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.InnerException.ToString());
    }
}

but it doesn't work. Any suggestions?

FranzHuber23
  • 3,311
  • 5
  • 24
  • 63
Tsal Giorgos
  • 390
  • 3
  • 16
  • 2
    What is the exception message? And are you sure this will/should run on every Windows version. From Windows 3.11 to Windows 10 with all the different APIs/frameworks? – Patric Apr 02 '19 at 11:06
  • 1
    "It doesn't work" is not a problem statement that we can assist you with. – Ian Kemp Apr 02 '19 at 11:27
  • 1
    @Patric There is no exception message, just the classic windows sound(the sound that makes when it pops up the Run as Admin). I am not sure about that, but I will be glad at least making it work at one version. Its my first time trying to do something like this and I am not sure how its working. Probably I have to embed the drivers to my project? Sorry for bad description but I have it indefinitely in my mind and my english are not the best. Thnx in advance – Tsal Giorgos Apr 02 '19 at 11:40
  • @IanKemp I apologize that perhaps my question is not just targeted, but is my first question and I am not so familiar with that. – Tsal Giorgos Apr 02 '19 at 11:43
  • Maybe you can have a look at https://stackoverflow.com/questions/818583/how-can-i-install-a-printer-using-net to get something started. And I guess you are developing in some kind of IDE with debugging so you should be able to produce some kind of error/exception message that will make it easier in helping you. – Patric Apr 02 '19 at 11:52
  • @Patric I am using visual studio 2017 community edition. As you see my code what should I add in order to understand where the error/exception is?I appreciate that you answer me – Tsal Giorgos Apr 02 '19 at 12:09
  • @Patric Hello Patric, here is the exception message I get: Error Received: Operation could not be completed (error 0x00000705). Windows cannot locate a suitable printer driver. Contact your administrator for help locating and installing a suitable driver. Manual install to check driver installation is successful. – Tsal Giorgos Apr 03 '19 at 06:31

1 Answers1

1

After a lot of research I made it work on win 7,8,8.1,10 on both x84 and x64 platforms (without any embeded drivers). Here is the code:

public static void installPrinter(string printerName) //works on win 7,8,8.1,10 on both x84 and x64
       {
           //https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rundll32-printui

           //  /if         Installs a printer by using an .inf file.
           //  /b[name]    Specifies the base printer name.
           //  /@[file]    Specifies a command-line argument file and directly inserts the text in that file into the command line.
           //  /f[file]    Species the Universal Naming Convention (UNC) path and name of the .inf file name or the output file name, depending on the task that you are performing. Use /F[file] to specify a dependent .inf file.
           //  /r[port]    Specifies the port name.
           //  /m[model]   Specifies the driver model name. (This value can be specified in the .inf file.)


           string arg;
           arg = "printui.dll , PrintUIEntry /if /b " + "\"" + printerName + "\"" + @" /f C:\Windows\inf\ntprint.inf /r " + "\"" + "lpt1:" + "\"" + " /m " + "\"" + "Generic / Text Only" + "\""; 
           ProcessStartInfo p = new ProcessStartInfo();
           p.FileName = "rundll32.exe";
           p.Arguments = arg;
           p.WindowStyle = ProcessWindowStyle.Hidden;

           try
           {
               Process.Start(p);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.InnerException.ToString());
           }
       }
Tsal Giorgos
  • 390
  • 3
  • 16