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. General Programming
  3. C / C++ / MFC
  4. Convert BSTR to Const char*

Convert BSTR to Const char*

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++ioscareer
14 Posts 4 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.
  • M manju 123

    Hi all.. As i am new c# .net......I have created a structure in Dll and calling that DLL in vc++ console Application.... I am getting the structure variable in my console application... when i put printf..I am able to see the values.. but when i save it in a text file i am getting buffer value .. Actually i am trying with code.. const _TCHAR *elementName[100]; const _TCHAR *elementValue[100]; Sharepoint_API::StructGetSite HUGEP *pBSTR; hr = SafeArrayAccessData(pICalc->GetData, (void HUGEP* FAR*)&pBSTR); for(i=0; i<pBSTR[0].totAttribute; i++){ elementName [i] = "NAME"; elementValue[i] = (LPCTSTR)pBSTR[i].Atrribute; printf("%d. is %S \n", i+1, elementValue[i]); ofstream TestFile("E:\\Test.txt",ios::app); if(TestFile) { TestFile<<elementValue[i];//"dd";//elementValue[i]; } else if(!TestFile) { } TestFile.close(); In printf I am getting the correct value... but in text file i am getting "h" pBSTR[0].totAttribute is the total count i am getting from structure from dll thank you....very much.. manju

    Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

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

    Probably you've to replace

    TestFile << elementValue[i];

    with

    TestFile.write( (const char*) elementValue[i], _tcslen(elementValue[i])*sizeof(TCHAR));

    since even a wostream would not do the trick. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    M 1 Reply Last reply
    0
    • C CPallini

      Probably you've to replace

      TestFile << elementValue[i];

      with

      TestFile.write( (const char*) elementValue[i], _tcslen(elementValue[i])*sizeof(TCHAR));

      since even a wostream would not do the trick. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      M Offline
      M Offline
      manju 123
      wrote on last edited by
      #3

      Sorry sir... not working.. yet the same result.. I am getting output as hhhhh

      Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

      C 1 Reply Last reply
      0
      • M manju 123

        Sorry sir... not working.. yet the same result.. I am getting output as hhhhh

        Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #4

        What's the input, instead?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        M 1 Reply Last reply
        0
        • C CPallini

          What's the input, instead?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          M Offline
          M Offline
          manju 123
          wrote on last edited by
          #5

          CPallini wrote:

          What's the input, instead?

          Actually i will get the sharepoint folders which are stored in structures in the dll... Actually i must get ///////////////////////////// http://sharepoint/Docs http://sharepoint/News http://sharepoint/Reports http://sharepoint/SearchCenter http://sharepoint/SiteDirectory ///////////////////////////// when i put printf i.e printf("%d. is %S \n", i+1, elementValue[i]); I am getting the value for elementValue[i]

          Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

          C 1 Reply Last reply
          0
          • M manju 123

            Hi all.. As i am new c# .net......I have created a structure in Dll and calling that DLL in vc++ console Application.... I am getting the structure variable in my console application... when i put printf..I am able to see the values.. but when i save it in a text file i am getting buffer value .. Actually i am trying with code.. const _TCHAR *elementName[100]; const _TCHAR *elementValue[100]; Sharepoint_API::StructGetSite HUGEP *pBSTR; hr = SafeArrayAccessData(pICalc->GetData, (void HUGEP* FAR*)&pBSTR); for(i=0; i<pBSTR[0].totAttribute; i++){ elementName [i] = "NAME"; elementValue[i] = (LPCTSTR)pBSTR[i].Atrribute; printf("%d. is %S \n", i+1, elementValue[i]); ofstream TestFile("E:\\Test.txt",ios::app); if(TestFile) { TestFile<<elementValue[i];//"dd";//elementValue[i]; } else if(!TestFile) { } TestFile.close(); In printf I am getting the correct value... but in text file i am getting "h" pBSTR[0].totAttribute is the total count i am getting from structure from dll thank you....very much.. manju

            Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #6

            manju#123 wrote:

            ofstream TestFile("E:\\Test.txt",ios::app);

            Since your writing a wide char text, either you have to use wofstream instead of ofstream or Covert the elementValue[i] to mutibyte using sprintf() or WideCharToMultiByte() before writing..

            manju#123 wrote:

            I am Mnaju

            Correct your signature :)

            nave [OpenedFileFinder]

            C 1 Reply Last reply
            0
            • M manju 123

              CPallini wrote:

              What's the input, instead?

              Actually i will get the sharepoint folders which are stored in structures in the dll... Actually i must get ///////////////////////////// http://sharepoint/Docs http://sharepoint/News http://sharepoint/Reports http://sharepoint/SearchCenter http://sharepoint/SiteDirectory ///////////////////////////// when i put printf i.e printf("%d. is %S \n", i+1, elementValue[i]); I am getting the value for elementValue[i]

              Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #7

              If you need to write a UNICODE file the code I provided works fine but you have to put the proper BOM at the very start of the file, for instance

              TCHAR * szIn = _T("http://sharepoint/Docs");
              ofstream of("foo.txt", ios::out);
              #ifdef _UNICODE
              TCHAR BOM = 0xFEFF;
              of.write((const char *) &BOM, sizeof(TCHAR));
              #endif
              of.write((const char *) szIn, _tcslen(szIn)* sizeof(TCHAR));
              of.close();

              On the other hand, if you need to write a ANSI file then follow Naveen suggestion: use wofstream instead of ofstream.

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              R 1 Reply Last reply
              0
              • N Naveen

                manju#123 wrote:

                ofstream TestFile("E:\\Test.txt",ios::app);

                Since your writing a wide char text, either you have to use wofstream instead of ofstream or Covert the elementValue[i] to mutibyte using sprintf() or WideCharToMultiByte() before writing..

                manju#123 wrote:

                I am Mnaju

                Correct your signature :)

                nave [OpenedFileFinder]

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #8

                Naveen wrote:

                manju#123 wrote: I am Mnaju Correct your signature [Smile] nave [OpenedFileFinder]

                Also 'lokking' should be fixed in the signature. :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                N 1 Reply Last reply
                0
                • C CPallini

                  Naveen wrote:

                  manju#123 wrote: I am Mnaju Correct your signature [Smile] nave [OpenedFileFinder]

                  Also 'lokking' should be fixed in the signature. :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  N Offline
                  N Offline
                  Naveen
                  wrote on last edited by
                  #9

                  ops. I didn't notice :laugh:

                  nave [OpenedFileFinder]

                  1 Reply Last reply
                  0
                  • C CPallini

                    If you need to write a UNICODE file the code I provided works fine but you have to put the proper BOM at the very start of the file, for instance

                    TCHAR * szIn = _T("http://sharepoint/Docs");
                    ofstream of("foo.txt", ios::out);
                    #ifdef _UNICODE
                    TCHAR BOM = 0xFEFF;
                    of.write((const char *) &BOM, sizeof(TCHAR));
                    #endif
                    of.write((const char *) szIn, _tcslen(szIn)* sizeof(TCHAR));
                    of.close();

                    On the other hand, if you need to write a ANSI file then follow Naveen suggestion: use wofstream instead of ofstream.

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #10

                    :suss:

                    Please leave us our small pleasures, they are small, but they are ours! - Mycroft Holmes ^ .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                    C 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      :suss:

                      Please leave us our small pleasures, they are small, but they are ours! - Mycroft Holmes ^ .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #11

                      Rajesh R Subramanian wrote:

                      [Suspicious]

                      :confused:

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      R 1 Reply Last reply
                      0
                      • C CPallini

                        Rajesh R Subramanian wrote:

                        [Suspicious]

                        :confused:

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #12

                        Hint: That's just not an emoticon, it is a hyperlink, in my previous post. A post of mine is downvoted for no good reason (IMHO) in that thread. So, superhero intervention needed.

                        Please leave us our small pleasures, they are small, but they are ours! - Mycroft Holmes ^ .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                        C 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          Hint: That's just not an emoticon, it is a hyperlink, in my previous post. A post of mine is downvoted for no good reason (IMHO) in that thread. So, superhero intervention needed.

                          Please leave us our small pleasures, they are small, but they are ours! - Mycroft Holmes ^ .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #13

                          Rajesh R Subramanian wrote:

                          That's just not an emoticon, it is a hyperlink, in my previous post.

                          OMG: I'm Mr.Dumb (drugz, maybe?). :-D BTW: balanced. :)

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          R 1 Reply Last reply
                          0
                          • C CPallini

                            Rajesh R Subramanian wrote:

                            That's just not an emoticon, it is a hyperlink, in my previous post.

                            OMG: I'm Mr.Dumb (drugz, maybe?). :-D BTW: balanced. :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            R Offline
                            R Offline
                            Rajesh R Subramanian
                            wrote on last edited by
                            #14

                            CPallini wrote:

                            BTW: balanced.

                            Thanks, Mr.Dumb. :-D :laugh:

                            Please leave us our small pleasures, they are small, but they are ours! - Mycroft Holmes ^ .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                            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