Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Equivalent Java Function - having a return type of BSTR (How to implement BSTR* in JAVA?)

Equivalent Java Function - having a return type of BSTR (How to implement BSTR* in JAVA?)

Scheduled Pinned Locked Moved Java
c++questionjavatutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amarasat
    wrote on last edited by
    #1

    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?

    D J 2 Replies Last reply
    0
    • A amarasat

      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?

      D Offline
      D Offline
      David Skelly
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A amarasat

        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?

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • J jschell

          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.

          A Offline
          A Offline
          amarasat
          wrote on last edited by
          #4

          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.

          S J 2 Replies Last reply
          0
          • A amarasat

            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.

            S Offline
            S Offline
            Skynet_Code
            wrote on last edited by
            #5

            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.

            J 1 Reply Last reply
            0
            • S Skynet_Code

              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.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • A amarasat

                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.

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                amarasat wrote:

                the thing is no one was actually helping at the J-Interop forum

                Googling using the following terms seems to provide topics jinterop bstr

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups