Java JNI calling .dll built with C#

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
 
B

Bjorn A

...
I am trying to use C# in lieu of C/C++ to build
some libraries that java can call via JNI.

It's doable...

What you have to do, is to make an "unmanaged wrapper" using the JNI-header.

That in turn can call a "managed wrapper", that in turn can make use of the
..NET-dll.

I've found an article on the subject, but I'll guess there's plenty. This
one gives an actual example:

http://www.cs.up.ac.za/rotor/pubs/Views-CCPE.pdf

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top