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. Mobile Development
  3. Mobile
  4. Some problen in converting char * to LPCWSTR

Some problen in converting char * to LPCWSTR

Scheduled Pinned Locked Moved Mobile
helpios
7 Posts 2 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
    anuhoho
    wrote on last edited by
    #1

    Hi I am facing some problem in the following code .Actually I am not able to print the full data present in my LPCWSTR .Here is the code

    ....
    SYSTEMTIME current_time;.

    GetLocalTime(¤t_time);

    char *str =new char[2000];

    sprintf(str, "%4d/%2d/%2d %2d:%2d:%2d:%2d\n", current_time.wYear,current_time.wMonth,current_time.wDay,current_time.wHour,current_time.wMinute,current_time.wSecond,current_time.wMilliseconds);

    int i;

    wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));

    i = mbstowcs( pwc, str, sizeof(str) );

    WriteToLogFile(pwc);

    delete [] str;

    In the log file I just want to print the year date and time but only the year gets printed. Here is the log file

    void WriteToLogFile(LPCWSTR strData)
    {
    try
    {
    ofstream myfile;
    myfile.open("\\MyData\\Log.txt",ios::app);

        int iSize = WideCharToMultiByte(CP\_ACP,0,strData, -1, NULL, 0, NULL, NULL );
    
        char\* lpNarrow = new char\[iSize\];
    
            lpNarrow\[0\] = 0;
    
        iSize= WideCharToMultiByte(CP\_ACP, 0,strData, -1, lpNarrow, iSize, NULL, NULL );
    
    	
    
        myfile<
    

    Any help on this..

    Thanks in advance

    C 2 Replies Last reply
    0
    • A anuhoho

      Hi I am facing some problem in the following code .Actually I am not able to print the full data present in my LPCWSTR .Here is the code

      ....
      SYSTEMTIME current_time;.

      GetLocalTime(¤t_time);

      char *str =new char[2000];

      sprintf(str, "%4d/%2d/%2d %2d:%2d:%2d:%2d\n", current_time.wYear,current_time.wMonth,current_time.wDay,current_time.wHour,current_time.wMinute,current_time.wSecond,current_time.wMilliseconds);

      int i;

      wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));

      i = mbstowcs( pwc, str, sizeof(str) );

      WriteToLogFile(pwc);

      delete [] str;

      In the log file I just want to print the year date and time but only the year gets printed. Here is the log file

      void WriteToLogFile(LPCWSTR strData)
      {
      try
      {
      ofstream myfile;
      myfile.open("\\MyData\\Log.txt",ios::app);

          int iSize = WideCharToMultiByte(CP\_ACP,0,strData, -1, NULL, 0, NULL, NULL );
      
          char\* lpNarrow = new char\[iSize\];
      
              lpNarrow\[0\] = 0;
      
          iSize= WideCharToMultiByte(CP\_ACP, 0,strData, -1, lpNarrow, iSize, NULL, NULL );
      
      	
      
          myfile<
      

      Any help on this..

      Thanks in advance

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      anuhoho wrote:

      wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));

      Maybe you should allocate more than 2 bytes for your string. ;P

      Greetings Covean

      A 1 Reply Last reply
      0
      • A anuhoho

        Hi I am facing some problem in the following code .Actually I am not able to print the full data present in my LPCWSTR .Here is the code

        ....
        SYSTEMTIME current_time;.

        GetLocalTime(¤t_time);

        char *str =new char[2000];

        sprintf(str, "%4d/%2d/%2d %2d:%2d:%2d:%2d\n", current_time.wYear,current_time.wMonth,current_time.wDay,current_time.wHour,current_time.wMinute,current_time.wSecond,current_time.wMilliseconds);

        int i;

        wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));

        i = mbstowcs( pwc, str, sizeof(str) );

        WriteToLogFile(pwc);

        delete [] str;

        In the log file I just want to print the year date and time but only the year gets printed. Here is the log file

        void WriteToLogFile(LPCWSTR strData)
        {
        try
        {
        ofstream myfile;
        myfile.open("\\MyData\\Log.txt",ios::app);

            int iSize = WideCharToMultiByte(CP\_ACP,0,strData, -1, NULL, 0, NULL, NULL );
        
            char\* lpNarrow = new char\[iSize\];
        
                lpNarrow\[0\] = 0;
        
            iSize= WideCharToMultiByte(CP\_ACP, 0,strData, -1, lpNarrow, iSize, NULL, NULL );
        
        	
        
            myfile<
        

        Any help on this..

        Thanks in advance

        C Offline
        C Offline
        Covean
        wrote on last edited by
        #3

        In addition to my first post this

        anuhoho wrote:

        i = mbstowcs( pwc, str, sizeof(str) );

        copies only 4(32bit)/8(64bit) chars to your 2 wchar_t long string, because the call sizeof(str) returns the length of an char* and not the length of the string.

        Greetings Covean

        A 1 Reply Last reply
        0
        • C Covean

          anuhoho wrote:

          wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));

          Maybe you should allocate more than 2 bytes for your string. ;P

          Greetings Covean

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

          SO what you suggest seeing the above code,.what should i give

          1 Reply Last reply
          0
          • C Covean

            In addition to my first post this

            anuhoho wrote:

            i = mbstowcs( pwc, str, sizeof(str) );

            copies only 4(32bit)/8(64bit) chars to your 2 wchar_t long string, because the call sizeof(str) returns the length of an char* and not the length of the string.

            Greetings Covean

            A Offline
            A Offline
            anuhoho
            wrote on last edited by
            #5

            what you suggest then

            C 1 Reply Last reply
            0
            • A anuhoho

              what you suggest then

              C Offline
              C Offline
              Covean
              wrote on last edited by
              #6

              Something like this:

              int nStrLen = strlen(str);
              wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t) * (nStrLen + 1));
              i = mbstowcs( pwc, str, nStrLen );

              Hope this work, couldn't test it.

              Greetings Covean

              A 1 Reply Last reply
              0
              • C Covean

                Something like this:

                int nStrLen = strlen(str);
                wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t) * (nStrLen + 1));
                i = mbstowcs( pwc, str, nStrLen );

                Hope this work, couldn't test it.

                Greetings Covean

                A Offline
                A Offline
                anuhoho
                wrote on last edited by
                #7

                thnx it worked

                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