JNI dll crashing app after injection

Hello, why my app crash after injecting this dll? I got this code from Visual Studio Debugger:
 
Unhandled exception at 0x00000000352B10CB in javaw.exe: 0xC0000005: Access violation reading location 0x0000000000000000.


This is my DLL code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include "jni.h"
#include <iostream>

JavaVM* jvm;
JNIEnv* env;
HANDLE jvmHandle;
FARPROC func_JNI_GetCreatedJavaVMs;
JavaVMInitArgs vm_args;

void init() {
    AllocConsole();
    freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
    std::cout << "This works" << std::endl;
 
    typedef jint(JNICALL* GetCreatedJavaVMs)(JavaVM**, jsize, jsize*);
    GetCreatedJavaVMs jni_GetCreatedJavaVMs;
    jni_GetCreatedJavaVMs = (GetCreatedJavaVMs)GetProcAddress(GetModuleHandle(
        TEXT("jvm.dll")), "JNI_GetCreatedJavaVMs");

    std::cout << "CreatedJavaVMs: "<<jni_GetCreatedJavaVMs << std::endl;

    std::cout << "JVM load succeeded: Version ";
    jint ver = env->GetVersion();
    std::cout << ((ver >> 16) & 0x0f) << "." << (ver & 0x0f) << std::endl;




    //if (getEnvStat == JNI_EDETACHED)
    //{
    //     vm->AttachCurrentThread((void**)&env, NULL);
    // }

    /*if (env != nullptr)
    {
        //start
    }


    if (env->ExceptionCheck())
    {
        env->ExceptionDescribe();
    }

    vm->DetachCurrentThread();*/

}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    init();
    return TRUE;
}
> freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
What's that cast?

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/freopen-s-wfreopen-s?view=vs-2019

Why isn't it
freopen_s(&stdout, "CONOUT$", "w", stdout);
Thanks, will test It later.

Got this error:

1
2
3
E0158	expression must be an lvalue or a function designator

Error	C2102	'&' requires l-value


Last edited on
nope, are You sure crashes is caused by this cast?
 
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
Are you?

> errno_t freopen( FILE** pFile, const char *path, const char *mode, FILE *stream );
The parameters have different levels of indirection.

Just papering over that fact with a cast doesn't create magic.
Without console it crashing too. This error probably is caused by sth else (still dont know what is this).

Calling JNI functions crashing my app ;/

Fixed, "vm->AttachCurrentThread((void**)&env, NULL);" should be before calling any env functions
Last edited on
Topic archived. No new replies allowed.