Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 640535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:57:50+00:00 2026-05-13T20:57:50+00:00

Is it possible to apply (and remove) Windows group policy settings using .NET? I

  • 0

Is it possible to apply (and remove) Windows group policy settings using .NET?

I am working on an application that needs to temporarily put a machine into a restricted, kiosk-like state. One of the things I need to control is access to USB drives which I believe I can do through group policy. I’d like my app to set the policy when it starts and revert the change when it exits… is this something I can do through .NET framework calls?

These are my primary requirements:

  • Apply group policy settings when my console app is started.
  • Identify when a user action is denied by the policy and log it.
    • Logging to the system security log is acceptable.
  • Revert my policy changes when my app stops.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:57:50+00:00Added an answer on May 13, 2026 at 8:57 pm

    Try using IGroupPolicyObject

    bool SetGroupPolicy(HKEY hKey, LPCTSTR subKey, LPCTSTR valueName, DWORD dwType, const BYTE* szkeyValue, DWORD dwkeyValue)
    {
        CoInitialize(NULL);
        HKEY ghKey, ghSubKey, hSubKey;
        LPDWORD flag = NULL;
        IGroupPolicyObject *pGPO = NULL;
        HRESULT hr = CoCreateInstance(CLSID_GroupPolicyObject, NULL, CLSCTX_ALL, IID_IGroupPolicyObject, (LPVOID*)&pGPO);
    
        if(!SUCCEEDED(hr))
        {
            MessageBox(NULL, L"Failed to initialize GPO", L"", S_OK);
        }
    
        if (RegCreateKeyEx(hKey, subKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, flag) != ERROR_SUCCESS)
        {
            return false;
            CoUninitialize();
        }
    
        if(dwType == REG_SZ)
        {
            if(RegSetValueEx(hSubKey, valueName, 0, dwType, szkeyValue, strlen((char*)szkeyValue) + 1) != ERROR_SUCCESS)
            {
                RegCloseKey(hSubKey);
                CoUninitialize();
                return false;
            }
        }
    
        else if(dwType == REG_DWORD)
        {
            if(RegSetValueEx(hSubKey, valueName, 0, dwType, (BYTE*)&dwkeyValue, sizeof(dwkeyValue)) != ERROR_SUCCESS)
            {
                RegCloseKey(hSubKey);
                CoUninitialize();
                return false;
            }
        }
    
        if(!SUCCEEDED(hr))
        {
            MessageBox(NULL, L"Failed to initialize GPO", L"", S_OK);
            CoUninitialize();
            return false;
        }
    
        if(pGPO->OpenLocalMachineGPO(GPO_OPEN_LOAD_REGISTRY) != S_OK)
        {
            MessageBox(NULL, L"Failed to get the GPO mapping", L"", S_OK);
            CoUninitialize();
            return false;
        }
    
        if(pGPO->GetRegistryKey(GPO_SECTION_USER,&ghKey) != S_OK)
        {
            MessageBox(NULL, L"Failed to get the root key", L"", S_OK);
            CoUninitialize();
            return false;
        }
    
        if(RegCreateKeyEx(ghKey, subKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &ghSubKey, flag) != ERROR_SUCCESS)
        {
            RegCloseKey(ghKey);
            MessageBox(NULL, L"Cannot create key", L"", S_OK);
            CoUninitialize();
            return false;
        }
    
        if(dwType == REG_SZ)
        {
            if(RegSetValueEx(ghSubKey, valueName, 0, dwType, szkeyValue, strlen((char*)szkeyValue) + 1) != ERROR_SUCCESS)
            {
                RegCloseKey(ghKey);
                RegCloseKey(ghSubKey);
                MessageBox(NULL, L"Cannot create sub key", L"", S_OK);
                CoUninitialize();
                return false;
            }
        }
    
        else if(dwType == REG_DWORD)
        {
            if(RegSetValueEx(ghSubKey, valueName, 0, dwType, (BYTE*)&dwkeyValue, sizeof(dwkeyValue)) != ERROR_SUCCESS)
            {
                RegCloseKey(ghKey);
                RegCloseKey(ghSubKey);
                MessageBox(NULL, L"Cannot set value", L"", S_OK);
                CoUninitialize();
                return false;
            }
        }
    
        if(pGPO->Save(false, true, const_cast<GUID*>(&EXTENSION_GUID), const_cast<GUID*>(&CLSID_GPESnapIn)) != S_OK)
        {
            RegCloseKey(ghKey);
            RegCloseKey(ghSubKey);
            MessageBox(NULL, L"Save failed", L"", S_OK);
            CoUninitialize();
            return false;
        }
    
        pGPO->Release();
        RegCloseKey(ghKey);
        RegCloseKey(ghSubKey);
        CoUninitialize();
        return true;
    }
    

    You can call this function like this..

    // Remove the Log Off in start menu
    SetGroupPolicy(HKEY_CURRENT_USER,
        L"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer",
        L"StartMenuLogOff", REG_DWORD, NULL, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to apply / create a YUI Button by using an element's
In vs2008, is it possible to write an extension methods which would apply to
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: How do you send email from a Java app using Gmail? How
I know how to set up a local webserver using xampp on windows... I
Can anybody help me out to know the possible reasons for which Apple store
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.