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

📄 tij0193.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>byte[]</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">BYTE
*
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>short[]</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">WORD
*
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char[]</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">TCHAR
*
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int[]</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">DWORD
*
</FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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="Index3104"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>@dll.struct</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
directive). For details on the use of these keywords, please refer to the
product documentation.
</FONT><a name="_Toc408018830"></a><P></DIV>
<A NAME="Heading603"></A><H3 ALIGN=LEFT>
Writing
callback functions
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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="Index3105"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>callback
function
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Another
example is the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>EnumWindows(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
API function that enumerates all top-level windows currently present in the
system. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>EnumWindows(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
do the same thing in Java, you must use the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Callback
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">class
in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.ms.dll</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
package. You inherit from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Callback
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
override 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>callback(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This method will accept only 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
parameters and will return 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>void</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The method signature and implementation depends on the Windows API function
that&#8217;s using this callback.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Now
all we need to do is create an instance of this 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Callback</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">-derived
class and pass it as the function pointer argument to the API function.
J/Direct will take care of the rest.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
example below calls the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>EnumWindows(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
Win32 API; the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>callback(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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><P></DIV>

<font color="#990000"><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>
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> ShowCallback {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String args[])
  <font color="#0000ff">throws</font> InterruptedException {
    <font color="#0000ff">boolean</font> ok = User32.EnumWindows(
      <font color="#0000ff">new</font> EnumWindowsProc(), 0);
    <font color="#0000ff">if</font>(!ok)
      System.err.println("EnumWindows failed.");
    Thread.currentThread().sleep(3000);
  }
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
call to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>sleep(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
allows the windows procedure to complete before 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
exits.
</FONT><a name="_Toc408018831"></a><P></DIV>
<A NAME="Heading604"></A><H3 ALIGN=LEFT>
Other
J/Direct features
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are two more J/Direct features you can get using modifiers in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>@dll.import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
directive. The first is simplified access to OLE functions, and the second is
the selection of the ANSI versus Unicode version of API functions. Here is a
short description of the two.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">By
convention, all OLE functions return a value of type HRESULT, which is a
structured integer value defined by COM. If you program at the COM level and
you want something different returned from an OLE function, you must pass it a
pointer to a memory area that the function will fill with data. But in Java we
don&#8217;t have pointers; also, this style is not exactly elegant. With
J/Direct, you can easily call OLE functions using the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ole</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
modifier in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>@dll.import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
directive. A native method marked as an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ole</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
function is automatically translated from a Java-style method signature, which
is where you decide the return type, into a COM-style function.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second feature selects between ANSI and Unicode string handling. Most Win32 API
functions that handle strings come in two versions. For example, if you look at
the symbols exported by the USER32 DLL, you will not find a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBox(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
function, but instead 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBoxA(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBoxW(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
functions, which are the ANSI and Unicode version, respectively. If you do not
specify which version you want to call in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>@dll.import
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">directive,
the JVM will try to figure it out. But this operation takes some time during
program execution time that you can save with the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ansi</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>unicode,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>auto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
modifiers.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
a more detailed discussion of these features, consult the Microsoft
documentation.
</FONT><a name="_Toc408018832"></a><P></DIV>

<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0192.html">Prev</a> | <a href="tij0194.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

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