Skip to main content

Unable to Retrieving the Default printer of a remote PC using the com4j

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
moujab
Offline
Joined: 2011-10-26

Dear all,

I want to know the default printer of a remote pc that I only know it's IP, so after google-ing a bit i found that com4j might do the job. I wrote the bellow attached java code that might do the trick, but i keep getting the message:

Exception in thread "main" com4j.ComException: 80041003 (Unknown error) : Access denied : .\invoke.cpp:517

at com4j.Wrapper.invoke(Wrapper.java:166)

at $Proxy12.execMethod_(Unknown Source)

at GetPrinter.PrintDefaultPrinter.getDefaultWindowsPrinter(PrintDefaultPrinter.java:67)

at GetPrinter.PrintDefaultPrinter.main(PrintDefaultPrinter.java:208)

Caused by: com4j.ComException: 80041003 (Unknown error) : Access denied : .\invoke.cpp:517

at com4j.Native.invoke(Native Method)

at com4j.StandardComMethod.invoke(StandardComMethod.java:42)

at com4j.Wrapper$InvocationThunk.call(Wrapper.java:335)

at com4j.Task.invoke(Task.java:51)

at com4j.ComThread.run0(ComThread.java:153)

at com4j.ComThread.run(ComThread.java:134)

I browsed the internet searching for a solution to the problem. All suggested that i should use an user that have administrator profile, which i did and it was the domain administrator of the Active directory, still no success.

The only case that the program was able to retrieve the default printer was when i ran the program after supplying the same user name and password of the user that is logged in the remote PC

Any hint is highly appreciated

The Java Code

package GetPrinter;

import java.util.StringTokenizer;

import wmi.ClassFactory;

import wmi.ISWbemLocator;

import wmi.ISWbemMethod;

import wmi.ISWbemObject;

import wmi.ISWbemObjectSet;

import wmi.ISWbemPropertySet;

import wmi.ISWbemServices;

import com4j.Com4jObject;

