2012-09-30 88 views
0

我對Java非常非常新,但是我決定要用java加載我的C++ dll。讓我解釋一下我想在Java端做什麼。在Java中,我創建了本機函數:GetGLBuffer。參數應該是一個指向ByteArray的指針。 Java雖然沒有Pointers,但我有點失落。在C++中,它將等同於:GetGLBuffer(byte * & Buffer); GetGLBuffer(byte * & Buffer); GetGLBuffer(byte * & Buffer);緩衝區從函數內部被填充。當訪問DLL時JNI崩潰

在Java中,我做了GetGLBuffer(ByteBuffer Buffer);緩衝區從C++ DLL中填充併發送回Java,以便Java可以在JFrame上繪製它。相反,只要訪問DLL就會崩潰。任何人都在意解釋我做錯了什麼?

package library; 

import java.io.IOException; 
import java.nio.ByteBuffer; 

class SharedLibrary { 

    static{System.loadLibrary("TestDLL");} 

    static native void GetGLBuffer(ByteBuffer Buffer); 

    public SharedLibrary() throws IOException { 
     int BitsPerPixel = 32, Width = 765, Height = 565; 
     int IntSize = ((Width * BitsPerPixel + 31)/32) * Height; 
     int ByteSize = IntSize * 4; 

     ByteBuffer Buffer = ByteBuffer.allocateDirect(ByteSize); 
     GetGLBuffer(Buffer); 
     Frame F = new Frame("Testing Buffer", Buffer.array()); //Draw The Image on a frame. 
    } 
} 

C++陣營:

JNIEXPORT void JNICALL Java_library_SharedLibrary_GetGLBuffer(JNIEnv *env, jclass cl, jobject buffer) 
{ 
    int Bpp = 32; 

    Bitmap Foo("C:/Users/Brandon/Desktop/Untitled.bmp"); 
    std::vector<RGB> Pixels = Foo.Foo(); 

    std::vector<unsigned char> TEMP(Foo.Size()); 
    unsigned char* BuffPos = &TEMP[0]; 

    for (int I = 0; I < Foo.Height(); ++I) 
    { 
     for (int J = 0; J < Foo.Width(); ++J) 
     { 
      *(BuffPos++) = Pixels[(Foo.Height() - 1 - I) * Foo.Width() + J].RGBA.B; 
      *(BuffPos++) = Pixels[(Foo.Height() - 1 - I) * Foo.Width() + J].RGBA.G; 
      *(BuffPos++) = Pixels[(Foo.Height() - 1 - I) * Foo.Width() + J].RGBA.R; 

      if (Bpp > 24) 
       *(BuffPos++) = Pixels[(Foo.Height() - 1 - I) * Foo.Width() + J].RGBA.A; 
     } 
     if(Bpp == 24) 
      BuffPos += Foo.Width() % 4; 
    } 


    jbyte *data = (jbyte*)env->GetDirectBufferAddress(buffer); //Crashes as soon as it hits this.. If commented out, I have no problem. 
    MessageBox(NULL, "", "", 0); 
    memcpy(data, TEMP.data(), Foo.Size()); 
} 
+0

注意** Buffer.array()**會拋出異常,因爲直接ByteBuffer沒有一個包裹陣列, –

回答

1

的了Methode的簽名不匹配!

使用javah從java類創建一個有效的C++ - 頭部簽名。

+0

它不匹配:S它給我: ' /* * 類:library_SharedLibrary *方法:GetGLBuffer *簽名:(Ljava/NIO /字節緩衝區;)V */ JNIEXPORT空隙JNICALL Java_library_SharedLibrary_GetGLBuffer (JNIEnv的*,JCLASS,jobject);' – Brandon

+0

PLZ更新問題,除去等效部分。 –

+0

恩,好吧有任何轉儲芯片可用? –