Last active
March 21, 2020 15:22
-
-
Save fishi0x01/51f6b1b2ea81f112a3ea to your computer and use it in GitHub Desktop.
Basic j-interop DCOM bridge setup snippets. Code for blog post https://fishi.devtail.io/weblog/2015/01/21/pure-java-dcom-bridge-j-interop/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Code snippet that tries to create a connection with the given wmiLocator object to the given namespace. | |
* @return An IJIDispatch object that can be used for WQL queries to the given namesapce of the target machine. | |
* @Exception In case the ConnectServer method could not be properly executed. | |
*/ | |
public IJIDispatch createNamespaceServer(IJIDispatch wmiLocator, String nameSpace, String ipAddress) throws Exception | |
{ | |
// create an instance of the WbemScripting.SWbemLocator object to talk to WMI on the target machine. | |
// namespace could be for instance "root\\cimv2" | |
JIVariant results[] = | |
bridge.getWbemLocator().callMethodA("ConnectServer", new Object[] { new JIString(ipAddress), new JIString(nameSpace), | |
JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), new Integer(0), | |
JIVariant.OPTIONAL_PARAM()}); | |
IJIDispatch wbemService = (IJIDispatch) JIObjectFactory.narrowObject(results[0].getObjectAsComObject()); | |
return wbemService; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Code snippet for creating a WbemScripting.SWbemLocator JIComServer through which queries to the WMI machine can be made. | |
* This method will only work, if a JISession is already established. | |
* @return A IJIDispatch object through which represents the WbemScripting.SWbemLocator class on the target machine. | |
* @throws Exception if WbemScripting.SWbemLocator could not be created | |
*/ | |
private IJIDispatch createWmiLocator(JISession dcomSession) throws Exception | |
{ | |
JIComServer wbemLocatorComObj = new JIComServer(JIClsid.valueOf("76A64158-CB41-11D1-8B02-00600806D9B6"), MyDefaults.IP_ADDR, dcomSession); | |
IJIDispatch wmiLocator = (IJIDispatch) JIObjectFactory.narrowObject(wbemLocatorComObj.createInstance().queryInterface(IJIDispatch.IID)); | |
return wmiLocator; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Code snippet for establishing a DCOM Session (JISession) which is the initial step in creating DCOM bridge | |
* @return A functional DCOM session | |
* @throws Exception if the DCOM session could not be created | |
*/ | |
private JISession createDComSession(String domain, String user, String password) throws Exception | |
{ | |
/** | |
* Please note that the user must have administrator permissions on the target Windows machine | |
*/ | |
JISession dcomSession = JISession.createSession(domain, user, password); | |
/** | |
* NTLM1 config | |
*/ | |
dcomSession.useSessionSecurity(true); | |
/** | |
* Set the socket timeout for connections in this session. | |
* This is very important - in case the other end shuts down, | |
* infinite blocking may occur... | |
*/ | |
dcomSession.setGlobalSocketTimeout(MyDefaults.DCOM_SOCKET_TO); | |
return dcomSession; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Code snippet for querying Disk IO performance from target machine and print to output. | |
* @Exception In case something goes wrong during query processing | |
*/ | |
public void printDiskIO(IJIDispatch wbemService) throws Exception | |
{ | |
/** | |
* The WQL query. | |
* WMI Classes are stored in a table. | |
*/ | |
final String QUERY = "SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk"; | |
/** | |
* Flags for the query. | |
*/ | |
final int RETURN_IMMEDIATE = 16; | |
final int FORWARD_ONLY = 32; | |
/** | |
* Execute the query | |
*/ | |
JIVariant[] sourceSet = wbemService.callMethodA("ExecQuery", new Object[] { new JIString(QUERY), new JIString("WQL"), new JIVariant(new Integer(RETURN_IMMEDIATE + FORWARD_ONLY))}); | |
IJIDispatch wbemSource = (IJIDispatch) JIObjectFactory.narrowObject((sourceSet[0]).getObjectAsComObject()); | |
IJIComObject enumSet = wbemSource.get("_NewEnum").getObjectAsComObject(); | |
IJIEnumVariant enumVariant = (IJIEnumVariant) JIObjectFactory.narrowObject(enumSet.queryInterface(IJIEnumVariant.IID)); | |
/** | |
* Process the result | |
*/ | |
Object[] values = enumVariant.next(1); | |
JIArray array = (JIArray) values[0]; | |
Object[] arrayObj = (Object[])array.getArrayInstance(); | |
/** | |
* Loop through each found disk | |
*/ | |
for (int j = 0; j < arrayObj.length; j++) | |
{ | |
IJIComObject procComObj = ((JIVariant)arrayObj[j]).getObjectAsComObject(); | |
IJIDispatch wbemProcObj = (IJIDispatch) JIObjectFactory.narrowObject(procComObj); | |
/** | |
* Print all the info from the current disk | |
*/ | |
String txt = getObjectResults.getObjectAsString().getString(); | |
System.out.println(txt); | |
System.out.println(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Code snippet for listening to events from Windows Event Viewer that have the given event code. | |
* Print the information from each received event to the output. | |
*/ | |
public void listen(IJIDispatch wbemService, String eventCode) | |
{ | |
final String QUERY_FOR_ALL_LOG_EVENTS = "SELECT\ *\ FROM\ __InstanceCreationEvent\ WITHIN\ 5\ WHERE\ TargetInstance\ ISA\ 'Win32_NTLogEvent' AND TargetInstance.EventCode = '"+eventCode+"'"; | |
final int RETURN_IMMEDIATE = 16; | |
final int FORWARD_ONLY = 32; | |
/** | |
* Subscribe. | |
*/ | |
JIVariant[] eventSourceSet = wbemService.callMethodA("ExecNotificationQuery", new Object[] { new JIString(QUERY_FOR_ALL_LOG_EVENTS), new JIString("WQL"), new JIVariant( new Integer(RETURN_IMMEDIATE + FORWARD_ONLY))}); | |
IJIDispatch wbemEventSource = (IJIDispatch) JIObjectFactory.narrowObject((eventSourceSet[0]).getObjectAsComObject()); | |
/** | |
* Start the listening loop | |
*/ | |
while(true) | |
{ | |
/** | |
* Note, this blocks until an event as specified by the query occurs! | |
*/ | |
JIVariant eventAsVariant = (JIVariant) (wbemEventSource.callMethodA("NextEvent", new Object[] {JIVariant.OPTIONAL_PARAM()}))[0]; | |
IJIDispatch wbemEvent = (IJIDispatch) JIObjectFactory.narrowObject(eventAsVariant.getObjectAsComObject()); | |
/** | |
* Process the received event | |
*/ | |
IJIComObject eventCom = eventAsVariant.getObjectAsComObject(); | |
IJIDispatch eventProcObj = (IJIDispatch) JIObjectFactory.narrowObject(eventCom); | |
JIVariant evtResult = (JIVariant) (eventProcObj.get("_Class")); | |
String classIdentifier = evtResult.getObjectAsString2(); | |
System.out.println("[Class Identifier]\t"+classIdentifier); | |
JIVariant objTextAsVariant = (JIVariant) (wbemEvent.callMethodA("GetObjectText_", new Object[] {new Integer(1)}))[0]; | |
String asText = objTextAsVariant.getObjectAsString().getString(); | |
System.out.println(asText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment