C
Cris
My question in a nutshell: Can a C header file be converted into a C#
interface? Details for why I want this below...
I am trying to use C# in lieu of C/C++ to build some libraries that
java can call via JNI. Of course it seems that Java cannot make use
of just any .dll file and make calls, rather Java can only make calls
on .dll that have headers corresponding to what javah outputs.
For example suppose I want to have java pull out some performance
statistics from the windows kernel, it might be nice to convert the
following C# program:
// BEGIN cpu.cs
using System;
using System.Diagnostics;
public class CPUStats {
private static PerformanceCounter PC;
public static float Java_Win32CPU_GetCpuUtilization()
//name above was initially just GetCpuUtilization, but I am trying
//to use the name as javah decorates.(javah header coming..)
{
if ( !PerformanceCounterCategory.Exists("Processor") ) {
return -1;
}
PC = new PerformanceCounter("Processor","% Processor
Time","_Total");
return PC.NextValue();
//Console.WriteLine("CPU % utilization is " + cpu);
}
}
//END cpu.cs
into a .dll (via csc /t:library /out:cpu.dll cpu.cs)
and have the following java program :
//BEGIN Win32CPU.java
class Win32CPU {
private native float GetCpuUtilization();
public static void main (String[] args) {
Win32CPU stat = new Win32CPU();
while (true)
{
try {
float f = stat.GetCpuUtilization();
System.out.println("CPU utilization is " + f);
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static {
System.loadLibrary("cpu");
}
}
//end Win32CPU.java
Now it would be nice if the above works, but, as far as I know, java
cannot just call any function in any .dll; rather it can only call
functions coresponding to a header file generated from javah, for
example after compiling
Win32CPU.java and running the following: command javah -jni Win32CPU
I receive the following header file:
//BEGIN header
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Win32CPU */
#ifndef _Included_Win32CPU
#define _Included_Win32CPU
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Win32CPU
* Method: GetCpuUtilization
* Signature: ()F
*/
JNIEXPORT jfloat JNICALL Java_Win32CPU_GetCpuUtilization
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
//END header
So it seems to me if I could convert a C style header file into a C#
interface I might be in business. Any ideas?
Thanks,
Cris
interface? Details for why I want this below...
I am trying to use C# in lieu of C/C++ to build some libraries that
java can call via JNI. Of course it seems that Java cannot make use
of just any .dll file and make calls, rather Java can only make calls
on .dll that have headers corresponding to what javah outputs.
For example suppose I want to have java pull out some performance
statistics from the windows kernel, it might be nice to convert the
following C# program:
// BEGIN cpu.cs
using System;
using System.Diagnostics;
public class CPUStats {
private static PerformanceCounter PC;
public static float Java_Win32CPU_GetCpuUtilization()
//name above was initially just GetCpuUtilization, but I am trying
//to use the name as javah decorates.(javah header coming..)
{
if ( !PerformanceCounterCategory.Exists("Processor") ) {
return -1;
}
PC = new PerformanceCounter("Processor","% Processor
Time","_Total");
return PC.NextValue();
//Console.WriteLine("CPU % utilization is " + cpu);
}
}
//END cpu.cs
into a .dll (via csc /t:library /out:cpu.dll cpu.cs)
and have the following java program :
//BEGIN Win32CPU.java
class Win32CPU {
private native float GetCpuUtilization();
public static void main (String[] args) {
Win32CPU stat = new Win32CPU();
while (true)
{
try {
float f = stat.GetCpuUtilization();
System.out.println("CPU utilization is " + f);
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static {
System.loadLibrary("cpu");
}
}
//end Win32CPU.java
Now it would be nice if the above works, but, as far as I know, java
cannot just call any function in any .dll; rather it can only call
functions coresponding to a header file generated from javah, for
example after compiling
Win32CPU.java and running the following: command javah -jni Win32CPU
I receive the following header file:
//BEGIN header
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Win32CPU */
#ifndef _Included_Win32CPU
#define _Included_Win32CPU
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Win32CPU
* Method: GetCpuUtilization
* Signature: ()F
*/
JNIEXPORT jfloat JNICALL Java_Win32CPU_GetCpuUtilization
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
//END header
So it seems to me if I could convert a C style header file into a C#
interface I might be in business. Any ideas?
Thanks,
Cris