0

I am trying to write a c code to launch/open an application only in administrator mode for windows. If user launch/open the application in non-Administrator mode, then it should not launch/open the application. But I am not getting any clue. Can anyone please help me out from this problem? I am struggling in this from yesterday. But I am not getting proper answers which I want as a code.

I have tried the below syntax,

    system("runas /user:lgreyx\Administrator /savecred C:\Program 
    Files\Corporation\TAT6\abc.exe");

Existing code:

    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #define MAX_KEY_LENGTH  255
    #define MAX_VALUE_NAME  16383
    #define MAX_FILEPATH    1024
    void GetExecutablePath(char *configLoc)
    {
      char cCurrentPath[MAX_FILEPATH] = { 0 }, 
      tempLoc[MAX_FILEPATH] = { 0 };
      unsigned int j = 0, Index = 0, pos;
      DWORD size = GetModuleFileNameA(NULL, cCurrentPath, MAX_FILEPATH);
      // Get file path in ASCII
      char *pResult = NULL;

      for (Index = 0; Index < strlen(cCurrentPath); Index++)
      // iterate through characters of buffer
      {
        tempLoc[j++] = cCurrentPath[Index]; // else just add char as normal
      }
      tempLoc[j] = '\0';
      pResult = (char*)strrchr((unsigned char*)tempLoc, '\\');
      if (!pResult)
      return;
      pos = (unsigned int)(pResult - tempLoc + 1);
      strncpy_s(configLoc, MAX_FILEPATH, tempLoc, pos);
    }

    int fileExists(char *file)
    {
      WIN32_FIND_DATA FindFileData;
      HANDLE handle = FindFirstFile(file, &FindFileData);
      int found = handle != INVALID_HANDLE_VALUE;
      if (found)
      {
        FindClose(handle);
      }
      return found;
     }

     int main()
     {
       char  webPagePath[MAX_FILEPATH] = { 0 };
       GetExecutablePath(webPagePath);
       if (strcmp(webPagePath, "") == 0)
          return -1;    
       strcat_s(webPagePath, MAX_FILEPATH, 
       "TATGUI\\abc.html");

        if (fileExists(webPagePath) == 0)
         {
           MessageBox(NULL, webPagePath, "File Not Found", MB_OK | 
           MB_ICONERROR);
           return -1;
         }

        ShellExecute(NULL, "open", webPagePath, NULL, NULL, SW_SHOWNORMAL);

        return 0;
      }

The above code is already existing code. As of now, Application is launching in both mode. But I need to modify in this way so that it should launch only in administrator mode. Can anyone please help me out from this.

Lushi Grey
  • 31
  • 1
  • 5
  • Possible duplicate of [Detect if program is running with full administrator rights](https://stackoverflow.com/questions/4230602/detect-if-program-is-running-with-full-administrator-rights) – Mario The Spoon Aug 20 '19 at 06:27
  • Oh, please format your source in a reasonable way. Is this so hard to do? – the busybee Aug 20 '19 at 08:16
  • sure @the busybee – Lushi Grey Aug 20 '19 at 08:20
  • @thebusybee ,I have corrected the format .let me know if it looks good now. – Lushi Grey Aug 20 '19 at 08:25
  • Better. It's overly complicated, but that's not your question. Did you look into the question and its answers that @MarioTheSpoon pointed you to? I'm sure that you'll find valuable hints there. – the busybee Aug 20 '19 at 08:45
  • Well, it works for me. `#include ` and `IsUserAnAdmin()` give no errors with MinGW, and it returns `TRUE` and `FALSE` as expected. Did you read the documentation at Micro$oft? If you say "it's not working" you also have to say exactly what you tried: source code, compile command, error messages. – the busybee Aug 20 '19 at 09:47
  • Thanks @busybee..It works..I was including header file wrongly.Thanks a ton. – Lushi Grey Aug 21 '19 at 09:19
  • how to mark this as resolved this issue,can anyone please tell. – Lushi Grey Aug 21 '19 at 09:24

0 Answers0