Windows 7 software development example: UAC, system version, permissions

  

see Win7 cool features drooling, can not help but want to embrace? Don't worry, Win7's temper is not good. To embrace its new features, our software must marry it - it works fine under Win7. Small and medium-sized software developed by individual developers, the function is relatively simple, and the degree of closeness to the system is generally low. Adding some of the necessary elements of Win7 to run normally is OK!

The necessary conditions for marrying Win7

We have to add the system version detection and installation path selection elements to the software. Why should we consider them?

Required Condition 1: Detection System Version

In the initial stage of operation, the software needs to determine the type and version number of the operating system, and execute specific code according to the type of operating system. The same function is in different operating systems. Different code may be executed on it. Most software does not work properly under Win7 because it is determined by the failure of the operating system version.

When the version detection error occurs, many software will follow the error. Users may find that after double-clicking the software image, nothing is reflected, or you may see a dialog box "You must run on Microsoft Windows XP or Update version", but actually the computer has Win7 installed.

Required condition 2: Adapt to user rights

Win7 runs non-admin account by default, and the permissions are relatively small. The software installation program will write some data to the Program Files, Windows installation directory, and registry including the system disk (the software may save some user data to the system folder or the registry at runtime), if the current user The permissions are not enough, and the software cannot enter Win7 normally.

Small knowledge: UAC is a way to reduce the default permissions of Windows users, which will bring security to users, it can contain some virus attacks to a certain extent - turn off anti-virus software, start Copy and distribute virus processes, inject online game clients, and listen to keyboard operations to steal passwords.

For example, the data seems to be saved successfully, but it cannot be found when the location is written, or when the logout is switched to another Windows user, the saved data cannot be found. The root cause of this type of problem is Win7's UAC mechanism, which reduces the default permissions of Windows users, making it impossible for some common operations to change the system settings and system protected folders.

Tip: When the software wants to combine some functions of the system, you need to call the corresponding API. The proper API call is very important for the software to run. We will start with the Win7 cool function from the next issue and introduce how to call correctly. Win7's latest API, with Win7's unique taskbar personalized Jump List, Icon Overlay, Progress Bar, Tabbed Thumbnail, and Thumbnails Toolbar (Thumbnail Toolbar)......

The necessary elements of "wedding"

Our software should be successfully married to Win7, and the following elements should be added to the software.

1. Code detection operating system version number

Add code to the software to determine the operating system is Win7 or 2008 Server:

C#

if ( Environment.OSVersion.Version > new Version(5, 1))
{MessageBox.Show("Windows 7 or Windows 2008 Server","Operating System",
MessageBoxButtons.OK, MessageBoxIcon.Error); BR> return;
}

2. Try to store data in non-system disk

Software Do not install to the system disk by default. When writing the software installation path, consider this. In addition, you need to add the code to modify the read and write registry in the software, use the key value under HKEY_CURRENT_USER\\Software as the node to store data:

static void Main(string[] args)
{
var registryKey = Registry.CurrentUser.CreateSubKey(
@"Software\\test");
registryKey.SetValue("name", "zswang");
registryKey = Registry.CurrentUser.OpenSubKey( @"Software\\test");
Console.WriteLine(string.Format("{0}={1}\ \ ",
"name", registryKey.GetValue("name"))) ;
}


3. Improve the running permission of the program

If the user enters the system as an administrator and wants to install the software on the system disk, How about? Need to determine the permissions of user permissions, the simple solution is to add a manifest file. To find the file with the same name as the executable file and the extension .manifest in the executable folder, enter the following code:

<?XML version="1.0" encoding="UTF-8" standalone ="yes"?>
<assembly XMLns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0"
processorArchitecture="X86"
name="test"
type="win32"/>
<description>Description of your application</description>
<!-- Identify the Application security requirements. -->
<trustInfo XMLns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
< requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges> ;
</security>
</trustInfo>
</assembly>

Copyright © Windows knowledge All Rights Reserved