Using JNI with C++ Objects
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Will_Brouns
Posted On:   Wednesday, September 10, 2003 01:37 AM

I have been trying to use my C++ Object called DoMeSomeMaths in Java through a wrapper dll. Unfortunately after I create my object using "InitMyObject" the object seems to disappear as I get the following error in Java: java.lang.UnsatisfiedLinkError: PutInThisNum at TestDeMath.PutInThisNum(Native Method) at TestDeMath.PutAndGetDaNum(TestDeMath.java:48) at TestDeMath.main(TestDeMath.java:35) Exception in thread "main" How can I access the same C++ object through a different native call in Java? I have attached all my code below: DoMeSomeMaths C++ object code: STDMETHODIMP CDoMeSomeMaths::PutInThisNum(   More>>

I have been trying to use my C++ Object called DoMeSomeMaths in Java through a wrapper dll. Unfortunately after I create my object using "InitMyObject" the object seems to disappear as I get the following error in Java:


java.lang.UnsatisfiedLinkError: PutInThisNum

at TestDeMath.PutInThisNum(Native Method)

at TestDeMath.PutAndGetDaNum(TestDeMath.java:48)

at TestDeMath.main(TestDeMath.java:35)

Exception in thread "main"



How can I access the same C++ object through a different native call in Java? I have attached all my code below:



DoMeSomeMaths C++ object code:

			
STDMETHODIMP CDoMeSomeMaths::PutInThisNum(LONG nNumToPut)
{
m_MyNum = nNumToPut;
return S_OK;
}

STDMETHODIMP CDoMeSomeMaths::GetOutTheNum (LONG* nNumToGet)
{
*nNumToGet = m_MyNum;
return S_OK;
}
STDMETHODIMP CDoMeSomeMaths::AddMeTwo (LONG nFirst, LONG nSecond, LONG* nReturn)
{
*nReturn = nFirst + nSecond;
return S_OK;
}



My wrapper C++ code

			
#include "TestDeMath.h"
#include "Wrap.h"

static IDoMeSomeMaths *m_MyObject;

JNIEXPORT void JNICALL Java_TestDeMath_InitMyObject (JNIEnv *env, jobject m_jObject)
{
HRESULT hr = 0;
hr = CoInitialize(0);
hr = CoCreateInstance(CLSID_CDoMeSomeMaths, NULL, CLSCTX_ALL, IID_IDoMeSomeMaths, (void**) &m_MyObject);
m_MyObject->AddRef();
CoUninitialize();
}

JNIEXPORT void JNICALL Java_TestDeMath_PutInThisNum (JNIEnv *env, jobject m_jObject, jlong jInNum)
{
HRESULT hr = 0;
hr = m_MyObject->PutInThisNum( (int) jInNum);
}

JNIEXPORT jint JNICALL Java_TestDeMath_GetOutTheNum (JNIEnv *env, jobject m_jObject)
{
long nNumGet = 0;
m_MyObject->GetOutTheNum(&nNumGet);
return nNumGet;
}

JNIEXPORT jint JNICALL Java_TestDeMath_AddMeTwo (JNIEnv *env, jobject m_jObject, jint jFirst, jint jSecond)
{
long nResult = 0;

m_MyObject->AddMeTwo(jFirst, jSecond, &nResult);

return (jint) nResult;
}

JNIEXPORT void JNICALL Java_TestDeMath_DestroyMyObject (JNIEnv *env, jobject m_jObject, jlong myLongObject)
{
m_MyObject->Release();
}



My Java Code

			
public class TestDeMath
{
public native void InitMyObject();
public native void PutInThisNum(int nNumToPut);
public native int GetOutTheNum();
public native int AddMeTwo (int nFirstNum, int nSecondNum);
public native void DestroyMyObject();

static
{
System.loadLibrary("Wrapper");
}
/** Creates a new instance of TestDeMath */
public TestDeMath()
{

}

public static TestDeMath MyTest;

public static void main(String[] args)
{
MyTest = new TestDeMath();
MyTest.PutAndGetDaNum();
}

void PutAndGetDaNum()
{
int myNum1 = 100;
int myNum2 = 200;
int myNum3 = 0;
int myNum4 = 0;

InitMyObject();
PutInThisNum(myNum1);
myNum3 = GetOutTheNum();
myNum4 = AddMeTwo(myNum1, myNum2);
System.out.println(myNum3);
System.out.println(myNum4);
DestroyMyObject();
}
}
   <<Less

Re: Using JNI with C++ Objects

Posted By:   Will_Brouns  
Posted On:   Wednesday, September 10, 2003 03:42 AM

I found that the last error that I got was due to a mismatch of types. It turns out that a java long is bigger than a C++ long...



However after type adjustments I get a lovely exception error:



An unexpected exception has been detected in native code outside the VM.

Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x1C7E5841



Which brings me back to the hypothesis that either the c++ object is gone or that the next native call is not allowed to access it. Any ideas anybody?
About | Sitemap | Contact