public class PrintDefaultPrinter {

public static String getDefaultWindowsPrinter(

String computerIP,

String userName,

String passWord,

String Domain)

{

String returnValue = new String ("");

int HKEY_CURRENT_USER = 0x80000001;

String ROOT = "Software\\Microsoft\\Windows NT\\CurrentVersion";

ISWbemLocator wbemLocator = ClassFactory.createSWbemLocator();

System.out.println("============== Connect to remote PC "+computerIP);

ISWbemServices wbemServices = wbemLocator.connectServer(

computerIP,

"Root\\default",

userName,

passWord,

"",

Domain,

0,

null);

ISWbemObject wbemObject = wbemServices.get("StdRegProv", 0, null);

System.out.println("wbemObject " + wbemObject.getObjectText_(0));

System.out.println("============== get the subkey Value");

ISWbemMethod method2 = wbemObject.methods_("GetStringValue", 0);

ISWbemMethod method3 = wbemObject.methods_("CheckAccess", 0);

ISWbemObject ip2 = method2.inParameters().spawnInstance_(0);

ISWbemPropertySet ipp2 = ip2.properties_();

ipp2.item("hDefKey", 0).value(HKEY_CURRENT_USER);

ipp2.item("sSubKeyName", 0).value(ROOT+"\\Windows");

ipp2.item("sValueName", 0).value("Device");

line 67==> ISWbemObject result2 = wbemObject.execMethod_

(

"GetStringValue",

ip2,

0,

method2.outParameters().spawnInstance_(0)

);

System.out.println("result2 " + result2.getObjectText_(0));

Object sValue = result2.properties_().item("sValue", 0).value();

System. out.println("sValue " + sValue.toString());

System.out.println("========== get the printer port");

StringTokenizer st = new StringTokenizer(sValue.toString(),",");

System.out.println("numberOfTokens: "+st.countTokens());

String Token1 = new String(st.nextToken());

System.out.println("Token1 "+Token1);

// query - what is the default printer?

ISWbemServices wbemServices1 = wbemLocator.connectServer(computerIP,"Root\\CIMv2","","","","",0,null);

System.out.println("Listing default printer");

ISWbemObjectSet result3 = wbemServices1.execQuery("Select Caption,PortName from Win32_Printer Where Caption=\""+Token1+"\"" ,"WQL",16,null);

for( Com4jObject obj : result3 )

{

ISWbemObject wo = obj.queryInterface(ISWbemObject.class);

System.out.println(wo.getObjectText_(0).toString());

StringBuffer stb = new StringBuffer(wo.getObjectText_(0).toString());

int point1 = stb.indexOf("PortName = ");

System.out.println("point1 " + point1);

String st1 = stb.substring(point1);

System.out.println("st1 " + st1);

int point2 = st1.indexOf("\";");

System.out.println("point2 " + point2);

String st2 = st1.substring(0,point2);

System.out.println("st2 " + st2);

int point3 = st2.indexOf("\"");

System.out.println("point3 " + point3);

String st3 = st2.substring(point3+4);

System.out.println("Default Printer Ip is " + st3);

returnValue = st3;

}

return returnValue;

}

public static String getDefaultWindowsPrinter(

String computerIP )

{

String returnValue = new String ("");

int HKEY_CURRENT_USER = 0x80000001;

String ROOT = "Software\\Microsoft\\Windows NT\\CurrentVersion";

ISWbemLocator wbemLocator = ClassFactory.createSWbemLocator();

System.out.println("============== Connect to remote PC "+computerIP);

ISWbemServices wbemServices = wbemLocator.connectServer(

computerIP,

"Root\\default",

"",

"",

"",

"",

0,

null);

ISWbemObject wbemObject = wbemServices.get("StdRegProv", 0, null);

System.out.println("wbemObject " + wbemObject.getObjectText_(0));

System.out.println("============== get the subkey Value");

ISWbemMethod method2 = wbemObject.methods_("GetStringValue", 0);

ISWbemObject ip2 = method2.inParameters().spawnInstance_(0);

ISWbemPropertySet ipp2 = ip2.properties_();

ipp2.item("hDefKey", 0).value(HKEY_CURRENT_USER);

ipp2.item("sSubKeyName", 0).value(ROOT+"\\Windows");

ipp2.item("sValueName", 0).value("Device");

ISWbemObject result2 = wbemObject.execMethod_

(

"GetStringValue",

ip2,

0,

method2.outParameters().spawnInstance_(0)

);

System.out.println("result2 " + result2.getObjectText_(0));

Object sValue = result2.properties_().item("sValue", 0).value();

System.out.println("sValue " + sValue.toString());

System.out.println("========== get the printer port");

StringTokenizer st = new StringTokenizer(sValue.toString(),",");

System.out.println("numberOfTokens: "+st.countTokens());

String Token1 = new String(st.nextToken());

System.out.println("Token1 "+Token1);

// query - what is the default printer?

ISWbemServices wbemServices1 = wbemLocator.connectServer(computerIP,"Root\\CIMv2","","","","",0,null);

System.out.println("Listing default printer");

ISWbemObjectSet result3 = wbemServices1.execQuery("Select Caption,PortName from Win32_Printer Where Caption=\""+Token1+"\"" ,"WQL",16,null);

for( Com4jObject obj : result3 )

{

ISWbemObject wo = obj.queryInterface(ISWbemObject.class);

System.out.println(wo.getObjectText_(0).toString());

StringBuffer stb = new StringBuffer(wo.getObjectText_(0).toString());

int point1 = stb.indexOf("PortName = ");

System.out.println("point1 " + point1);

String st1 = stb.substring(point1);

System.out.println("st1 " + st1);

int point2 = st1.indexOf("\";");

System.out.println("point2 " + point2);

String st2 = st1.substring(0,point2);

System.out.println("st2 " + st2);

int point3 = st2.indexOf("\"");

System.out.println("point3 " + point3);

String st3 = st2.substring(point3+4);

System.out.println("Default Printer Ip is " + st3);

returnValue = st3;

}

return returnValue;

}

public static void main(String[] args) throws Exception {

if (args.length == 4) {

System.out.println("Type 1");

System.out.println(args[0]);

System.out.println(args[1]);

System.out.println(args[2]);

System.out.println(args[3]);

System.out.println("Default Printer IP: " + getDefaultWindowsPrinter(args[0],args[1],args[2],args[3]));}

else if (args.length == 1) {

System.out.println("Type 2");

System.out.println(args[0]);

System.out.println("Default Printer IP: " + getDefaultWindowsPrinter(args[0]));}

else

}}