0

I should make use of com methods present in a 32-bit dll, on a 64-bit Java application.

Image 1image 2

My idea was to use JNI in order to use the dll methods.

I then tried to create a java class with native methods from which I then created the .h file to be used in C++. Java class with native method cpp file.

#include <jni.h>

#include "pfr_KCryptDll.h"
#include <windows.h>
#include <tchar.h>
#import "KCrypt.dll" raw_interfaces_only no_namespace

//typedef int (__GPESTDCALL *pFc_Transaction) (int, int);
typedef jstring(CALLBACK* pFc_Transaction) (jstring, jstring);


JNIEXPORT jstring JNICALL Java_pfr_KCryptDll_Encrypt (JNIEnv* env, jclass, jstring text, jstring key) {

    pFc_Transaction lpfnDllFunc;    
    HRESULT hrReturnVal = NULL;
    HINSTANCE dllHandle = NULL;
    jstring result = NULL;
    
 
    
   
    dllHandle = LoadLibrary(L"KCrypt.dll");

    if (NULL != dllHandle)
    {
       
        lpfnDllFunc = (pFc_Transaction)GetProcAddress(dllHandle, "Encrypt");
        if (NULL != lpfnDllFunc)
        {
            
            result = lpfnDllFunc(text, key);
        }
        else
        {
           
           HRESULT hrReturnValErr = ERROR_DELAY_LOAD_FAILED;
           return text;
        }
    }
    else
    {
        HRESULT hrReturnValErr = ERROR_DELAY_LOAD_FAILED;
        return key;
    }


    FreeLibrary(dllHandle);
    return result;
    }
    


}



typedef jstring(CALLBACK* pfr_test) (jstring);

JNIEXPORT jstring JNICALL Java_pfr_KCryptDll_test(JNIEnv*, jclass, jstring input) {

    return input;
}

int main(int argc, TCHAR* argv[]) {

}

I'm definitely doing something wrong, partly because I'm not that knowledgeable about these things. Could you by any chance help me and especially tell me if it is possible to use dll com methods from java.

Thank you very much.

  • *cpp file* [Please never post images of text](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors/285557#285557). They are not searchable, we cannot copy-paste... Always copy-paste the text and format it properly. You [might need to install a 32 bit JDK](https://stackoverflow.com/questions/9757456/java-native-interface-32-bit-dll-on-64-bit-system) to do this but why use native code just to encrypt a string? – g00se Aug 30 '23 at 10:33
  • i can't use 32-bit jdk unfortunately. i have a 64-bit java application and i need to use those dll functions to encrypt data to send over a web service. – user9655938 Aug 30 '23 at 10:59
  • Mixing a 64-bit JVM with a 32-bit DLL will lead to sadness. don't. – Botje Aug 30 '23 at 13:06
  • This is also **not** how you are supposed to use a COM DLL. Normally you would use [CoCreateInstance](https://learn.microsoft.com/en-us/windows/win32/learnwin32/creating-an-object-in-com) to summon an instance of `IKPassCrypt` using its GUID and calling methods on the returned object handle. – Botje Aug 30 '23 at 13:18
  • thank you very much for the feedback! @Botje – user9655938 Aug 30 '23 at 13:52
  • could I ask you kindly, if you can give me an example of how to use CoCreateInstance? thank you very much. – user9655938 Aug 30 '23 at 14:00
  • I linked you to the MS introduction on it. – Botje Aug 30 '23 at 14:09
  • What @Botje is saying is correct. But don't forget that a previous comment was advising you against using a 32 bit dll at all. You'd probably be better, *if you're really forced to use it*, to wrap it in a small C# app that operates on stdin input and outputs results to stdout – g00se Aug 30 '23 at 14:14
  • unfortunately I am forced to use that 32bit dll – user9655938 Aug 30 '23 at 18:59
  • ho provato ad utilizzare il CoInitialize ma ricevo l'errore: 'REGDB_E_CLASSNOTREG Class not registered' – user9655938 Aug 30 '23 at 19:00
  • "This error code can also indicate that the CLSID does not correspond to any component registered on the user's computer." This is the bit where you wield regsvr32.exe – Botje Aug 30 '23 at 19:27
  • I tried to register the dll with the regsvr32.exe "path dll" command, but I still receive the same error – user9655938 Aug 31 '23 at 09:11
  • Suggest you ask a new question about using that DLL from a native application first. – Botje Aug 31 '23 at 09:33

0 Answers0