This sample is located in \Samples\Com\Security_com.
Note To use this sample, it is recommended that you have Microsoft® Visual C++® version 5.0 or higher installed.
Description
Using the Sample
Key Project Files
Technologies Demonstrated
This sample shows how to protect access to resources from native code. There are two ways to do this:
The first mechanism has the advantage of always being safe regardless of who declares the Java wrapper for the native code. Anyone can easily link to the native code, regardless of who authored it. If the native code is installed on the system, it is accessible to fully-trusted code. For this reason, initiating the security check from native code ensures complete safety.
Because calling a Java method to perform a security check may incur a performance penalty, some scenarios may prefer to use the second mechanism, which prevents calls from normal native code from being penalized. COM code that is not specifically written to be called from Java will prefer the second mechanism. The second mechanism has the disadvantage of being slightly less safe for the reason previously indicated.
This sample demonstrates both mechanisms for COM calls. Microsoft® J/Direct calls to native APIs may have some measure of built-in security by optionally checking the stack for fully-trusted code at each invocation, but the same mechanisms demonstrated here can be applied to J/Direct as well as to performing more specific types of security checks.
To compile the sample
Use the following command in the base directory \Samples\Com\Security_com to build debug versions of the classes and C++-implemented COM component:
Nmake
Use the following command to build release versions:
nmake BUILD=retail
To install the sample
Run Register.bat from the base directory to register both the Java- and C++-implemented components.
To uninstall the sample
Run Remove.bat from the base directory to unregister both the Java- and C++-implemented components.
To run the sample
There are three ways to run the sample:
jview -cp:p sample;sample\demo demo.Main
jview -cp:p sample;sample\demo -a demo.Main
jview -cp:p sample -a codebase=sample\demo demo.Main
This file contains a Java/COM object that implements a custom interface and an inner class that is used from COM to perform security checks.
comsamp.cppThis file contains an implementation of a COM object that contains the custom interface, and a class factory for that COM object. The DLL function calls required for getting the class factory and registering as the InprocServer for the C COM object are also implemented.
comsamp.defThis definition file for the comsamp DLL registers and unregisters the COM component.
comsamp.idlThis file is the IDL (Interface Definition Language) definition of the custom interface and coclass that is implemented in C and Java.
Main.javaThis class tests the security. It implements Runnable and extends Applet.
This sample shows how to: