Microsoft KB Archive/325595

= PRB: J/Direct or P/Invoke Parameters Must Be Strongly Typed =

Article ID: 325595

Article Last Modified on 4/16/2007

-

APPLIES TO


 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



This article was previously published under Q325595



SYMPTOMS
An unhandled exception may occur during a call to native code.



CAUSE
Because Visual J# .NET uses the .NET marshaling engine, polymorphic parameters are no longer supported for native interoperating. The .NET marshaler only supports marshaling of object types as either a Variant (the default) or an interface when the object is a structure field.

For more information, see the &quot;Default Marshaling for Objects&quot; topic in the Microsoft Developer Network (MSDN) .NET documentation.



RESOLUTION
The Microsoft Software Development Kit (SDK) for Java documentation uses the native Win32 WinHelp function as an example scenario where a polymorphic parameter might prove useful. The following is an excerpt:

Some Microsoft Win32 functions declare a parameter whose type depends on the value of another parameter. For example, the WinHelp function is declared as follows:

BOOL WinHelp(int hwnd, LPCTSTR szHelpFile, UINT cmd, DWORD dwData); The dwData parameter can be any one of the following, depending on the value of the cmd parameter: a pointer to a String, a pointer to a MULTIKEYHELP structure, a pointer to a HELPWININFO, or a plain integer.

The Microsoft SDK for Java documentation goes on to explain that there are two ways to declare such a parameter by using J/Direct: &quot;Declare the parameter to be type Object ...&quot; or &quot;Use overloading to declare a separate method for each possible type.&quot; The second method is the only method that is supported in Visual J# .NET.

See the &quot;More Information&quot; section for a WinHelp code sample of the second method.



STATUS
This behavior is by design.



MORE INFORMATION
The following is a code sample taken from the Microsoft SDK for Java documentation. This sample shows how to use overloading to declare multiple types for the same native function. Note also that because this method allows compile time binding, it offers better run-time performance and better type-checking. /** @dll.import(&quot;USER32&quot;) */ static native boolean WinHelp(int hwnd, String szHelpFile,                             int cmd, int dwData);

/** @dll.import(&quot;USER32&quot;) */ static native boolean WinHelp(int hwnd, String szHelpFile,                             int cmd, String dwData);

/** @dll.import(&quot;USER32&quot;) */ static native boolean WinHelp(int hwnd, String szHelpFile,                             int cmd, MULTIKEYHELP dwData);

/** @dll.import(&quot;USER32&quot;) */ static native boolean WinHelp(int hwnd, String szHelpFile,                             int cmd, HELPWININFO dwData);

