CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2010
    Posts
    18

    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?

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to implement BSTR in Java

    I tried to implement the same function in Java:
    I don't understand what you mean. Are you trying to provide a Java implementation of the C++ code or are you trying to call the C++ code from your Java code.

    Unfortunately the Java code you have shown makes little sense unless you show/explain what a JIString is and what object type 'dispatch' is and what it's callMethod() method does.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2010
    Posts
    18

    Re: How to implement BSTR in Java

    Quote Originally Posted by keang View Post
    I don't understand what you mean. Are you trying to provide a Java implementation of the C++ code or are you trying to call the C++ code from your Java code.

    Hello Keang,
    I am just trying to do a java implementation of the C++ code, i want to write the same function in Java too. I am not calling any Cpp code

    Unfortunately the Java code you have shown makes little sense unless you show/explain what a JIString is and what object type 'dispatch' is and what it's callMethod() method does.
    Forget about the code, my aim is to implement the same function in Java too. In the JI is J-Interop "http://j-interop.org/".

    JISession session = null;
    IJIComObject sysInfoObject = null;
    IJIComObject sysInfoServer = null;
    IJIDispatch dispatch = null;
    String identifier = null;
    ControlSoft2(String[] args) throws JIException, UnknownHostException
    {
    session = JISession.createSession("localhost", "username", "password");
    session.useSessionSecurity(true);
    JIComServer comServer = new JIComServer(JIProgId.valueOf("application prog ID"), "localhost",session);
    sysInfoServer = comServer.createInstance();
    sysInfoObject = (IJIComObject)sysInfoServer.queryInterface("Com server inetrface");
    dispatch = (IJIDispatch)JIObjectFactory.narrowObject(sysInfoObject.queryInterface(IJIDispatch.IID));
    }


    private void PerformOp() throws JIException
    {
    JIString outStr = new JIString("");
    dispatch.callMethod("GetActiveProjectName", new Object[]{outStr});
    System.out.println("Out String = "+outStr.toString();
    }

    Now i have registered my interface into the dispatch object, i am just trying to call a method "GetActiveProjectName" from my com interface.

    My question is:

    Now i just have to call my function GetActiveProjectName(/*([out]*/ BSTR* nameOfProject) with a BSTR pointer which is an output parameter.

    How can i implement this in Java? Do i ahve to use CallBuilder? or use some other pointers etc.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to implement BSTR in Java

    If JIString is based on java.lang.String then it is immutable and so you can't change its contents. You will either have to return the new value from the method or pass in an object that can be changed or pass in a object that has a callback method (normally done by implementing an interface defining the callback method).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Oct 2010
    Posts
    18

    Re: How to implement BSTR in Java

    private void PerformOp() throws JIException
    {
    JIString InputStr= new JIString("Project2");
    dispatch.callMethod("SetActiveProjectName", new Object[]{outStr});
    System.out.println("Out String = "+outStr.toString();
    }

    private void PerformOp() throws JIException
    {
    JIString InputStr= new JIString("Project2");
    dispatch.callMethod("DeleteProject", new Object[]{outStr});
    System.out.println("Out String = "+outStr.toString();
    }


    These two are working fine, new project is strating and projcts are getting deleted. Their Equivalent Cpp functions are

    HRESULT cIntuneServer::SetActiveProjectName(/*([in]*/ BSTR* nameOfProject)

    HRESULT cIntuneServer:eleteProject(/*([in]*/ BSTR* nameOfProject)

    So i think i need to do some extra work to make OutStr accept a value may be make it OuputParam type etc....

    Any ideas?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to implement BSTR in Java

    These two are working fine, new project is strating and projcts are getting deleted.
    These two methods both read from the value you are passing in and do not try to change it. See my previous post for why it can't be changed.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Oct 2010
    Posts
    18

    Re: How to implement BSTR in Java

    Ok i understood what you meant!! But i am new to Java!!


    ""You will either have to return the new value from the method or pass in an object that can be changed""

    ""or pass in a object that has a callback method (normally done by implementing an interface defining the callback method).""

    I don't know how to do this one, do you have an example for any of these methods or actually help me by writing the code for my above example.

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Post Re: How to implement BSTR in Java

    You return the value as follows:
    Code:
    String getProjectName()
    {
    return projectName;
    }
    You change an object passed in as follows:
    MyObject can be an Class or an Interface, it's generally considered better practice to use an Interface.
    Code:
    void getProjectName(MyObject in)
    {
    in.setProjectName(projectName);
    }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Oct 2010
    Posts
    18

    Re: How to implement BSTR in Java

    Quote Originally Posted by keang View Post
    You return the value as follows:
    Code:
    String getProjectName()
    {
    return projectName;
    }
    You change an object passed in as follows:
    MyObject can be an Class or an Interface, it's generally considered better practice to use an Interface.
    Code:
    void getProjectName(MyObject in)
    {
    in.setProjectName(projectName);
    }

    Ok let me understand this!! Are you talking about the Cpp functions, so if i have a Cpp method(implemented for the COM server lets say GetProjectNameForJava) which can return a string value, then i can use this PerfromOp function:


    Code:
    private void PerformOp() throws JIException
    {
    JIString outStr = new JIString("");
    dispatch.callMethod("GetProjectNameForJava", new Object[]{outStr});
    System.out.println("Out String = "+outStr.toString();
    }
    to call GetProjectNameForJava method. Do you think this would then populate outStr with my project name?

  10. #10
    Join Date
    Oct 2010
    Posts
    18

    Re: How to implement BSTR in Java

    Hello Keang, could you please verify this:

    My Cpp Code:

    Code:
    std::string xxxxx::GetActiveProjectNameForJava()
    {
                CComBSTR projectName(L"\\Default\\");
    
                std::string sTarget = OLE2A(projectName);
                return sTarget;
    }
    
    public void PerformOp() throws JIException
    {
    	JIString[] result = dispatch.callMethod("GetActiveProjectNameForJava", new Object[]{});
            System.out.println("Out String = "+result );
    }
    when i compile i am getting this error:
    Code:
    CSFT2.java:49: incompatible types
    found   : void
    required: org.jinterop.dcom.core.JIString[]
                    JIString[] result = dispatch.callMethod("GetActiveProjectNameFor
    Java", new Object[]{});
                                                           ^
    1 error

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to implement BSTR in Java

    The problem is the dispatch.callMethod(). It presumably (I don't know for certain as I haven't seen the code for it) is expecting to call a method with the given name and which accepts a parameter of the given type. Your new method doesn't accept a parameter.

    You either need to find a dispatch method that returns the value returned by the method call or use the second approach I outlined.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured