Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C++

Detecting WRP files and Registry on Windows Vista

Rate me:
Please Sign up or sign in to vote.
2.39/5 (8 votes)
24 Aug 2010Public Domain2 min read 25.5K   127   5   3
This program helps installers to check whether files and registry keys are included in WRP or not

Introduction

This article presents a program to determine if files and registry entries to be installed are protected by Windows Resource Protection (WRP)[^].

Background

There appears to be tremendous confusion among developers as to what resources are protected by Windows Resource Protection (WRP) and which are not. This becomes very important when applying for VistaTM logo compliance. Note that using this program will not guarantee certification, but it is just another tool to help detect WRP problems.

Using the code

The code is very simple C++ code. It makes use of windows API's SfcIsKeyProtected and SfcIsFileProtected from SFC.dll to determine whether a file is protected by WRP or not. Note that SfcIsKeyProtected definitely will not run on Windows XPTM since the function is not available in the XP version of sfc.dll. It does, however, run quite well in VistaTM. Here is the code

//
// To detect Keys
//
BOOL loadModule_RunSfcIsKeyProtected (HKEY keyRoot, wchar_t* keyPath)
{
    HINSTANCE hinstLib;
    MYPROC_IsKey ProcAdd;
    REGSAM reg = 0;
    BOOL retValue = 1;

    hinstLib = LoadLibrary (L"sfc.dll");

    if (hinstLib != NULL)
    {
        ProcAdd = (MYPROC_IsKey) GetProcAddress(hinstLib, "SfcIsKeyProtected");

        if (ProcAdd != NULL)
        {
            retValue = ProcAdd (keyRoot, keyPath, 0);
        }
        else
            printf ("Registry, Root: %ls, Key: %ls, SfcIsKeyProtected function cannot be found",
                L"Root", keyPath);

        BOOL fFreeResult = FreeLibrary(hinstLib);

    }
    else
        printf ("Registry, Root: %ls, Key: %ls, SFC.DLL cannot be found", L"Root", keyPath);
    return retValue;
}

// And to detect files
BOOL loadModule_RunSfcIsFileProtected (LPTSTR fileName)
{
    HINSTANCE hinstLib;
    MYPROC_IsFile ProcAdd;
    BOOL retValue = 1;

    hinstLib = LoadLibrary (L"sfc.dll");

    if (hinstLib != NULL)
    {
        ProcAdd = (MYPROC_IsFile) GetProcAddress(hinstLib, "SfcIsFileProtected");

        if (ProcAdd != NULL)
        {
            retValue = ProcAdd (0, fileName);
        }
        else
            printf ("File, %ls, SfcIsFileProtected function cannot be found\n", fileName);

        BOOL fFreeResult = FreeLibrary(hinstLib);
    }
    else
        printf ("File, %ls, SFC.DLL cannot be found\n", fileName);
    return retValue;
}

The above is the complete code, so just open Visual Studio, make a Win32 console application and copy and paste the code there. You can then compile it to make your own program file (.exe)

Points of Interest

To run the program use the command line:

  • WRPDetectionProgram.exe fileName
    OR
  • WRPDetectionProgram.exe RootKey

The program outputs "TRUE WRP" if the file or registry key is a WRP resource and may not be altered by an installer (of course, except when Trusted once is specified). If the file is not a WRP resource, the output is "FALSE WRP".

The output is comma separated, which is very useful. I generally make a list of all the registry keys and files to be installed using some other program. Sometimes I parse the output of appverifier and sometimes use a total uninstall application. Next I put the list into the following format in a batch file:

WRPDetectionProgram.exe filename1 >> Evaluation.csv
WRPDetectionProgram.exe filename2 >> Evaluation.csv 
WRPDetectionProgram.exe ROOT1 KEY1 >> Evaluation.csv 
WRPDetectionProgram.exe ROOT2 KEY2 >> Evaluation.csv

With the executable and batch file in the same folder, running the .bat file append the results to the .csv file. The .csv file can then be opened in ExcelTM and sorted or organised as desired, then saved as an Excel worklbook and the job is done.

Isn't that simple?

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGetting error Pin
Komal Mangal10-May-16 3:42
Komal Mangal10-May-16 3:42 
GeneralMy vote of 2 Pin
Ajay Vijayvargiya25-Aug-10 19:35
Ajay Vijayvargiya25-Aug-10 19:35 
GeneralJust in case anyone wonders... Pin
Axel Rietschin17-Jul-08 16:13
professionalAxel Rietschin17-Jul-08 16:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.