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?
Re: How to implement BSTR in Java
Quote:
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.
Re: How to implement BSTR in Java
Quote:
Originally Posted by
keang
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.
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).
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::DeleteProject(/*([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?
Re: How to implement BSTR in Java
Quote:
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.
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.
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);
}
Re: How to implement BSTR in Java
Quote:
Originally Posted by
keang
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?
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
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.