Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I programatically connect to a printer located at a different network if I have the credentials to a user account with admin previliages on the server to which the printer is connected?

Tried the following code:
try
{
    ConnectionOptions connection = new ConnectionOptions();
    connection.Username = "username";
    connection.Password = "password";
    connection.Authority = "ntlmdomain:DOMAINNAME";
    connection.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope scope = new ManagementScope("\\\\Servers' public ip\\root\\CIMV2", connection);
    scope.Connect();

    ManagementObject classInstance =
        new ManagementObject(scope,
        new ManagementPath("Win32_Printer.DeviceID='\\\\Servers' public ip\\Printer name'"),
        null);

    // no method in-parameters to define


    // Execute the method and obtain the return values.
    ManagementBaseObject outParams =
        classInstance.InvokeMethod("PrintTestPage", null, null);

    // List outParams
    Console.WriteLine("Out parameters:");
    Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);


}
catch (ManagementException err)
{
    Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.ErrorCode);
}
catch (System.UnauthorizedAccessException unauthorizedErr)
{
    Console.WriteLine("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
}


This one is generated using WMI Code Creator, but the following Exception pops up:
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

Tried out setting the inbound rules as explained in the following link: http://technet.microsoft.com/en-us/library/jj717250.aspx, but availed no result. Any and all help is greatly appreciated.

Thank you.
Posted
Updated 7-Oct-13 2:57am
v4

1 solution

 
Share this answer
 
Comments
blackninja7 9-Oct-13 2:41am    
Thanks for the reply. I was very well able to print from a local printer or a locally networked printer, but the issue relates to remote printing from a totally different network, where all I am having are the public ip and user credentials of the server to which the Printer is connected. Moreover, this seemed like an RPC/Security related conflict from the server side. A little bit more of browsing revealed the usage of Internet Printing Protocol to be a better option to be worth consideration in this context (http://h30565.www3.hp.com/t5/Feature-Articles/3-Ways-to-Print-Over-the-Internet/ba-p/4654), even though I am still wondering if there are any wrappers classes or APIs that can be used in C# for that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900