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. Windows XP Convert SystemDrive Variable e.g. %SystemDrive% - into equivelant C++ code

Windows XP Convert SystemDrive Variable e.g. %SystemDrive% - into equivelant C++ code

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionhelptutorial
29 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.
  • J jackngill

    Hi Victor Many thanks can I take a further liberty in answering your question with yet another question? Your "?" - Did you try GetEnvironmentVariable function (winbase.h) - Win32 apps | Microsoft Docs ? Can I answer with: Is there (of some Kind like for instance Emu8086) that would be able to run C++ code to check if it works & possibly feedback errors compatable that is compatable with XP as my skills in C++ are shall we say are very sadly lacking as suggested in my 1st post. I know what I want to do but don't know how to get there. The short answer is no sorry! I have been researching further & I have found this snippet of code from (To reveal the source click on the link below) C++/CLI Code Snippet - Access current environment directories and logical drives[^] which states this

    // Gets the home drive - %HOMEDRIVE% (possible HOMEDRIVE is related to Windows7)
    // Typical value - C:
    Console::WriteLine("Get Environment Variable: Home Drive " + Environment::GetEnvironmentVariable("HOMEDRIVE"));

    However Line 15 (the last 2 lines) is all one line & it states Writeline which infers output to console? Then could Homedrive be called/injected into this line in the first post

    SetSfcFileException(0, L"c:\\windows\\system32\\calc.exe",-1);

    like this:

    SetSfcFileException(0, L"HOMEDRIVE:\\windows\\system32\\calc.exe",-1);

    I am assuming I would need new headers & would need to know where to insert the code. I am assuming prior to the execution of the code in the 1st post I made (which I did not write), because once the code is executed within the C++ it would remain memory resident to access later to envoke the HOMEDRIVE system variable. Hope I am not blowing smoke here just a theory? P.S. Is the C++ code in post 1 for windows or commandline as the above code I think is for commandline, I wouldn't have thought mixing code from two standpoints would Not be Good? All the best, Jackngill

    V Offline
    V Offline
    Victor Nijegorodov
    wrote on last edited by
    #21

    jackngill wrote:

    I have been researching further & I have found this snippet of code from (To reveal the source click on the link below) C++/CLI Code Snippet - Access current environment directories and logical drives[^]

    The sample from CPP-CLI source has nothing to do with the native C++ code that you seem to be using! Try this:

    TCHAR buffer[30] = {0};
    if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
    {
    TCHAR path[MAX_PATH] = {0};
    _stprintf(path, _T("%s\\windows\\system32\\calc.exe"), buffer);
    TRACE(path); // just to be sure it is what we supposed...
    SetSfcFileException(0, path, -1);
    }

    J 1 Reply Last reply
    0
    • V Victor Nijegorodov

      jackngill wrote:

      I have been researching further & I have found this snippet of code from (To reveal the source click on the link below) C++/CLI Code Snippet - Access current environment directories and logical drives[^]

      The sample from CPP-CLI source has nothing to do with the native C++ code that you seem to be using! Try this:

      TCHAR buffer[30] = {0};
      if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
      {
      TCHAR path[MAX_PATH] = {0};
      _stprintf(path, _T("%s\\windows\\system32\\calc.exe"), buffer);
      TRACE(path); // just to be sure it is what we supposed...
      SetSfcFileException(0, path, -1);
      }

      J Offline
      J Offline
      jackngill
      wrote on last edited by
      #22

      Hi Victor, Sorry about the prior code snippet apologies. I have copied your code and ran it here: https://www.onlinegdb.com/online_c++_compiler[^] Unfortunately it came up with a few errors: Compilation failed due to following error(s). main.cpp:1:1: error: ‘TCHAR’ does not name a type TCHAR buffer[30] = {0}; ^~~~~ main.cpp:2:5: error: expected unqualified-id before ‘if’ if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer))) ^~ Do these error codes mean anything to you? Best Regards David

      V 1 Reply Last reply
      0
      • J jackngill

        Hi Victor, Sorry about the prior code snippet apologies. I have copied your code and ran it here: https://www.onlinegdb.com/online_c++_compiler[^] Unfortunately it came up with a few errors: Compilation failed due to following error(s). main.cpp:1:1: error: ‘TCHAR’ does not name a type TCHAR buffer[30] = {0}; ^~~~~ main.cpp:2:5: error: expected unqualified-id before ‘if’ if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer))) ^~ Do these error codes mean anything to you? Best Regards David

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #23

        I have no idea what compiler you are using... If you compile it as UNICODE then replace TCHAR with wchar_t, _T() macro - with L, and _stprintf - with swprintf. Besides, learn using Google to fast search for such types of errors...

        J 1 Reply Last reply
        0
        • V Victor Nijegorodov

          I have no idea what compiler you are using... If you compile it as UNICODE then replace TCHAR with wchar_t, _T() macro - with L, and _stprintf - with swprintf. Besides, learn using Google to fast search for such types of errors...

          J Offline
          J Offline
          jackngill
          wrote on last edited by
          #24

          Hi Victor, I am using online compiler as I am not setup to compile programs see link here: https://www.onlinegdb.com/online_c++_compiler[^] The file is temp called main.cpp enter the code and depress run small dialogue box at the bottom of the screen generates errors with indicators like with exchanged code as you suggested:

          Compilation failed due to following error(s). main.cpp:1:8: error: expected unqualified-id before ‘,’ token
          wchar_t**,** _T() buffer[30] = {0};
          ^
          main.cpp:1:15: error: expected initializer before ‘buffer’
          wchar_t, _T() buffer[30] = {0};
          ^~~~~~
          main.cpp:2:5: error: expected unqualified-id before ‘if’
          if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
          ^~

          For this:

          wchar_t, _T() buffer[30] = {0};
          if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
          {
          wchar_t, _T() path[MAX_PATH] = {0};
          swprintf(path, _T("%s\windows\system32\calc.exe"), buffer);
          TRACE(path); // just to be sure it is what we supposed...
          SetSfcFileException(0, path, -1);
          }

          Could not find macro - with L in your code. I will research further error codes on WWW & get back to you Best Regards David

          V 1 Reply Last reply
          0
          • J jackngill

            Hi Victor, I am using online compiler as I am not setup to compile programs see link here: https://www.onlinegdb.com/online_c++_compiler[^] The file is temp called main.cpp enter the code and depress run small dialogue box at the bottom of the screen generates errors with indicators like with exchanged code as you suggested:

            Compilation failed due to following error(s). main.cpp:1:8: error: expected unqualified-id before ‘,’ token
            wchar_t**,** _T() buffer[30] = {0};
            ^
            main.cpp:1:15: error: expected initializer before ‘buffer’
            wchar_t, _T() buffer[30] = {0};
            ^~~~~~
            main.cpp:2:5: error: expected unqualified-id before ‘if’
            if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
            ^~

            For this:

            wchar_t, _T() buffer[30] = {0};
            if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
            {
            wchar_t, _T() path[MAX_PATH] = {0};
            swprintf(path, _T("%s\windows\system32\calc.exe"), buffer);
            TRACE(path); // just to be sure it is what we supposed...
            SetSfcFileException(0, path, -1);
            }

            Could not find macro - with L in your code. I will research further error codes on WWW & get back to you Best Regards David

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #25

            jackngill wrote:

            Compilation failed due to following error(s). main.cpp:1:8: error: expected unqualified-id before ‘,’ token
            wchar_t**,** _T() buffer[30] = {0};
            ^
            main.cpp:1:15: error: expected initializer before ‘buffer’
            wchar_t, _T() buffer[30] = {0};
            ^~~~~~
            main.cpp:2:5: error: expected unqualified-id before ‘if’
            if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
            ^~

            For this:

            wchar_t, _T() buffer[30] = {0};
            if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
            {
            wchar_t, _T() path[MAX_PATH] = {0};
            swprintf(path, _T("%s\windows\system32\calc.exe"), buffer);
            TRACE(path); // just to be sure it is what we supposed...
            SetSfcFileException(0, path, -1);
            }

            What the abracadabra you wrote? :thumbsdown: Try

            wchar_t buffer[30] = {0};
            if (::GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, 30))
            {
            wchar_t path[MAX_PATH] = {0};
            swprintf(path, L"%s\windows\system32\calc.exe", buffer);
            TRACE(path); // just to be sure it is what we supposed...
            SetSfcFileException(0, path, -1);
            }

            J 1 Reply Last reply
            0
            • V Victor Nijegorodov

              jackngill wrote:

              Compilation failed due to following error(s). main.cpp:1:8: error: expected unqualified-id before ‘,’ token
              wchar_t**,** _T() buffer[30] = {0};
              ^
              main.cpp:1:15: error: expected initializer before ‘buffer’
              wchar_t, _T() buffer[30] = {0};
              ^~~~~~
              main.cpp:2:5: error: expected unqualified-id before ‘if’
              if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
              ^~

              For this:

              wchar_t, _T() buffer[30] = {0};
              if (::GetEnvironmentVariable(_T("SYSTEMDRIVE"), buffer, _countof(buffer)))
              {
              wchar_t, _T() path[MAX_PATH] = {0};
              swprintf(path, _T("%s\windows\system32\calc.exe"), buffer);
              TRACE(path); // just to be sure it is what we supposed...
              SetSfcFileException(0, path, -1);
              }

              What the abracadabra you wrote? :thumbsdown: Try

              wchar_t buffer[30] = {0};
              if (::GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, 30))
              {
              wchar_t path[MAX_PATH] = {0};
              swprintf(path, L"%s\windows\system32\calc.exe", buffer);
              TRACE(path); // just to be sure it is what we supposed...
              SetSfcFileException(0, path, -1);
              }

              J Offline
              J Offline
              jackngill
              wrote on last edited by
              #26

              The only error now being reported back is this one by the way sorry for the gobble-de-gook code must have cut & paste wrong. main.cpp:2:1: error: expected unqualified-id before ‘if’ if (::GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, 30)) ^~ "If" for some reason is proving a problem but only one error repoted though encouraging it is under the input tab on the compiler. Perhaps an if/else might work? Best Regards, David

              V 1 Reply Last reply
              0
              • J jackngill

                The only error now being reported back is this one by the way sorry for the gobble-de-gook code must have cut & paste wrong. main.cpp:2:1: error: expected unqualified-id before ‘if’ if (::GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, 30)) ^~ "If" for some reason is proving a problem but only one error repoted though encouraging it is under the input tab on the compiler. Perhaps an if/else might work? Best Regards, David

                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #27

                Are you banned by [Google](https://www.google.com/search?q=error%3A+expected+unqualified-id+before+‘if’&oq=error%3A+expected+unqualified-id+before+‘if’&aqs=chrome..69i57j69i58&sourceid=chrome&ie=UTF-8)?

                J 1 Reply Last reply
                0
                • V Victor Nijegorodov

                  Are you banned by [Google](https://www.google.com/search?q=error%3A+expected+unqualified-id+before+‘if’&oq=error%3A+expected+unqualified-id+before+‘if’&aqs=chrome..69i57j69i58&sourceid=chrome&ie=UTF-8)?

                  J Offline
                  J Offline
                  jackngill
                  wrote on last edited by
                  #28

                  @ Victor LOL! Nope I have been using google to try to find some form of explanation I have been trying to input code within the online compiler based on the search engines but although the search error codes are similair there examples do not work for me. I think I am going to draw a line under this endevour as coding is clearly not for me & it is far reach from exchanging one item "C:\\" for "%SystemDrive%" for another in the hope it would work. However many thanks for your patience & help I am very grateful but I have decided to quit. If you want to proceed and complete the code feel free to do so there maybe others who would benefit from it. All the best Victor.

                  V 1 Reply Last reply
                  0
                  • J jackngill

                    @ Victor LOL! Nope I have been using google to try to find some form of explanation I have been trying to input code within the online compiler based on the search engines but although the search error codes are similair there examples do not work for me. I think I am going to draw a line under this endevour as coding is clearly not for me & it is far reach from exchanging one item "C:\\" for "%SystemDrive%" for another in the hope it would work. However many thanks for your patience & help I am very grateful but I have decided to quit. If you want to proceed and complete the code feel free to do so there maybe others who would benefit from it. All the best Victor.

                    V Offline
                    V Offline
                    Victor Nijegorodov
                    wrote on last edited by
                    #29

                    :)

                    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