C# Java

Amitava_Mukherjee
Amitava_Mukherjee used Ask the Experts™
on
I tried to call C# dll from Java. I was searching fr some JNI examples for that job. EE had provided the following link

http://www.codeproject.com/KB/cross-platform/javacsharp.aspx

And, thats a nice one indeed. But when I downloaded the code and tried to compile those codes, it failed. Also the details of the steps are not clear to me in that article. The VC++ compilation procedure is also a bit backdated as far as I understand (I am sorry if I am wrong, I had no experience in VC++).

Can you please provide me a sample code how to call a C# dll from Java? (C# dll may be a hello world in console). I want a step by step procedure or any link where step by step procedure is described. Please help me.

I am using Netbeans 6.9.1 and VS 2008

Thanx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Valeri,

I wantto call C# dll from Java, not C. The link is callcing C dll from Java. Is there any hint for C#?
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

It's almost the same, everything is linked throught the header files.
ok, then read this:
http://www.codeproject.com/KB/cross-platform/javacsharp.aspx

Author

Commented:
Ok, trying, give me some time

Author

Commented:
The VC++ code provided with that article is throwing error at build time. Also a warning is comming that

"Warning 1 Command line warning D9035 : option 'clr:oldsyntax' has been deprecated and will be removed in a future release cl VCMyMnd"

Not working :-(.

J/C# portion is ok, I just stuck in the VC++ portion, can't figure out why that is not compiling. Can you please help?
what error? at which lines? could oyu please post the error?

Author

Commented:
The following errors are throwing for the attached VC++ code.


Warning      1      Command line warning D9035 : option 'clr:oldsyntax' has been deprecated and will be removed in a future release      cl      VCMyMnd
Error      2      error C2143: syntax error : missing ';' before 'gc pointer'      d:\JNI1\MyMnd\VC\VCMyMnd\VCMyMnd\VCMyMnd.cpp      13      VCMyMnd
Error      3      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      d:\JNI1\MyMnd\VC\VCMyMnd\VCMyMnd\VCMyMnd.cpp      13      VCMyMnd
Error      4      error C3160: 'int __gc *' : a data member of a managed class cannot have this type      d:\JNI1\MyMnd\VC\VCMyMnd\VCMyMnd\VCMyMnd.cpp      13      VCMyMnd
Error      5      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      d:\JNI1\MyMnd\VC\VCMyMnd\VCMyMnd\VCMyMnd.cpp      13      VCMyMnd
Error      6      error C2065: 't' : undeclared identifier      d:\JNI1\MyMnd\VC\VCMyMnd\VCMyMnd\VCMyMnd.cpp      16      VCMyMnd
Error      7      error C2061: syntax error : identifier 'CSMyMnd'      d:\JNI1\MyMnd\VC\VCMyMnd\VCMyMnd\VCMyMnd.cpp      16      VCMyMnd
Error      8      error C2065: 't' : undeclared identifier      d:\JNI1\MyMnd\VC\VCMyMnd\VCMyMnd\VCMyMnd.cpp      20      VCMyMnd
Error      9      error C2227: left of '->SayMyMnd' must point to class/struct/union/generic type      d:\JNI1\MyMnd\VC\VCMyMnd\VCMyMnd\VCMyMnd.cpp      20      VCMyMnd

// VCMyMnd.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#using <mscorlib.dll>
//#using "CSMyMnd.netmodule"
using namespace System; 

// This code wraps the C# class using Managed C++
public __gc class VCMyMnd
{
public:
	CSMyMnd __gc *t; // Provide .NET interop and garbage collecting to the pointer.

	VCMyMnd() {
		t = new CSMyMnd(); // Assign the reference a new instance of the constructor.
	}

	void callCSSayMyMnd() {
		t->SayMyMnd(); 
	}
};

Open in new window

hm... it looks like your method in the JNI wrapper is not defined in the right way. You shoul follow this convention:
Java_<package name>_<class name>_<method name> did you wrote it in this way?
Unfortunately I can't help you with this compile problems because currently I don't have VC++ installed :-(

Author

Commented:
I think it is ok
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class mymnd_MyMnd */

#ifndef _Included_mymnd_MyMnd
#define _Included_mymnd_MyMnd
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     mymnd_MyMnd
 * Method:    SayMyMnd
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_mymnd_MyMnd_SayMyMnd
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Open in new window

yes, looks ok! did you compile the C# code as module?
try to post all of this files and post new question in C# and C++ zones to see if anyone of the experts in this zones will be able to compile this files?

Author

Commented:
OK, I am sending my full code.

The problem is the VC++ code is not compiling
MyMnd.Java
-----------

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mymnd;

/**
 *
 * @author Administrator
 */
public class MyMnd {

    public native void SayMyMnd();
    static{
        System.load("D:\\JNI1\\MyMnd\\dll\\MyMnd.dll");
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new MyMnd().SayMyMnd();
    }
}

=================================================================

MyMnd.h  [Generated from the above java file]
---------------------------------------------

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class mymnd_MyMnd */

#ifndef _Included_mymnd_MyMnd
#define _Included_mymnd_MyMnd
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     mymnd_MyMnd
 * Method:    SayMyMnd
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_mymnd_MyMnd_SayMyMnd
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

==================================================================

VCMyMnd.cpp [ VC++ Project Code ]
---------------------------------

//

#include "stdafx.h"
#using <mscorlib.dll>
//#using "CSMyMnd.netmodule"
using namespace System; 

// This code wraps the C# class using Managed C++
public __gc class VCMyMnd
{
public:
	CSMyMnd __gc *t; // Provide .NET interop and garbage collecting to the pointer.

	VCMyMnd() {
		t = new CSMyMnd(); // Assign the reference a new instance of the constructor.
	}

	void callCSSayMyMnd() {
		t->SayMyMnd(); 
	}
};

==================================================================

CSMyMnd.cs [ C# Code ]
----------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSMyMnd
{
    public class CSMyMnd
    {
        public void SayMyMnd()
        {
            Console.WriteLine("My Mnd");
            Console.Read();
            System.Windows.Forms.MessageBox.Show("My Mnd", "Msg", 
                System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
        }
    }
}

===============================================================

Open in new window

Author

Commented:
I am sending my project as a zip file here

Author

Commented:
The total JNI project is attached herewith

Author

Commented:
Sorry, I tried but I can't attach my project, for the miscellenious file extensions from java / VC++

Author

Commented:
thanx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial