Difference between revisions of "Call COM object"

From CodeCodex

(Java)
 
(Implementations)
 
Line 5: Line 5:
 
First generate Java type definitions from a COM type library. Here we are doing for the type library for the Windows Scripting Host.  
 
First generate Java type definitions from a COM type library. Here we are doing for the type library for the Windows Scripting Host.  
  
<highlightsyntax language="java122">
+
<pre class="java">
 
> java -jar tlbimp.jar -o wsh -p test.wsh %WINDIR%\system32\wshom.ocx
 
> java -jar tlbimp.jar -o wsh -p test.wsh %WINDIR%\system32\wshom.ocx
 
+
</pre>
</highlightsyntax>
+
 
Then we are able to call WSH objects/methods.  
 
Then we are able to call WSH objects/methods.  
<highlightsyntax language="java122">
+
<pre class="java">
 
public class Main {
 
public class Main {
 
   public static void main(String[] args) {
 
   public static void main(String[] args) {
Line 19: Line 18:
 
}
 
}
  
</highlightsyntax>
+
</pre>
 
https://com4j.dev.java.net/  
 
https://com4j.dev.java.net/  
  
Line 30: Line 29:
 
http://sourceforge.net/projects/j-xchange/  
 
http://sourceforge.net/projects/j-xchange/  
  
 
+
=== Tcl ===
 +
<pre class="tcl">
 +
# Call Excel COM object, open an xls file and get the content of C7
 +
package require tcom
 +
set app [::tcom::ref createobject "Excel.Application"]
 +
set app [::tcom::ref getobject "C:/test.xls"]
 +
set ws [$app Worksheets]
 +
set xlCellTypeLastCell 11
 +
set cells [$ws Cells]
 +
set C7 [[$cells Item 7 C] -get Value]
 +
</pre>
 +
<pre class="tcl">
 +
# Call Internet Explorer COM object and go to a web site
 +
package require tcom
 +
set ie [::tcom::ref createobject InternetExplorer.Application.1]
 +
$ie Visible True
 +
$ie Navigate "http://www.tcl.tk/"
 +
</pre>
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
  
 
See also this [http://www.rgagnon.com/javadetails/java-0516.html HowTo] for an alternative package to access a COM package from Java.  
 
See also this [http://www.rgagnon.com/javadetails/java-0516.html HowTo] for an alternative package to access a COM package from Java.  
[[Category:Java]]              
+
[[Category:Java]] | [[Category:Tcl]]         
  
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------

Latest revision as of 17:23, 17 February 2011

Implementations[edit]

Java[edit]

com4j A Java library that allows Java applications to seemlessly interoperate with Microsoft Component Object Model. First generate Java type definitions from a COM type library. Here we are doing for the type library for the Windows Scripting Host.

> java -jar tlbimp.jar -o wsh -p test.wsh %WINDIR%\system32\wshom.ocx

Then we are able to call WSH objects/methods.

public class Main {
  public static void main(String[] args) {
    IFileSystem3 fs = ClassFactory.createFileSystemObject();
    for( String file : args )
      System.out.println(fs.getFileVersion(file));
  }
}

https://com4j.dev.java.net/

j-interop Implementation of DCOM wire protocol (MSRPC) to enable development of Pure Bi-Directional, Non-Native Java applications which can interoperate with any COM component. The implementation is itself purely in Java and does not use JNI to provide COM access. http://sourceforge.net/projects/j-interop

j-xchange Pure java implementation of the entire Collaboration Data Objects (CDO 1.21) library for accessing Microsoft Exchange Server in a platform independent manner. http://sourceforge.net/projects/j-xchange/

Tcl[edit]

# Call Excel COM object, open an xls file and get the content of C7
package require tcom
set app [::tcom::ref createobject "Excel.Application"]
set app [::tcom::ref getobject "C:/test.xls"]
set ws [$app Worksheets]
set xlCellTypeLastCell 11
set cells [$ws Cells]
set C7 [[$cells Item 7 C] -get Value]
# Call Internet Explorer COM object and go to a web site
package require tcom
set ie [::tcom::ref createobject InternetExplorer.Application.1]
$ie Visible True
$ie Navigate "http://www.tcl.tk/"

See also this HowTo for an alternative package to access a COM package from Java. |