Equivalent Java Function - having a return type of BSTR (How to implement BSTR* in JAVA?)
-
I am having a ComServer, and its outlined functions are implemneted in C++. Below is the Cpp function.
HRESULT cIntuneServer::GetActiveProjectName(/*([out]*/ BSTR* nameOfProject)
{
CComBSTR projectName(L"\\Default\\");\*nameOfProject = projectName; return S\_OK;
}
I tried to implement the same function in Java:
JIString outStr = new JIString("");
dispatch.callMethod("GetActiveProjectName", new Object[]{outStr});
System.out.println("Out String = "+outStr.toString();OUTPUT: Out String = [Type: 1 , []] How do i implement this in java?, I knew i am doing something worng, how do i get the BSTR* value to the OutStr in Java?
-
I am having a ComServer, and its outlined functions are implemneted in C++. Below is the Cpp function.
HRESULT cIntuneServer::GetActiveProjectName(/*([out]*/ BSTR* nameOfProject)
{
CComBSTR projectName(L"\\Default\\");\*nameOfProject = projectName; return S\_OK;
}
I tried to implement the same function in Java:
JIString outStr = new JIString("");
dispatch.callMethod("GetActiveProjectName", new Object[]{outStr});
System.out.println("Out String = "+outStr.toString();OUTPUT: Out String = [Type: 1 , []] How do i implement this in java?, I knew i am doing something worng, how do i get the BSTR* value to the OutStr in Java?
I'm not a JNI expert but a quick google search suggests this is not so straight-forward. Here is how one person describes it:
a BSTR is a funky kind of Microsoft String object, and converting between them and C strings is complicated (and from there to Java Strings adds yet another step)
Google "BSTR java string" brings up lots of articles about it. One of them may help.
-
I am having a ComServer, and its outlined functions are implemneted in C++. Below is the Cpp function.
HRESULT cIntuneServer::GetActiveProjectName(/*([out]*/ BSTR* nameOfProject)
{
CComBSTR projectName(L"\\Default\\");\*nameOfProject = projectName; return S\_OK;
}
I tried to implement the same function in Java:
JIString outStr = new JIString("");
dispatch.callMethod("GetActiveProjectName", new Object[]{outStr});
System.out.println("Out String = "+outStr.toString();OUTPUT: Out String = [Type: 1 , []] How do i implement this in java?, I knew i am doing something worng, how do i get the BSTR* value to the OutStr in Java?
My guess is the question is incomplete. Java doesn't access random dll methods. One must use JNI to do it. I believe the poster is using a dynamic 3rd party wrapper for JNI because a similar post with info to that effect showed up in the Oracle Java forums.
-
My guess is the question is incomplete. Java doesn't access random dll methods. One must use JNI to do it. I believe the poster is using a dynamic 3rd party wrapper for JNI because a similar post with info to that effect showed up in the Oracle Java forums.
You are right jschell, i posted it at oracle forums too, the thing is no one was actually helping at the J-Interop forum, so i have been posting at some other good forums for help etc... My only aim is to read a string from a cpp function, doesn't matter if i am using a wrapper. At least if someone could help me how to do this using Native or Standard Java without a wrapper like an example or something. Maybe i can proceed from there.
-
You are right jschell, i posted it at oracle forums too, the thing is no one was actually helping at the J-Interop forum, so i have been posting at some other good forums for help etc... My only aim is to read a string from a cpp function, doesn't matter if i am using a wrapper. At least if someone could help me how to do this using Native or Standard Java without a wrapper like an example or something. Maybe i can proceed from there.
Having a C++ written class, you need to convert it into a shared library (so or DLL); for example, suppose you can embed the snippet you are using in C++ (the one you posted before) into a function like LPSTR yourSnippet(). You can dynamically link the generated shared library from Java using: static {System.loadLibrary("libreria.dll");} and declaring method as native : native String getCadena(); When building wrapper header file (with javah) you'll see in .h file type corresponging : JNIEXPORT jstring JNICALL <>_getCadena (JNIEnv *, jobject) You just have to write a C file including the generated header file you builded with javah, and implement <>get_Cadena function with: return yourSnippet(); . Just compile and link them as shared library 'libreria.dll' and you got it! I hope helped you. Best Regards.
-
Having a C++ written class, you need to convert it into a shared library (so or DLL); for example, suppose you can embed the snippet you are using in C++ (the one you posted before) into a function like LPSTR yourSnippet(). You can dynamically link the generated shared library from Java using: static {System.loadLibrary("libreria.dll");} and declaring method as native : native String getCadena(); When building wrapper header file (with javah) you'll see in .h file type corresponging : JNIEXPORT jstring JNICALL <>_getCadena (JNIEnv *, jobject) You just have to write a C file including the generated header file you builded with javah, and implement <>get_Cadena function with: return yourSnippet(); . Just compile and link them as shared library 'libreria.dll' and you got it! I hope helped you. Best Regards.
Skynet_Code wrote:
Having a C++ written class, you need to convert it into a shared library (so or DLL);
Jinterop is a Java library that by its very nature provides direct access to existing shared libraries. It does that by using dynamic access (via OS calls) in it own jni library. Thus one does NOT need to create another shared library when using that API. Jnative is another example of the same sort of thing.
-
You are right jschell, i posted it at oracle forums too, the thing is no one was actually helping at the J-Interop forum, so i have been posting at some other good forums for help etc... My only aim is to read a string from a cpp function, doesn't matter if i am using a wrapper. At least if someone could help me how to do this using Native or Standard Java without a wrapper like an example or something. Maybe i can proceed from there.