This sample is located in \Samples\Com\CustomMarshal\ansistr.
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
So far, all the ETYPE examples have been fixed-size structures. For some data structures, this is a prohibitive limitation. The classic example is a simple null-terminated string. This sample shows how to marshal variable-sized data structures.
Defining a variable-sized structure in a hook class requires omitting the cbByValSize field. The absence of this field marks the hook class as variable size. Unlike a fixed-sized hook class, a variable-size hook class must support toExternal and releaseExternal to be useful (the Microsoft virtual machine (Microsoft VM) cannot allocate a variable-sized structure on the stack). In addition, certain mappings available to fixed-size hooks (in particular, those that pass the data type by value) are not available to variable-size hook classes.
More specifically, consider the simple case where ETYPE is char (not LPSTR), and JTYPE is java.lang.String. (Both types can be considered immutable for this example.) This structure can be defined by a hook class with only three methods: toJava, toExternal, and releaseExternal. This is the minimum useful variable-size hook class.
The AnsiMarshaler class maps Strings to the LPSTR (ANSI null-terminated strings).
The uses for AnsiMarshaler are listed in the following table.
COM type | Marshaled to Java as |
HRESULT func([in] CHAR*) | func(String) |
HRESULT func([out] CHAR*) | func(String[]) |
HRESULT func([in,out] CHAR*) | func(String[]) |
HRESULT func([out,retval] CHAR**) | String func() |
HRESULT func([in] CHAR**) | func(String[]) |
HRESULT func([out] CHAR**) | func(String[]) |
HRESULT func([in,out] CHAR**) | func(String[]) |
To compile the sample
Use Nmake.exe to compile the makefile in the \Samples\Com\CustomMarshal\ansistr directory. Type the following command:
Nmake
To run the sample
Run Go.bat from the \Samples\Com\CustomMarshal\rundir directory.