jdk/src/share/sample/vm/clr-jvm/invoker.cs
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 142 f78032e8a30b
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 142
diff changeset
     2
 * Copyright 2006-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    32
/*
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    33
*/
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
using System;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
using System.Runtime.InteropServices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class jinvoker{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static int Main(string[] aArgs){
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    41
        
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    42
        // Print Hello to show we are in CLR
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    43
        Console.WriteLine("Hello from C#");
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    44
        if(aArgs.Length > 0)
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    45
            // invoke JVM
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    46
            return InvokeMain(aArgs[0]);
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    47
        else
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    48
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // Link the JVM API functions and the wrappers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    [DllImport("jvm.dll")]      public unsafe static extern int  JNI_CreateJavaVM(void** ppVm, void** ppEnv, void* pArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    [DllImport("jinvoker.dll")] public unsafe static extern int  MakeJavaVMInitArgs( void** ppArgs );
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    55
    [DllImport("jinvoker.dll")] public unsafe static extern void FreeJavaVMInitArgs( void* pArgs );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    [DllImport("jinvoker.dll")] public unsafe static extern int  FindClass( void* pEnv, String sClass, void** ppClass );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    [DllImport("jinvoker.dll")] public unsafe static extern int  GetStaticMethodID( void*  pEnv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                                                                    void*  pClass, 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                                                                    String szName, 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                                                                    String szArgs, 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                                                                    void** ppMid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    [DllImport("jinvoker.dll")] public unsafe static extern int NewObjectArray( void*  pEnv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                                                                int    nDimension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                                                                String sType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                                                                void** ppArray );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    [DllImport("jinvoker.dll")] public unsafe static extern int CallStaticVoidMethod( void* pEnv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                                                                      void* pClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                                                                      void* pMid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                                                                      void* pArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    [DllImport("jinvoker.dll")] public unsafe static extern int DestroyJavaVM( void* pJVM );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
	public unsafe static int InvokeMain( String sClass ){
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    76
	    
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        void*  pJVM;    // JVM struct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        void*  pEnv;    // JVM environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        void*  pVMArgs; // VM args
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        void*  pClass;  // Class struct of the executed method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        void*  pMethod; // The executed method struct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        void*  pArgs;   // The executed method arguments struct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    84
        // Fill the pVMArgs structs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        MakeJavaVMInitArgs( &pVMArgs );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    87
        // Create JVM
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int nRes = JNI_CreateJavaVM( &pJVM, &pEnv, pVMArgs );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if( nRes == 0 ){
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    90
			
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    91
            // Find the executed method class 
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if(FindClass( pEnv, sClass, &pClass) == 0 )
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    93
				
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    94
                // Find the executed method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if( GetStaticMethodID( pEnv, pClass, "main", "([Ljava/lang/String;)V", &pMethod ) == 0 )
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    96
					
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    97
                    // Create empty String[] array to pass to the main()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    if( NewObjectArray( pEnv, 0, "java/lang/String", &pArgs ) == 0 ){
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
    99
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
   100
                        // Call main()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        nRes = CallStaticVoidMethod( pEnv, pClass, pMethod, pArgs );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        if( nRes != -1 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                            Console.WriteLine("Result:"+nRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                            Console.WriteLine("Exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        Console.WriteLine("Error while making args array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        nRes = -100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    Console.WriteLine("can not find method main(String[])");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    nRes = -101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                Console.WriteLine("can not find class:"+sClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                nRes = -102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
   119
			
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
   120
            // Destroy the JVM
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            DestroyJavaVM( pJVM );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }else
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
   124
            Console.WriteLine("Can not create Java VM");
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
   125
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
   126
        // Free the JVM args structs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        FreeJavaVMInitArgs(pVMArgs);
142
f78032e8a30b 6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents: 2
diff changeset
   128
		
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return nRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}