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.