PRB: J/Direct or P/Invoke Parameters Must Be Strongly Typed (325595)



The information in this article applies to:

  • Microsoft Visual J# .NET (2002)
  • Microsoft Visual J# .NET (2003)

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 "Default Marshaling for Objects" 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: "Declare the parameter to be type Object ..." or "Use overloading to declare a separate method for each possible type." The second method is the only method that is supported in Visual J# .NET.

See the "More Information" 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("USER32") */ 
static native boolean WinHelp(int hwnd, String szHelpFile,
                              int cmd, int dwData);

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

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

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

				

REFERENCES

For more information about the .NET marshaling engine and default marshaling behavior and type mapping, visit the following MSDN Web site:

Modification Type:MajorLast Reviewed:8/7/2003
Keywords:kbMarshal kbprb KB325595