Solved

How to make an XP MFC app Windows 7 aware?

Posted on 2011-03-02
3
517 Views
Last Modified: 2013-12-04
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?
0
Comment
Question by:yossikally
3 Comments
 
LVL 2

Expert Comment

by:gobikannan
ID: 35024426
current (user) account has not an administrator. So better u set your account as administrator rights.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35024507
Ideally you should modify your application to not require administrative privileges - I think the real point of UAC is to encourage developers to write applications that don't unnecessarily depend on administrator access.

You can also add a UAC manifest to your application (see step 6 in this guide: http://msdn.microsoft.com/en-us/library/bb756973.aspx).  The UAC manifest can tell Windows that your program needs to be Run as Administrator, and users will automatically receive the prompt to run elevated without needing to explicitly select the Run As Admin menu option - although there is nothing you can do about bypassing that prompt.
0
 
LVL 4

Expert Comment

by:parnasso
ID: 35024928
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to get a file from SqlServer DB 4 29
Query datatable in LINQ 6 39
Problem to open Excel file 15 40
Long URL in SMS 6 24
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now