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

📄 appendixa.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
on the order of a thousand functions, constants, and data types. Of course, you
do not want to write the Java equivalent of every single Win32 API function.
Microsoft took care of this, distributing a Java package that maps the Win32 API
to Java classes using J/Direct. This package, named
<A NAME="Index3090"></A><B>com.ms.win32</B>, is installed in your classpath
during the installation of the Java SDK 2.0 if you select it in the setup
options. The package is made up of large number of Java classes that reproduce
the constants, data structures, and functions of the Win32 API. The three
richest classes are <B>User32.class</B>, <B>Kernel32.class</B>, and
<B>Gdi32.class</B>. These contain the core of the Win32 API. To use them, just
import them in your Java code. The <B>ShowMsgBox</B> example above can be
rewritten using <B>com.ms.win32</B> as follows (I also took care of the
<B>UnsatisfiedLinkError</B> in a more civilized way):</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> com.ms.win32.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> UseWin32Package {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String args[]) {
    <font color=#0000ff>try</font> {
      User32.MessageBeep(
        winm.MB_ICONEXCLAMATION);
      User32.MessageBox(0,
        <font color=#004488>"Created by the MessageBox() Win32 func"</font>,
        <font color=#004488>"Thinking in Java"</font>,
        winm.MB_OKCANCEL |
        winm.MB_ICONEXCLAMATION);
    } <font color=#0000ff>catch</font>(UnsatisfiedLinkError e) {
      System.out.println(<font color=#004488>"Can&#8217;t link Win32 API"</font>);
      System.out.println(e);
    }
  }
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The package is imported in the
first line. The <B>MessageBeep(&#160;)</B> and <B>MessageBox(&#160;)</B>
functions can now be called with no other declarations. In
<B>MessageBeep(&#160;) </B>you can see that importing the package has also
declared the Win32 constants. These constants are defined in a number of Java
interfaces, all named winx (x is the first letter of the constant you want to
use). </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At the time of this writing, the
classes in the <B>com.ms.win32</B> package are still under development, but
usable nonetheless.</FONT><A NAME="_Toc408018829"></A><BR></P></DIV>
<A NAME="Heading601"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Marshaling<BR><A NAME="Index3091"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Marshaling means converting a
function argument from its native binary representation into some
language-independent format, and then converting this generic representation
into a binary format that is appropriate to the called function. In the example
above, we called the <B>MessageBox(&#160;)</B> function and passed it a couple
of <B>String</B>s. <B>MessageBox(&#160;)</B> is a C function, and the binary
layout of Java <B>String</B>s is not the same as C strings, but the arguments
are nonetheless correctly passed. That&#8217;s because J/Direct takes care of
converting a Java <B>String</B> into a C string before calling the C code. This
happens with all standard Java types. Below is a table of the implicit
conversions for simple data types:</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Java</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>C</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>byte</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BYTE or CHAR</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>short</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">SHORT or WORD</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>int</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">INT, UINT, LONG, ULONG, or
DWORD</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>char</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">TCHAR</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>long</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">__int64</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>float</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Float</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>double</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Double</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>boolean</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BOOL</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>String</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">LPCTSTR (Allowed as return value
only in ole mode)</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>byte[]</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">BYTE *</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>short[]</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">WORD *</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>char[]</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">TCHAR *</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>int[]</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">DWORD *</FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The list continues, but this gives
you the idea. In most cases, you do not need to worry about converting to and
from simple data types, but things are different when you must pass arguments of
user-defined data types. For example, you might need to pass the address of a
structured, user-defined data type, or you might need to pass a pointer to a raw
memory area. For these situations, there are special compiler directives to mark
a Java class so that it can be passed as a pointer to a structure (the
<A NAME="Index3092"></A><B>@dll.struct</B> directive). For details on the use of
these keywords, please refer to the product
documentation.</FONT><A NAME="_Toc408018830"></A><BR></P></DIV>
<A NAME="Heading602"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Writing callback functions</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Some Win32 API functions require a
function pointer as one of the parameters. The Windows API function may then
call the argument function, possibly at a later time when some event occurs.
This technique is called a <A NAME="Index3093"></A><I>callback function</I>.
Examples include window procedures and the callbacks you set up during a print
operation (you give the print spooler the address of your callback function so
it can update the status and possibly interrupt printing).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Another example is the
<B>EnumWindows(&#160;)</B> API function that enumerates all top-level windows
currently present in the system. <B>EnumWindows(&#160;)</B> takes a function
pointer, then traverses a list maintained internally by Windows. For every
window in the list, it calls the callback function, passing the window handle as
an argument to the callback.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To do the same thing in Java, you
must use the <B>Callback </B>class in the <B>com.ms.dll</B> package. You inherit
from <B>Callback </B>and override <B>callback(&#160;)</B>. This method will
accept only <B>int</B> parameters and will return <B>int</B> or <B>void</B>. The
method signature and implementation depends on the Windows API function
that&#8217;s using this callback.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now all we need to do is create an
instance of this <B>Callback</B>-derived class and pass it as the function
pointer argument to the API function. J/Direct will take care of the
rest.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The example below calls the
<B>EnumWindows(&#160;)</B> Win32 API; the <B>callback(&#160;)</B> method in the
EnumWindowsProc class gets the window handle for each top-level window, obtains
the caption text, and prints it to the console window.</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> com.ms.dll.*;
<font color=#0000ff>import</font> com.ms.win32.*;

<font color=#0000ff>class</font> EnumWindowsProc <font color=#0000ff>extends</font> Callback {
  <font color=#0000ff>public</font> <font color=#0000ff>boolean</font> callback(<font color=#0000ff>int</font> hwnd, <font color=#0000ff>int</font> lparam) {
    StringBuffer text = <font color=#0000ff>new</font> StringBuffer(50);
    User32.GetWindowText(
      hwnd, text, text.capacity()+1);
    <font color=#0000ff>if</font>(text.length() != 0)
      System.out.println(text);
    <font color=#0000ff>return</font> <font color=#0000ff>true</font>;  <font color=#009900>// to continue enumeration.</font>
  }

⌨️ 快捷键说明

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