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. Back slashes '\' and SHGetSpecialFolderPath

Back slashes '\' and SHGetSpecialFolderPath

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasequestion
14 Posts 3 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 Achim Klein

    Hi, if you want to get the path of your application, just access your

    theApp.m_pszHelpFilePath

    member. Its extension is "hlp". Just cahnge it to "exe". Regards


    We can do no great things, only small things with great love. - Mother Theresa

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

    It's not my application actually.. There might be several applications whose "theApp" I don't have.

    A 2 Replies Last reply
    0
    • C caykahve

      It's not my application actually.. There might be several applications whose "theApp" I don't have.

      A Offline
      A Offline
      Achim Klein
      wrote on last edited by
      #4

      Sorry, I missed the point. If your want to wrap your path name, try something like that:

      // puts the passed text into quotation marks
      CString getWrapped(const CString& Text, char Esc)
      {
      CString buffer;

      int size = Text.GetLength();
      
      buffer += '"';
      
      for(int i = 0; i < size; i++)
      {
      	char c = Text\[i\];
      
      	// insert escape character
      	if ((c == '"') || (c == Esc))
      	{
      		buffer += Esc;
      	}
      
      	buffer += c;
      }
      
      buffer += '"';
      
      return buffer;
      

      }

      CString test = getWrapped(path, '\\');

      Is this your question? Regards


      We can do no great things, only small things with great love. - Mother Theresa

      1 Reply Last reply
      0
      • C caykahve

        It's not my application actually.. There might be several applications whose "theApp" I don't have.

        A Offline
        A Offline
        Achim Klein
        wrote on last edited by
        #5

        The example I posted recently was previously developed for the std::string class. If you are just using CString objects wrapping is even easier:

        CString test;
        test += "\"";
        test += path;
        test += "\"";
        test.Replace("\\", "\\\\");

        Regards


        We can do no great things, only small things with great love. - Mother Theresa

        C 1 Reply Last reply
        0
        • A Achim Klein

          The example I posted recently was previously developed for the std::string class. If you are just using CString objects wrapping is even easier:

          CString test;
          test += "\"";
          test += path;
          test += "\"";
          test.Replace("\\", "\\\\");

          Regards


          We can do no great things, only small things with great love. - Mother Theresa

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

          I do not want to put quotation marks to both ends of the text. I want to convert a string (i.e. CString) like C:\Program Files to C:\\Program Files - which is doubling the back slashes... Regards

          A 1 Reply Last reply
          0
          • C caykahve

            I do not want to put quotation marks to both ends of the text. I want to convert a string (i.e. CString) like C:\Program Files to C:\\Program Files - which is doubling the back slashes... Regards

            A Offline
            A Offline
            Achim Klein
            wrote on last edited by
            #7

            Ok, that's a one-liner :) If you have

            CString test("C:\\Program Files");

            just call

            test.Replace("\\", "\\\\");

            Regards


            We can do no great things, only small things with great love. - Mother Theresa

            C 1 Reply Last reply
            0
            • A Achim Klein

              Ok, that's a one-liner :) If you have

              CString test("C:\\Program Files");

              just call

              test.Replace("\\", "\\\\");

              Regards


              We can do no great things, only small things with great love. - Mother Theresa

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

              When the string has a backslash it acts differently. I know that to add a quotation mark, you need a backslash in front of it. CString::Insert and CString::operator + act different when there is a backslash in the CString object. I'm sorry to say that I could not get a result with the code you sent either. Regards -- modified at 7:48 Monday 29th August, 2005

              A 2 Replies Last reply
              0
              • C caykahve

                When the string has a backslash it acts differently. I know that to add a quotation mark, you need a backslash in front of it. CString::Insert and CString::operator + act different when there is a backslash in the CString object. I'm sorry to say that I could not get a result with the code you sent either. Regards -- modified at 7:48 Monday 29th August, 2005

                A Offline
                A Offline
                Achim Klein
                wrote on last edited by
                #9

                Ok, please give me a moment...


                We can do no great things, only small things with great love. - Mother Theresa

                1 Reply Last reply
                0
                • C caykahve

                  When the string has a backslash it acts differently. I know that to add a quotation mark, you need a backslash in front of it. CString::Insert and CString::operator + act different when there is a backslash in the CString object. I'm sorry to say that I could not get a result with the code you sent either. Regards -- modified at 7:48 Monday 29th August, 2005

                  A Offline
                  A Offline
                  Achim Klein
                  wrote on last edited by
                  #10

                  I'm a bit confused... I can't find a difference between CString::Insert and CString::operator+ I sent you an email with the example I have coded...


                  We can do no great things, only small things with great love. - Mother Theresa

                  C 1 Reply Last reply
                  0
                  • A Achim Klein

                    I'm a bit confused... I can't find a difference between CString::Insert and CString::operator+ I sent you an email with the example I have coded...


                    We can do no great things, only small things with great love. - Mother Theresa

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

                    I see that it works with your code, but not with the code I have, which is similar to yours (see my initial message). I don't understand. Thanks and regards

                    A 1 Reply Last reply
                    0
                    • C caykahve

                      I see that it works with your code, but not with the code I have, which is similar to yours (see my initial message). I don't understand. Thanks and regards

                      A Offline
                      A Offline
                      Achim Klein
                      wrote on last edited by
                      #12

                      Hi, this time I implemented your posted code back-to-back and got the same error. The problem is related to the CString::GetBuffer() method. CString::GetBufferSetLength() should fix the problem. But I would prefer:

                      char buffer[_MAX_PATH];

                      SHGetSpecialFolderPath(NULL, buffer, CSIDL_PROGRAM_FILES, FALSE);

                      CString PFPath(buffer);

                      Regards


                      We can do no great things, only small things with great love. - Mother Theresa

                      C 1 Reply Last reply
                      0
                      • A Achim Klein

                        Hi, this time I implemented your posted code back-to-back and got the same error. The problem is related to the CString::GetBuffer() method. CString::GetBufferSetLength() should fix the problem. But I would prefer:

                        char buffer[_MAX_PATH];

                        SHGetSpecialFolderPath(NULL, buffer, CSIDL_PROGRAM_FILES, FALSE);

                        CString PFPath(buffer);

                        Regards


                        We can do no great things, only small things with great love. - Mother Theresa

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

                        Thank you very much, that works. Regards

                        1 Reply Last reply
                        0
                        • C caykahve

                          Hi, I have a problem with using a path. I have to obtain the program files' path programatically, therefore I use this function.

                          // Get Program Files Path
                          CString PFPath;
                          SHGetSpecialFolderPath(NULL,PFPath.GetBuffer(_MAX_PATH),

                          CSIDL_PROGRAM_FILES, FALSE);
                          MessageBox(PFPath);

                          and the message box says: C:\Program Files I need to add something at the end of this string and then i try to use it in another function which needs the paths in double back slashes like: "C:\\Program Files" Then I try to do a conversion in PFPath in order to double the backslashes to change from "C:\Program Files" to "C:\Program Files"

                          // double
                          int i=0;
                          i = PFPath.Find("\\",i);
                          while( i>-1 )
                          {
                          CString found;
                          found.Format("Backslash found at index: %d",i);
                          MessageBox(found);
                          //Insert a \ in front of the found one to double it
                          PFPath.Insert(i,(CString)"\\");
                          MessageBox(PFPath);
                          //Get the next one
                          i += 2;
                          i = PFPath.Find("\\",i);
                          }
                          if(i==-1)
                          MessageBox("No more backslashes");
                          MessageBox(PFPath);

                          The message box output of this path is: Backslash found at index: 2 \ No more backslashes \ As seen, the insert function turned the whole string into a single backslash instead of inserting one.

                          // extend the path
                          CString WholePath = PFPath + (CString)"\\some_folder\\myfile.exe";
                          SHELLEXECUTEINFO shellInfo;
                          ...
                          shellInfo.lpFile = WholePath;
                          ::ShellExecuteEx(&shellInfo);

                          The conversion cannot be done properly, and shellInfo.lpFile needs double slashes. Therefore ShellExecuteEx cannot receive the path. I have also tried the '\' conversion issue with CString::Replace; CString::left, CString::Right and concetanation with operator +. Now I'm almost convinced that it is not possible to do it with the CString class. :confused: Is there any other way to convert the '\' to '\\'? Regards and thanks in advance, Caykahve -- modified at 5:49 Monday 29th August, 2005

                          J Offline
                          J Offline
                          Jose Lamas Rios
                          wrote on last edited by
                          #14

                          caykahve wrote: and the message box says: C:\Program Files I need to add something at the end of this string and then i try to use it in another function which needs the paths in double back slashes like: "C:\\Program Files" Then I try to do a conversion in PFPath in order to double the backslashes to change from "C:\Program Files" to "C:\Program Files" You don't actually need any conversion. The double back slash is needed only when you are writing a string literal in your code. That's because the back slash is a escape character (used in combinations like \t, \r and \n, to represent a tab, carrier return, and line feed characters), when what you want is an actual back slash character, you need to type it twice. However, the double back slash pair represents one (single) back slash character in memory. -- jlr http://jlamas.blogspot.com/[^]

                          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