⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0195.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
just uses ProgIDs or CLSIDs. In other words, the Registry allows for location
transparency of the server code. With the introduction of DCOM (Distributed
COM), a server that was running on a local machine can even be moved to a
remote machine on the network, without the client even noticing it (well,
almost...).
</FONT><P></DIV>
<A NAME="Heading610"></A><H4 ALIGN=LEFT>
Type
Libraries
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Because
of COM&#8217;s dynamic linking and the independent evolution of client and
server code, the client always needs to dynamically detect the services that
are exposed by the server. These services are described in a binary,
language-independent way (as interfaces and method signatures) in the <A NAME="Index3112"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>type
library
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This can be a separate file (usually with the .TLB extension), or a Win32
resource linked into the executable. At runtime, the client uses the
information in the type library to call functions in the server.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can generate a type library by writing a Microsoft Interface Definition
Language (MIDL) source file and compiling it with the MIDL compiler to generate
a .TLB file. MIDL is a language that describes COM classes, interfaces, and
methods. It resembles the OMG/CORBA IDL in name, syntax, and purpose. The Java
programmer has no need to use MIDL, though. A different Microsoft tool,
described later, reads a Java class file and generates a type library.
</FONT><P></DIV>
<A NAME="Heading611"></A><H4 ALIGN=LEFT>
Function
return codes in COM: HRESULT
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">COM
functions exposed by a server return a value of the predefined type HRESULT. An <A NAME="Index3113"></A>HRESULT
is an integer containing three fields. This allows for multiple failure and
success codes, along with additional information. Because a COM function
returns an HRESULT, you cannot use the return value to hand back ordinary data
from the function call. If you must return data, you pass a pointer to a memory
area that the function will fill. This is known as an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>out
parameter
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You don&#8217;t need to worry about this as a Java/COM programmer since the
virtual machine takes care of it for you. This is described in the following
sections.
</FONT><a name="_Toc408018836"></a><P></DIV>
<A NAME="Heading612"></A><H3 ALIGN=LEFT>
MS
Java/COM Integration
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
Microsoft Java compiler, Virtual Machine, and tools make life a lot easier for
the Java/COM programmer than it is for the C++/COM programmer. The compiler has
special directives and packages for treating Java classes as COM classes, but
in most cases, you&#8217;ll just rely on the Microsoft JVM support for COM, and
on a couple of external tools.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
Microsoft Java Virtual Machine acts as a bridge between COM and Java objects.
If you create a Java object as a COM server, your object will still be running
inside the JVM. The Microsoft JVM is implemented as a DLL, which exposes COM
interfaces to the operating system. Internally, the JVM maps function calls to
these COM interfaces to method calls in your Java objects. Of course, the JVM
must know which Java class file corresponds to the server executable; it can
discover this information because you previously registered the class file in
the Windows Registry using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javareg</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
a utility in the Microsoft Java SDK. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javareg</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
reads a Java class file, generates a corresponding type library and a GUID, and
registers the class in the system. <A NAME="Index3114"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javareg</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be used to register remote servers as well, for example, servers that run
on a different physical machine.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you want to write a Java/COM client, you must go through a different process. A
Java/COM client is Java code that wants to activate and use one of the COM
servers registered on your system. Again, the virtual machine interfaces with
the COM server and exposes its services as methods in a Java class. Another
Microsoft tool, <A NAME="Index3115"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jactivex</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
reads a type library and generates Java source files that contain special
compiler directives. The generated source files are part of a package named
after the type library you specified. The next step is to import that package
in your COM client Java source files.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Let&#8217;s
look at a couple of examples.
</FONT><a name="_Toc408018837"></a><P></DIV>
<A NAME="Heading613"></A><H3 ALIGN=LEFT>
Developing
COM servers in Java
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
section shows the process you will apply to the development of ActiveX
Controls, Automation Servers, or any other COM-compliant server. The following
example implements a simple Automation server that adds integer numbers. You
set the value of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>addend</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setAddend(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, and each time you call the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>sum(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>addend</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is added to the current 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>result</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You retrieve the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>result</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getResult(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and reset the values with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clear(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The Java class that implements this behavior is straightforward:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">public</font> <font color="#0000ff">class</font> Adder {
  <font color="#0000ff">private</font> <font color="#0000ff">int</font> addend;
  <font color="#0000ff">private</font> <font color="#0000ff">int</font> result;
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> setAddend(<font color="#0000ff">int</font> a) { addend = a; }
  <font color="#0000ff">public</font> <font color="#0000ff">int</font> getAddend() { <font color="#0000ff">return</font> addend; }
  <font color="#0000ff">public</font> <font color="#0000ff">int</font> getResult() { <font color="#0000ff">return</font> result; }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> sum() { result += addend;  }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> clear() {
    result = 0;
    addend = 0;
  }
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
use this Java class as a COM object, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javareg</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
tool is applied to the compiled 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Adder.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file. This tool has a number of options; in this case we specify the Java class
file name (&#8220;Adder&#8221;), the ProgID we want to put in the Registry for
this server (&#8220;JavaAdder.Adder.1&#8221;), and the name we want for the
type library that will be generated (&#8221;JavaAdder.tlb&#8221;). Since no
CLSID is given, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javareg</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will generate one; if we call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javareg</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
again on the same server, the existing CLSID will be used.
</FONT><P></DIV>

<font color="#990000"><PRE>javareg /register
/<font color="#0000ff">class</font>:Adder /progid:JavaAdder.Adder.1
/typelib:JavaAdder.tlb</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Javareg</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
also registers the new server in the Windows Registry. At this point, you must
remember to copy your 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Adder.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file into the Windows\Java\trustlib directory. For security reasons, related
mostly to the use of COM services by applets, your COM server will be activated
only if it is installed in the trustlib directory.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
now have a new Automation server installed on your system. To test it, you need
an Automation controller, and &#8220;the&#8221; Automation Controller is <A NAME="Index3116"></A>Visual
Basic (VB). Below, you can see a few lines of VB code. On the VB form, I put a
text box to input the value of the addend, a label to show the result, and two
push buttons to invoke the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>sum(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clear(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods. At the beginning, an object variable named 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Adder</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is declared. In the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Form_Load</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
subroutine, executed when the form is first displayed, a new instance of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Adder</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
automation server is instantiated and the text fields on the form are
initialized. When the user presses the &#8220;Sum&#8221; or &#8220;Clear&#8221;
buttons, appropriate methods in the server are invoked.
</FONT><P></DIV>

<font color="#990000"><PRE>Dim Adder As Object

Private Sub Form_Load()
    Set Adder = CreateObject("JavaAdder.Adder.1")
    Addend.Text = Adder.getAddend
    Result.Caption = Adder.getResult
End Sub

Private Sub SumBtn_Click()
    Adder.setAddend (Addend.Text)
    Adder.Sum
    Result.Caption = Adder.getResult
End Sub

Private Sub ClearBtn_Click()
    Adder.Clear
    Addend.Text = Adder.getAddend
    Result.Caption = Adder.getResult
End Sub </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -