xuyanzc
2015-03-25 09:38
采纳率: 100%
浏览 3.3k

jacob c++里方法的类型是bstr ,为啥JAVA调用时老是出错

[id(4)] HRESULT sayHello2([in] BSTR say, [out, retval] BSTR* result);

ComThread.InitMTA(true);
ActiveXComponent com = new ActiveXComponent("CLSID:FF90EFCA-0856-4159-A9CC-0E7BBC317554") ;

  Dispatch disp = (Dispatch) com.getObject();
  Variant  a = new Variant(11);
  Variant  b = new Variant(2);
  Variant c  =  new Variant();

  c = Dispatch.call(disp, "Add",2,3);
  System.out.println(c.toString());
  Variant v = Dispatch.call(disp, "sayHello2","fuck");

  System.out.println(v.toString());
     Can't map name to dispid: sayHello2
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • orochi717 2015-03-25 10:01

    BSTR 在VC里面是utf16编码, 你传字符串过去要转成那样才行。

    打赏 评论
  • oyljerry 2015-03-25 12:40

    就是宽字符指针,送对应的宽字符串进去。

    打赏 评论
  • xuyanzc 2015-03-26 07:30

    ComThread.InitMTA(true);
    ActiveXComponent com = new ActiveXComponent("CLSID:FF90EFCA-0856-4159-A9CC-0E7BBC317554") ;

      Dispatch disp = (Dispatch) com.getObject();
      Variant  a = new Variant(11);
      Variant  b = new Variant(2);
      Variant c  =  new Variant();
    
      c = Dispatch.call(disp, "Add",2,3);
      System.out.println(c.toString());
    
    
    
       //用新的字符编码生成字符串
      try {
        byte[] bs = "fuck".getBytes();
        String xx = new String(bs, "UTF-8");
        Variant v = Dispatch.call(disp, "sayHello2",xx);
        System.out.println(v.toString());
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     结果正确,
    
    打赏 评论

相关推荐 更多相似问题