Saturday, January 28, 2012

C# in Java-II String Handling

I like to share another java & .net interop  for string handling
for your refernce with parameter
==========================================
 public class sample
{
    public string add(string a1, string a2)
    {
        return a1+a2;
    }
}
===========================================
save as sample.cs and compile it to module
using cmd:

>csc /t:module sample.cs

Create Java File to test
===========================================
public class test
{
    public native String cat(String a,String b);
    static
    {
        System.loadLibrary("Jtest");
    }
    public static void main(String a[])
    {
        System.out.println(new test().cat("dreamy,","I love you forever..."));
    }
}
==========================================
save it as test.java compile as
>javac test.java
create native header file
>javah -jni test
it will create test.h
=================================================
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class test */

#ifndef _Included_test
#define _Included_test
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     test
 * Method:    cat
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_test_cat
  (JNIEnv *, jobject, jstring, jstring);

#ifdef __cplusplus
}
#endif
#endif
=================================================

create win32 project using visual studio (I used VS2010)
Choose project name as Jtest

include header and C#.net module
write header for manged C++ conversion
==============================================
#using<mscorlib.dll>
#using"sample.netmodule"
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;

public __gc class SampleC
{
public:
    sample __gc *t;
    SampleC()
    {
        t=new sample();
    }
    const char * callsample(const char* a, const char* b)
    {

        const char* str2 = (const char*)(void*)Marshal::StringToHGlobalAnsi(t->add(a,b));
        return str2;
    }
};
===========================================
save it as sample.h

create sample.cpp file
============================================
#include <jni.h>
#include"test.h"
#include"sample.h"

JNIEXPORT jstring JNICALL Java_test_cat
    (JNIEnv *env, jobject obj , jstring a, jstring b)
{
    SampleC* t = new SampleC();
    const char* a1 = (env)->GetStringUTFChars(a ,NULL);
    const char* b1 = (env)->GetStringUTFChars(b ,NULL);
    const char  *t1= t->callsample(a1,b1);   
    (env)->ReleaseStringUTFChars(a , a1);
    (env)->ReleaseStringUTFChars(b , b1);
    jstring str = env->NewStringUTF(t1);
    return str;
}
=============================================
optimze compliler to build /clr:oldSyntax
Include Jdk/Include directory path
build the project.
we will Get Jtest DLL

run the project
with C#.net module,Native DLL file and class file at the same folder.

>java test
dreamy,I love you forever...
================================================
Hope it helps

find the link

https://docs.google.com/leaf?id=0B8j-6CFq6hsGZDM1MDEyNDgtNDhhMi00ZThiLThhYWItNmZjNzY1NGU2YTg1&hl=en_US

refernce:

http://support.microsoft.com/kb/311259






Regards
..arun@darkside..