Calling Native Methods
Visual J# provides complete support for the J/Direct technology available in Visual J++ 6.0. In addition, you can use the platform invocation services provided by the common language runtime to call into native code. For more information, see Consuming Unmanaged DLL Functions.
Example
The following code uses the platform invoke mechanism to output some text using functions in the Microsoft Visual C++ run-time library DLL.
// vjc_pinvoke.jsl
import System.Runtime.InteropServices.*;
class MyClass
{
/** @attribute DllImport("msvcrt.dll") */
public static native int puts(String c);
/** @attribute DllImport("msvcrt.dll") */
private static native int _flushall();
public static void main(String [] args)
{
puts("Test");
_flushall();
}
}
Output
Test
When using platform invoke to call into native code, marshaling support exists only for the following:
Types defined in the .NET Framework
java.lang.Object
java.lang.String
Primitive types
To marshal other Java-language types to native code, you might be required to use custom marshaling, as these are not handled by the default .NET Framework platform invoke marshaler. For more information, see Interop Marshaling.
When upgrading Visual J++ 6.0 applications, using @dll and @com directives of Visual J++ 6.0 with the .NET Framework interop attributes is not supported and is strongly discouraged. Such uses might lead to run-time issues in the compiled application.