Link to home
Start Free TrialLog in
Avatar of yossikally
yossikally

asked on

How to make an XP MFC app Windows 7 aware?

I have an MFC application that I built and miantain on XP.
It runs on all Windows platforms, including Windows 7.
When its running under Windows 7, I would like it to run with elevated rights (Run as admin) without the user having to explicitly select run as admin option in the menu.

Any ideas how to do it?
Avatar of gobikannan
gobikannan
Flag of India image

current (user) account has not an administrator. So better u set your account as administrator rights.
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In order to fit your needs, you can ask the system for admin priviledges and self elevation programmatically when running in Windows Vista or Windows 7.

FInd the following code as an example:

OSVERSIONINFO osver = { sizeof(osver) };
if (GetVersionEx(&osver) && osver.dwMajorVersion >= 6)
{
        // Running Windows Vista or later (major version >= 6).

	WCHAR szPath[MAX_PATH];
         if (GetModuleFileName(NULL, szPath, MAX_PATH))
         {
               // Launch itself as administrator.
               SHELLEXECUTEINFO sei = { sizeof(sei) };
               sei.lpVerb = L"runas";
               sei.lpFile = szPath;
               sei.hwnd = hWnd;
               sei.nShow = SW_NORMAL;

               if (!ShellExecuteEx(&sei))
               {
                    DWORD dwError = GetLastError();
                    if (dwError == ERROR_CANCELLED)
                    {
                            // The user refused the elevation.
                            // Do someting ...
                        }
                    }
                 }
            }
}

Open in new window


Hope this is helpful