author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 142 | f78032e8a30b |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2006-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
7 |
* |
|
8 |
* - Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
14 |
* |
|
15 |
* - Neither the name of Sun Microsystems nor the names of its |
|
16 |
* contributors may be used to endorse or promote products derived |
|
17 |
* from this software without specific prior written permission. |
|
18 |
* |
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
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 | 35 |
using System; |
36 |
using System.Runtime.InteropServices; |
|
37 |
||
38 |
class jinvoker{ |
|
39 |
||
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 | 49 |
} |
50 |
||
51 |
// Link the JVM API functions and the wrappers |
|
52 |
||
53 |
[DllImport("jvm.dll")] public unsafe static extern int JNI_CreateJavaVM(void** ppVm, void** ppEnv, void* pArgs); |
|
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 | 56 |
[DllImport("jinvoker.dll")] public unsafe static extern int FindClass( void* pEnv, String sClass, void** ppClass ); |
57 |
[DllImport("jinvoker.dll")] public unsafe static extern int GetStaticMethodID( void* pEnv, |
|
58 |
void* pClass, |
|
59 |
String szName, |
|
60 |
String szArgs, |
|
61 |
void** ppMid); |
|
62 |
||
63 |
[DllImport("jinvoker.dll")] public unsafe static extern int NewObjectArray( void* pEnv, |
|
64 |
int nDimension, |
|
65 |
String sType, |
|
66 |
void** ppArray ); |
|
67 |
||
68 |
[DllImport("jinvoker.dll")] public unsafe static extern int CallStaticVoidMethod( void* pEnv, |
|
69 |
void* pClass, |
|
70 |
void* pMid, |
|
71 |
void* pArgs); |
|
72 |
||
73 |
[DllImport("jinvoker.dll")] public unsafe static extern int DestroyJavaVM( void* pJVM ); |
|
74 |
||
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 | 77 |
void* pJVM; // JVM struct |
78 |
void* pEnv; // JVM environment |
|
79 |
void* pVMArgs; // VM args |
|
80 |
void* pClass; // Class struct of the executed method |
|
81 |
void* pMethod; // The executed method struct |
|
82 |
void* pArgs; // The executed method arguments struct |
|
83 |
||
142
f78032e8a30b
6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents:
2
diff
changeset
|
84 |
// Fill the pVMArgs structs |
2 | 85 |
MakeJavaVMInitArgs( &pVMArgs ); |
86 |
||
142
f78032e8a30b
6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents:
2
diff
changeset
|
87 |
// Create JVM |
2 | 88 |
int nRes = JNI_CreateJavaVM( &pJVM, &pEnv, pVMArgs ); |
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 | 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 | 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 | 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 | 101 |
nRes = CallStaticVoidMethod( pEnv, pClass, pMethod, pArgs ); |
102 |
if( nRes != -1 ) |
|
103 |
Console.WriteLine("Result:"+nRes); |
|
104 |
else |
|
105 |
Console.WriteLine("Exception"); |
|
106 |
||
107 |
}else{ |
|
108 |
Console.WriteLine("Error while making args array"); |
|
109 |
nRes = -100; |
|
110 |
} |
|
111 |
else{ |
|
112 |
Console.WriteLine("can not find method main(String[])"); |
|
113 |
nRes = -101; |
|
114 |
} |
|
115 |
else{ |
|
116 |
Console.WriteLine("can not find class:"+sClass); |
|
117 |
nRes = -102; |
|
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 | 121 |
DestroyJavaVM( pJVM ); |
122 |
||
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 | 127 |
FreeJavaVMInitArgs(pVMArgs); |
142
f78032e8a30b
6627817: Remove ^M characters in all files (Makefiles too)
ohair
parents:
2
diff
changeset
|
128 |
|
2 | 129 |
return nRes; |
130 |
} |
|
131 |
} |