访问DLL时JNI崩溃

时间:2012-09-30 05:41:55

标签: java c++ java-native-interface jnienv

我对Java非常非常新,但我决定在java中加载我的C ++ dll。让我解释一下我在Java方面要做的事情。

在Java中,我正在创建本机函数:GetGLBuffer。参数应该是指向ByteArray的指针。 Java没有Pointers,所以我有点迷失。

在C ++中,它等同于:GetGLBuffer(byte *& Buffer);缓冲区从函数内填充。

在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());
}

1 个答案:

答案 0 :(得分:1)

Method-Signatures不匹配!

使用java从java类创建有效的c ++标头签名。