C代码始终以管理员权限运行应用程序

时间:2019-08-20 05:48:48

标签: c permissions

我试图编写C代码以仅在Windows的管理员模式下启动/打开应用程序。如果用户以非管理员模式启动/打开应用程序,则不应启动/打开应用程序。但是我没有任何线索。谁能帮我解决这个问题?从昨天开始,我为此感到挣扎。但是我没有得到想要作为代码的正确答案。

我尝试了以下语法,

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

现有代码:

    #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;
      }

以上代码已存在。截至目前,Application均以两种模式启动。但是我需要以这种方式进行修改,以便仅在管理员模式下启动。谁能帮我这个忙。

0 个答案:

没有答案