This sample is located in \Samples\Com\CustomMarshal\Point.
Note To use this sample, it is recommended that you have Microsoft® Visual C++® version 5.0 or higher installed.
Description
Using the Sample
Technologies Demonstrated
The previous custom marshaling examples showed JTYPEs as immutable. As a result, [out] parameters could only be handled by single element arrays; that is, to deal with immutable types, a caller must write code similar to the following:
{ double ad1[] = {0}; double ad2[] = {0}; func(ad1, ad2); System.out.println("func gave back: " + ad1[0] + ", " + ad2[0]); }
For immutable objects, the function:
HRESULT func([out] FIXED *, [out] FIXED *);
could not be usefully mapped to:
func(double d1, double d2);
This is because func() would have no way of returning information to the caller.
However, consider the Java class java.awt.Point (which represents a point in two-dimensional space.) The Point class is mutable, as its x and y fields are public and can be set by anyone. Therefore, it is advantageous to use the non-array form for passing [out] parameters. To do this requires only two new methods, copyToJava and toUninitJava.
The PointMarshaler class maps java.awt.Point to the Microsoft® Win32® POINT structure. The source code for PointMarshaler is in \Samples\Com\CustomMarshal\point. Copy the executables PointMarshaler.class and PointMarshaler.dll to the run location.
PointMarshaler can be used as shown in the following table.
COM type | Marshaled to Java as |
HRESULT func([in] POINT) | func(Point) |
HRESULT func([out,retval] POINT*) | Point func() |
HRESULT func([in] POINT*) | func(Point) |
HRESULT func([out] POINT*) | func(Point) |
HRESULT func([in,out] POINT*) | func(Point) |
This differs from FixedPtMarshaler in that [out] POINT* becomes a single element, Point, rather than a single-element array, Point[]. To force jactivex to generate the non-array mapping, omit the const modifier from the Point entry in the .jnf file.
To compile the sample
Use Nmake.exe to compile the makefile in the \Samples\Com\CustomMarshal\point directory. Type the following command:
Nmake
To run the sample
Run Go.bat from the \Samples\Com\CustomMarshal\rundir directory.