bo's profileAmosPhotosBlogLists Tools Help

Blog


    May 31

    different type convert

    1.CString to Char *
     
    char* strTo= (char*)malloc(SIZE);
    CString strFrom;
    
    // wide char string to multibyte string
    wcstombs(strTo,strFrom,SIZE);
    
    // don't for get to free char after you finish
    free(strTo);
    2 char * to CString
    char* strFrom;
    CString strTo;
    
    //construct a CString from char*
    strTo = strFrom;

     3 BSTR to CString

    CString strTo;
    BSTR strFrom;
    
    // initialize the BSTR string
    strFrom = SysAllocString(L"some string");
    
    strTo.SetSysString ( &strFrom );
     
    // free the BSTR string
    SysFreeString ( strFrom );

     

     

    4 CString to BSTR

    // initiaslize the CString
    CString strFrom = "some string";
    BSTR strTo  = NULL;
    
    strTo   = strFrom.AllocSysString();
    
    // free the BSTR
    SysFreeString ( strTo );

     

    5 TCHAR * to char*

    char* strTo = (char *)malloc( SIZE );;
    TCHAR* strFrom;
    
    // I got this from MSDN
    for(int i = 0; i < _tcslen(strFrom); i++)
       strTo[i] = (CHAR) strFrom[i];
    
    // another way to do this
    wcstombs(strTo,strFrom, SIZE);
    
    // don't for get to free char after you finish
    free(strTo);
    

     

    6. char * to TCHAR*

    char* strFrom = (char *)malloc( SIZE );;
    TCHAR* strTo;
    
    // I got this from MSDN
    for(int i = 0; i < strlen(strFrom); i++)
       strTo[i] = (TCHAR) strFrom[i];
    
    // another way to do this
    mbstowcs(strTo,strFrom, SIZE);
    
    // don't for get to free char after you finish
    free(strTo);

     

     

    8.wstring to int

    // initialize the basic_string
    wstring strFrom=L"some string";
    int nTo;
    
    // pass the ponter c_str() a class constructer if it accepts this type
    swscanf(strFrom.c_str(),L"%i",&nTo);

     

    9 wstring to CString

    CString strTo;
    
    // initialize the wstring
    wstring strFrom = L"some string";
    
    // pass the pointer c_str() to the Cstring variable
    strTo = strFrom.c_str();
    
    10 char to num
    char* strInt_From = "1234";
    char* strFloat_From = "12.34";
    
    int i = atoi(strInt_From );
    float f = atof(strFloat_From);
     
    11 char to wchar
    char string[150] = "How are you?";
    TCHAR wstring[150];

    MultiByteToWideChar(CP_ACP, 0, string, -1, wstring, 150);
    May 23

    Axis(5)

    The second file is axiscpp.conf , will be modified like

     

    # The comment character is '#'

    #Available directives are as follows

    #(Some of these directives may not be implemented yet)

    #

    #WSDDFilePath:The path to the server wsdd

    #LogPath:The path to the axis log

    #ClientLogPath:The path to the axis client log

    #ClientWSDDFilePath:The path to the client wsdd

    #Transport_http:The HTTP transport library

    #Transport_smtp:The SMTP transport library

    #XMLParser:The xml parser library

    #NodeName:Node name

    #ListenPort:Listening port

    #Channel_HTTP:The HTTP transport channel library

    #Channel_HTTP_SSL:The HTTP transport secure channel library

     

    #LogPath:Axis\logs\AxisLog.txt

    #WSDDFilePath:Axis\conf\server.wsdd

    LogPath:C:\AxisLog.txt

    WSDDFilePath:C:\server.wsdd

    XMLParser:C:\myc++\axis-c-1.6b-Win32-trace-src\lib\axis\XmlLibrary.dll

    Transport_http:C:\axis-c-1.6b-Win32-trace-bin\bin\HTTPTransport.dll

    Channel_HTTP:C:\axis-c-1.6b-Win32-trace-bin\bin\HTTPChannel.dll

    Channel_HTTP_SSL:C:\axis-c-1.6b-Win32-trace-bin\bin\HTTPSSLChannel.dll

     

    Unfortunately, I modified a lot of axis’ code to let my rule service works, if you are familiar with c++ language, you can debug and find some codes need to change. Maybe with new release version, they will fix some bugs and add new functions.

     

    1.  Create java client stub codes

    java -Djava.ext.dirs=c:\axis-c-1.6b-Win32-trace-bin\lib\axisjava -cp C:\axis-c-1.6b-Win32-trace-bin\lib\axis\wsdl4j.jar  org.apache.axis.wsdl.WSDL2Java rule.wsdl

     

                After this command was executed, some java class files should be generated. And you can write one class for testing. Just few lines.

      RuleBeanSetInterfaceServiceLocator locator = new RuleBeanSetInterfaceServiceLocator();

            DefaultNamespace.RuleBean bean = new DefaultNamespace.RuleBean();

           

           

            DefaultNamespace.Param param = new DefaultNamespace.Param();

            param.setName("nametest1");

            param.setType("typetest1");

            DefaultNamespace.Param param1 = new DefaultNamespace.Param();

            param1.setName("nametest2");

            param1.setType("typetest2");

            DefaultNamespace.Param params[] = new DefaultNamespace.Param[2];

            params[0] = param;

            params[1] = param1;

            bean.setParams(params);

            bean.setRuleID("ruleIDtest".getBytes());

            bean.setEventType("eventType".getBytes());

            bean.setInputChanel("inputChanel".getBytes());

            bean.setTemp("temptest");

           

            locator.getrule().addRule(bean);

    If you want to have a look the content of request message, just to add one line.

    System.out.println(locator.getCall().getMessageContext().getRequestMessage().getSOAPPartAsString());

    And you also can print response message.

     

    2.  Run and get resultYou can just run SimpleAxisServer and then run java client, you will see the method in server side should be called and executed successfully.

    Axis(4)

    1.  Create c++ server codes

    java -Djava.ext.dirs=c:\axis-c-1.6b-Win32-trace-bin\lib\axisjava -cp C:\axis-c-1.6b-Win32-trace-bin\lib\axis\wsdl2ws.jar  org.apache.axis.wsdl.wsdl2ws.WSDL2Ws rule.wsdl -lc++ -sserver

         

    After this command was executed, you will get some c++ files.  You can create new dll project and add those files to compile. After compile, if you are luck with no errors , you will get a dll file.

     

    2.  Configure in Axis server

     

    I am using independent server without application server. That is , SimpleAxisServer. For run this, you need configure two files.

     

    One is server.wsdd, like

     

    <?xml version="1.0" encoding="UTF-8"?>

    <deployment name="defaultClientConfig"

                xmlns="http://xml.apache.org/axis/wsdd/"

                xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"

                xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler">

     <globalConfiguration>

                <parameter name="sendMultiRefs" value="true" />

                <parameter name="disablePrettyXML" value="false" />

                <parameter name="adminPassword" value="admin" />

                <parameter name="dotNetSoapEncFix" value="true" />

                <parameter name="enableNamespacePrefixOptimization"

                      value="true" />

                <parameter name="sendXMLDeclaration" value="true" />

                <parameter name="sendXsiTypes" value="true" />

                <parameter name="axis.development.system" value="true" />

       <requestFlow>

         <handler type="C:\myc++\axis-c-1.6b-Win32-trace-src\bin\echoStringHeaderHandler.dll">       

         </handler>    

       </requestFlow>

     </globalConfiguration>

    <service name="Calculator" provider="CPP:RPC" description="Simple Calculator Axis C++ Service">

     

    <!--

    <requestFlow name="CalculatorHandlers">

        <handler name="ESHHandler" type="C:\myc++\axis-c-1.6b-Win32-trace-src\bin\echoStringHeaderHandler.dll">

        </handler>

      </requestFlow>

      <responseFlow name="CalculatorHandlers">

        <handler name="ESHHandler" type="C:\myc++\axis-c-1.6b-Win32-trace-src\bin\echoStringHeaderHandler.dll">

        </handler>

      </responseFlow>

    -->

    <parameter name="allowedMethods" value="add sub mul div"/>

      <parameter name="className" value="C:\myc++\calculator_server\debug\calculator_server.dll" />

    </service>

     

    <service name="rule" provider="CPP:RPC" description="Simple Calculator Axis C++ Service">

    <parameter name="allowedMethods" value="addRule"/>

      <parameter name="className" value="C:\myc++\rule\debug\rule.dll" />

    </service>

     

     <transport name="local">

      <responseFlow>

       <!--

       <handler type="C:\myc++\axis-c-1.6b-Win32-trace-src\bin\echoStringHeaderHandler.dll"/>

       -->

      </responseFlow>

     </transport>

    </deployment>

     You will noticed that a service’s name is rule, that is what we want to test.

    Axis(3)

    I think this is a bug or deficient function, I have to modify this file by hand, after I modified, the file likes like:

    <?xml version="1.0" encoding="UTF-8"?><wsdl:definitions targetNamespace="http://localhost/axis/rule" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost/axis/rule" xmlns:intf="http://localhost/axis/rule" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://DefaultNamespace" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:xsd="http://www.w3.org/2001/XMLSchema

     <wsdl:types>  <schema targetNamespace="http://localhost/axis/rule" xmlns="http://www.w3.org/2001/XMLSchema">   <import namespace="http://DefaultNamespace"/>   <import namespace="http://xml.apache.org/xml-soap"/>  <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>    <complexType name="Param">    <sequence>  <element name="name" nillable="true" type="soapenc:string"/>   <element name="type" nillable="true" type="soapenc:string"/>    </sequence>   </complexType>   <complexType name="ArrayOf_xsd_anyType">    <complexContent>     <restriction base="soapenc:Array">     <attribute ref="soapenc:arrayType" wsdl:arrayType="tns1:Param[]"/>    </restriction>    </complexContent>   </complexType>  </schema>  <schema targetNamespace="http://xml.apache.org/xml-soap" xmlns="http://www.w3.org/2001/XMLSchema">   <import namespace="http://localhost/axis/rule"/>   <import namespace="http://DefaultNamespace"/>  <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>   <complexType name="Vector">

        <sequence>     <element maxOccurs="unbounded" minOccurs="0" name="item" type="xsd:anyType"/>

        </sequence>   </complexType>

      </schema>

      <schema targetNamespace="http://DefaultNamespace" xmlns="http://www.w3.org/2001/XMLSchema">

       <import namespace="http://localhost/axis/rule"/>

       <import namespace="http://xml.apache.org/xml-soap"/>

       <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

        <complexType name="Param">

        <sequence>    

         <element name="name" nillable="true" type="soapenc:string"/>

         <element name="type" nillable="true" type="soapenc:string"/>

        </sequence>

       </complexType>

       <complexType name="RuleBean">

        <sequence><element name="ruleID" nillable="true" type="soapenc:base64Binary"/>

         <element name="eventType" nillable="true" type="soapenc:base64Binary"/>

         <element name="params" nillable="true" type="impl:ArrayOf_xsd_anyType"/>

         <element name="inputChanel" nillable="true" type="soapenc:base64Binary"/>

         <element name="temp" nillable="true" type="soapenc:string"/></sequence></complexType>

      </schema>

     </wsdl:types> <wsdl:message name="addRuleRequest"> <wsdl:part name="in0" type="tns1:RuleBean"/> 

       </wsdl:message>  <wsdl:message name="addRuleResponse">  <wsdl:part name="addRuleReturn" type="xsd:int"/>  </wsdl:message> <wsdl:portType name="RuleBeanSetInterface"> 

          <wsdl:operation name="addRule" parameterOrder="in0"> 

             <wsdl:input message="impl:addRuleRequest" name="addRuleRequest"/>

             <wsdl:output message="impl:addRuleResponse" name="addRuleResponse"/> 

          </wsdl:operation>    </wsdl:portType>    <wsdl:binding name="ruleSoapBinding" type="impl:RuleBeanSetInterface">   <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>       <wsdl:operation name="addRule">      <wsdlsoap:operation soapAction=""/>        <wsdl:input name="addRuleRequest">             <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/axis/rule" use="encoded"/>          </wsdl:input>          <wsdl:output name="addRuleResponse">       <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/axis/rule" use="encoded"/>      </wsdl:output>       </wsdl:operation>    </wsdl:binding> 

       <wsdl:service name="RuleBeanSetInterfaceService">      <wsdl:port binding="impl:ruleSoapBinding" name="rule">          <wsdlsoap:address location="http://localhost/axis/rule"/> 

          </wsdl:port>    </wsdl:service> </wsdl:definitions> You can compare two files’ different to learn wsdl for complex type.

    Axis (2)

    1.  Java2WSDL 

    In cmd console type in

    java -Djava.ext.dirs=c:\axis-c-1.6b-Win32-trace-bin\lib\axisjava -cp C:\axis-c-1.6b-Win32-trace-bin\lib\axis\wsdl4j.jar org.apache.axis.wsdl.Java2WSDL –o rule.wsdl -l"http://localhost/axis/rule " -n"http://localhost/axis/rule " RuleBeanSetInterface

     

    For more information about this tool , please go to Axis’ website. 

    After this command is executed, rule.wsdl will be generated like. 

    <?xml version="1.0" encoding="UTF-8"?>

    <wsdl:definitions targetNamespace="http://localhost/axis/rule" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost/axis/rule" xmlns:intf="http://localhost/axis/rule" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://DefaultNamespace" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <!--WSDL created by Apache Axis version: 1.4

    Built on Apr 22, 2006 (06:55:48 PDT)-->

     <wsdl:types>

      <schema targetNamespace="http://localhost/axis/rule" xmlns="http://www.w3.org/2001/XMLSchema">

       <import namespace="http://DefaultNamespace"/>

       <import namespace="http://xml.apache.org/xml-soap"/>

       <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

       <complexType name="ArrayOf_xsd_anyType">

        <complexContent>

         <restriction base="soapenc:Array">

          <attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:anyType[]"/>

         </restriction>

        </complexContent>

       </complexType>

      </schema>

      <schema targetNamespace="http://xml.apache.org/xml-soap" xmlns="http://www.w3.org/2001/XMLSchema">

       <import namespace="http://localhost/axis/rule"/>

       <import namespace="http://DefaultNamespace"/>

       <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

       <complexType name="Vector">

        <sequence>

         <element maxOccurs="unbounded" minOccurs="0" name="item" type="xsd:anyType"/>

        </sequence>

       </complexType>

      </schema>

      <schema targetNamespace="http://DefaultNamespace" xmlns="http://www.w3.org/2001/XMLSchema">

       <import namespace="http://localhost/axis/rule"/>

       <import namespace="http://xml.apache.org/xml-soap"/>

       <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

       <complexType name="RuleBean">

        <sequence>

         <element name="ruleID" nillable="true" type="soapenc:base64Binary"/>

         <element name="eventType" nillable="true" type="soapenc:base64Binary"/>

         <element name="params" nillable="true" type="impl:ArrayOf_xsd_anyType"/>

         <element name="inputChanel" nillable="true" type="soapenc:base64Binary"/>

        </sequence>

       </complexType>

      </schema>

     </wsdl:types> 

       <wsdl:message name="addRuleRequest"> 

          <wsdl:part name="in0" type="tns1:RuleBean"/> 

       </wsdl:message> 

       <wsdl:message name="addRuleResponse"> 

          <wsdl:part name="addRuleReturn" type="xsd:int"/> 

       </wsdl:message> 

       <wsdl:portType name="RuleBeanSetInterface"> 

          <wsdl:operation name="addRule" parameterOrder="in0"> 

             <wsdl:input message="impl:addRuleRequest" name="addRuleRequest"/> 

             <wsdl:output message="impl:addRuleResponse" name="addRuleResponse"/> 

          </wsdl:operation> 

       </wsdl:portType> 

       <wsdl:binding name="ruleSoapBinding" type="impl:RuleBeanSetInterface"> 

          <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> 

          <wsdl:operation name="addRule"> 

             <wsdlsoap:operation soapAction=""/> 

             <wsdl:input name="addRuleRequest"> 

                <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/axis/rule" use="encoded"/> 

             </wsdl:input> 

             <wsdl:output name="addRuleResponse"> 

                <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/axis/rule" use="encoded"/> 

             </wsdl:output> 

          </wsdl:operation> 

       </wsdl:binding> 

       <wsdl:service name="RuleBeanSetInterfaceService"> 

          <wsdl:port binding="impl:ruleSoapBinding" name="rule"> 

             <wsdlsoap:address location="http://localhost/axis/rule"/> 

          </wsdl:port> 

       </wsdl:service> 

    </wsdl:definitions> 

    I think this is a bug or deficient function, I have to modify this file by hand, after I modified, the file likes like:

    Axis webservice, java client call c++ server (1)

    I want to use webservice as an communication platform from java to c++.  No interesting with easy stuff , I want to pass in a java bean to c++ server side.

    1.       Write java interface first.

    public class Param{

        public String name;

        public String type;   

    }

    public class RuleBean {

     

        public byte[] ruleID;

        public byte[] eventType;

        public List<Param> params;

        public byte[] inputChanel;

        public String temp;

       

    }

    public interface RuleBeanSetInterface {

        public int addRule(RuleBean bean);

    }

    Why I am using public variable directly not using set/get as common java bean, because if declaration of variable is private will cause generated c++ class without any variable. Like

    Class A

    {

    }

    May 09

    just for memo

    import java.io.*;
    
    public class RegQuery {
    
      private static final String REGQUERY_UTIL = "reg query ";
      private static final String REGSTR_TOKEN = "REG_SZ";
      private static final String REGDWORD_TOKEN = "REG_DWORD";
    
      private static final String PERSONAL_FOLDER_CMD = REGQUERY_UTIL +
        "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\" 
         + "Explorer\\Shell Folders\" /v Personal";
      private static final String CPU_SPEED_CMD = REGQUERY_UTIL +
        "\"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\"" 
         + " /v ~MHz";
      private static final String CPU_NAME_CMD = REGQUERY_UTIL +
       "\"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\"" 
         + " /v ProcessorNameString";
    
      public static String getCurrentUserPersonalFolderPath() {
        try {
          Process process = Runtime.getRuntime().exec(PERSONAL_FOLDER_CMD);
          StreamReader reader = new StreamReader(process.getInputStream());
    
          reader.start();
          process.waitFor();
          reader.join();
    
          String result = reader.getResult();
          int p = result.indexOf(REGSTR_TOKEN);
    
          if (p == -1)
             return null;
    
          return result.substring(p + REGSTR_TOKEN.length()).trim();
        }
        catch (Exception e) {
          return null;
        }
      }
    
      public static String getCPUSpeed() {
        try {
          Process process = Runtime.getRuntime().exec(CPU_SPEED_CMD);
          StreamReader reader = new StreamReader(process.getInputStream());
    
          reader.start();
          process.waitFor();
          reader.join();
    
          String result = reader.getResult();
          int p = result.indexOf(REGDWORD_TOKEN);
    
          if (p == -1)
             return null;
    
          // CPU speed in Mhz (minus 1) in HEX notation, convert it to DEC
          String temp = result.substring(p + REGDWORD_TOKEN.length()).trim();
          return Integer.toString
              ((Integer.parseInt(temp.substring("0x".length()), 16) + 1));
        }
        catch (Exception e) {
          return null;
        }
      }
    
      public static String getCPUName() {
        try {
          Process process = Runtime.getRuntime().exec(CPU_NAME_CMD);
          StreamReader reader = new StreamReader(process.getInputStream());
    
          reader.start();
          process.waitFor();
          reader.join();
    
          String result = reader.getResult();
          int p = result.indexOf(REGSTR_TOKEN);
    
          if (p == -1)
             return null;
    
          return result.substring(p + REGSTR_TOKEN.length()).trim();
        }
        catch (Exception e) {
          return null;
        }
      }
    
      static class StreamReader extends Thread {
        private InputStream is;
        private StringWriter sw;
    
        StreamReader(InputStream is) {
          this.is = is;
          sw = new StringWriter();
        }
    
        public void run() {
          try {
            int c;
            while ((c = is.read()) != -1)
              sw.write(c);
            }
            catch (IOException e) { ; }
          }
    
        String getResult() {
          return sw.toString();
        }
      }
    
      public static void main(String s[]) {
        System.out.println("Personal directory : " 
           + getCurrentUserPersonalFolderPath());
        System.out.println("CPU Name : " + getCPUName());
        System.out.println("CPU Speed : " + getCPUSpeed() + " Mhz");
      }
    }