简体   繁体   中英

JNI Crashes When Accessing DLL

I'm very very very new to Java but I decided I want to load my C++ dll in java. Let me explain what I'm trying to do on the Java side..

In Java, I'm creating the native function: GetGLBuffer. The parameters is supposed to be a Pointer to a ByteArray. Java doesn't have Pointers though so I'm kind of lost.

In C++ it'd be equivalent to: GetGLBuffer(byte* &Buffer); Buffer gets filled from within the function.

In Java I did GetGLBuffer(ByteBuffer Buffer); Buffer gets filled from the C++ DLL and sent back to Java so that Java can draw it on a JFrame. Instead, it crashes as soon as it accesses the DLL. Anyone care to explain what I'm doing wrong?

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++ Side:

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

The Methode-Signatures does not match!

Use javah to create a valid c++-header signature from the java class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM