none
Renaming the binary results in loadLibrary error in Windows 64

    Question

  • I have a c++ application (built with Visual Studio 2005) that loads JVM
    and invokes Java methods through JNI.

    dirxml_remote.exe loads JVM and calls System.loadLibrary(dirxml_remote_jni.dll) from Java code.
    While building dirxml_remote_jni.dll, it is linked with dirxml_remote.lib.

    Dependency Walker shows dirxml_remote_jni.dll depends on dirxml_remote.exe

    If I rename dirxml_remote.exe to dirxml_remote_x.exe and start the executable, loadLibrary fails
    with the following error

    java.lang.UnsatisfiedLinkError: dirxml_remote_jni.dll: A
    dynamic link library (DLL) initialization routine failed.

    This error happens only for 64-bit applications.

    WinDbg shows the following sequence

    ModLoad: 00000000`06c70000 00000000`06c7c000   dirxml_remote_jni.dll
    ModLoad: 00000000`06c90000 00000000`06e87000   dirxml_remote.exe
    ntdll!ZwTerminateProcess+0xa:
    00000000`774a6fda c3              ret

    The linker flags are the same for both 32-bit and 64-bit. For 32-bit, renaming the exe works.
    For 64-bit, loadLibrary fails.

    Please let me know if you have any clues on this.

    Monday, February 01, 2010 10:49 AM

All replies

  • Hello m_siva,

    Thanks for your post.

    It seems the error is thrown by the System.loadLibrary function, which is a function from jave libraries. I suggestion you post this issue on some java community as well.

    If you could provide more information to reprodice this issue, I will dig into this issue and keep you updated.

    Thanks,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, February 02, 2010 10:53 AM

  • The problem occurs only for Windows 64-bit application. (both Windows 2003 and 2008). 32-bit application in 64-bit Windows works fine.

    Also, loadlibrary fails only if the exe name is different.

    So, it looks more like a 64-bit Windows specific issue rather than a Java issue.
    Tuesday, February 02, 2010 1:03 PM
  • Hello m_siva,

    Thanks for your post.

    As the issue only happen on 64bit Windows, have you installed the 64bit JRE on the machine?

    http://www.java.com/en/download/faq/java_win64bit.xml

    To troubleshoot this issue, could you please send me a simple project that could repro this issue to my mail box(v-rzhang[at]microsoft.com)? I will debug this issue on my side and keep you updated.

    Thanks,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, February 03, 2010 7:31 AM
  • Hello m_siva,

    Have you got any progress on this issue? If there is anything else we can help, welcome to post here.

    Thanks,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, February 05, 2010 10:05 AM

  • I have tested the same with latest JRE (1.6.0_18 - Windows x64) and the loadlibrary error continues to occur.

    Also, calling LoadLibrary API from C++ code gives the same error.  (error code 1114).

    If I call LoadLibraryEx with DONT_RESOLVE_DLL_REFERENCES flag set, it loads the library, but later results in a crash later.

    So, the problem is not with Java.
    Monday, February 15, 2010 6:28 AM
  • Hello  m_siva,

    Could you please send me a simple project that could repro this issue to my mail box(v-rzhang[at]microsoft.com)? Once I can repro the issue on my side, I will dedug it and keep you updated.

    Thanks,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, February 22, 2010 5:38 AM
  • I have sent a sample program to you. Please let me know if you need anything else.
    Tuesday, February 23, 2010 2:19 PM
  • Here is the sample program.

    main.cpp:
    -----------

    #include <stdio.h>
    #include "windows.h"

    __declspec( dllexport ) void f1();

    static int count;
    void main()
    {

        DWORD err;

        count++;
        fprintf(stderr, "count in main is %d and address is %p\n", count, &count);

        HINSTANCE hDLL = LoadLibrary(L"C:\\Project\\testwin\\test_app_lib.dll"); // Handle to DLL
        if(hDLL != NULL)
        {
            fprintf(stderr, "Library has been loaded\n");
        }
        else
        {
            err = GetLastError();
                fprintf(stderr, "LoadLibrary failed ... err=%d \n", err);
        }


    }

    void f1()
    {
        count++;
        fprintf(stderr, "count in f1 is %d and address is %p\n", count, &count);
        return;
    }

    test_lib.cpp:
    --------------

    #include <stdio.h>
    #include "windows.h"

    extern void f1();

    BOOL APIENTRY                           //return TRUE if DLL init succeeded
    DllMain(
            HANDLE hModule,                 //DLL HINSTANCE
            DWORD reason,                   //reason function is being called
            LPVOID unused)
    {
        fprintf(stderr, "In DllMain\n");
        f1();
        fprintf(stderr, "Leaving DllMain\n");
        return TRUE;
    }

    Build Scripts:
    ---------------

    build.x86.bat:
    ---------------

    cl.exe /c /nologo /Zc:wchar_t- /Zi /W3 /Wp64 /WX /EHa /MD /O2 /Zp4  /D_WINDOWS /DWINDOWS /DUNICODE /D_UNICODE /DN_PLAT_WNT /DN_INC_NO_OLD_MACROS /DPRECHECKIN=0 /DSAL_WIN32 /DDS_FOR_WIN32 /DSTRICT /DUSE_STL_NAMESPACE /D_CRT_SECURE_NO_DEPRECATE  /DSAL_NO_DEPRECATED_APIS /DNDEBUG /DWIN32 /D_USE_32BIT_TIME_T /D_CONSOLE /DSTREAM_INTERFACE_EXCEPTIONS /DUSE_OPENSSL /DSTREAM_INTERFACE_FACTORY /I"." main.cpp
    link.exe /nologo /subsystem:console /debug /machine:x86 /nodefaultlib msvcrt.lib msvcprt.lib oldnames.lib advapi32.lib user32.lib kernel32.lib gdi32.lib ws2_32.lib comctl32.lib shell32.lib psapi.lib wtsapi32.lib main.obj /out:test_app.exe
    mt.exe /nologo -manifest test_app.exe.manifest -outputresource:test_app.exe;1
    cl.exe /c /nologo /Zc:wchar_t- /Zi /W3 /Wp64 /WX /EHa /MD /O2 /Zp4  /D_WINDOWS /DWINDOWS /DUNICODE /D_UNICODE /DN_PLAT_WNT /DN_INC_NO_OLD_MACROS /DPRECHECKIN=0 /DSAL_WIN32 /DDS_FOR_WIN32 /DSTRICT /DUSE_STL_NAMESPACE /D_CRT_SECURE_NO_DEPRECATE  /DSAL_NO_DEPRECATED_APIS /DNDEBUG /DWIN32 /D_USE_32BIT_TIME_T /D_CONSOLE /DSTREAM_INTERFACE_EXCEPTIONS /DUSE_OPENSSL /DSTREAM_INTERFACE_FACTORY /I"." test_lib.cpp
    link.exe /nologo /subsystem:console /machine:x86 -dll -out:test_app_lib.dll test_lib.obj test_app.lib
    mt.exe /nologo -manifest test_app_lib.dll.manifest -outputresource:test_app_lib.dll;2

    build.x64.bat:
    ---------------

    cl.exe /c /nologo /Zc:wchar_t- /Zi /W3 /Wp64 /WX /EHa /MD /O2 /Zp8  /D_WINDOWS /DWINDOWS /DUNICODE /D_UNICODE /DN_PLAT_WNT /DN_INC_NO_OLD_MACROS /DPRECHECKIN=0 /DSAL_WIN64 /DDS_FOR_WIN32 /DSTRICT /DUSE_STL_NAMESPACE /D_CRT_SECURE_NO_DEPRECATE  /DSAL_NO_DEPRECATED_APIS /DNDEBUG /DWIN64 /D_CONSOLE /DSTREAM_INTERFACE_EXCEPTIONS /DUSE_OPENSSL /DSTREAM_INTERFACE_FACTORY /I"." main.cpp
    link.exe /nologo /subsystem:console /debug /machine:x64 /nodefaultlib msvcrt.lib msvcprt.lib oldnames.lib advapi32.lib user32.lib kernel32.lib gdi32.lib ws2_32.lib comctl32.lib shell32.lib psapi.lib wtsapi32.lib main.obj /out:test_app.exe
    mt.exe /nologo -manifest test_app.exe.manifest -outputresource:test_app.exe;1
    cl.exe /c /nologo /Zc:wchar_t- /Zi /W3 /Wp64 /WX /EHa /MD /O2 /Zp8  /D_WINDOWS /DWINDOWS /DUNICODE /D_UNICODE /DN_PLAT_WNT /DN_INC_NO_OLD_MACROS /DPRECHECKIN=0 /DSAL_WIN64 /DDS_FOR_WIN32 /DSTRICT /DUSE_STL_NAMESPACE /D_CRT_SECURE_NO_DEPRECATE  /DSAL_NO_DEPRECATED_APIS /DNDEBUG /DWIN64 /D_CONSOLE /DSTREAM_INTERFACE_EXCEPTIONS /DUSE_OPENSSL /DSTREAM_INTERFACE_FACTORY /I"." test_lib.cpp
    link.exe /nologo /subsystem:console /machine:x64 -dll -out:test_app_lib.dll test_lib.obj test_app.lib
    mt.exe /nologo -manifest test_app_lib.dll.manifest -outputresource:test_app_lib.dll;2

    Output:
    --------

    output for 32-bit:
    -------------------

    C:\Project\testwin>test_app
    count in main is 1 and address is 00405120
    In DllMain
    count in f1 is 2 and address is 00405120
    Leaving DllMain
    Library has been loaded
    In DllMain
    count in f1 is 3 and address is 00405120
    Leaving DllMain

    C:\Project\testwin>copy test_app.exe test_app_x.exe
    Overwrite test_app_x.exe? (Yes/No/All): y
            1 file(s) copied.

    C:\Project\testwin>test_app_x
    count in main is 1 and address is 00405120
    In DllMain
    count in f1 is 2 and address is 00405120
    Leaving DllMain
    Library has been loaded
    In DllMain
    count in f1 is 3 and address is 00405120
    Leaving DllMain

    output for 64-bit:
    -------------------

    C:\Project\testwin>test_app
    count in main is 1 and address is 0000000000405128
    In DllMain
    count in f1 is 2 and address is 0000000000405128
    Leaving DllMain
    Library has been loaded
    In DllMain
    count in f1 is 3 and address is 0000000000405128
    Leaving DllMain

    C:\Project\testwin>copy test_app.exe test_app_x.exe
            1 file(s) copied.

    C:\Project\testwin>test_app_x
    count in main is 1 and address is 0000000000405128
    In DllMain
    count in f1 is 1 and address is 0000000000155128
    Leaving DllMain
    Library has been loaded
    In DllMain
    count in f1 is 2 and address is 0000000000155128
    Leaving DllMain


    Wednesday, February 24, 2010 12:41 PM