<p>I'm trying to find out if there is any way to elevate a specific function within an application. For example, I have an app with system and user settings that are stored in the registry, I only need elevation for when the system settings need to be changed. </p>

<p>Unfortunately all of the info I've come across talks about only starting a new process with elevated privileges. </p>

[{'answer_id': 78707, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 3, 'selected': True, 'text': '<p>What you really need to do is store your settings the Application Data folder.</p>\n'}, {'answer_id': 78734, 'author': 'Bryant', 'author_id': 10893, 'author_profile': 'https://Stackoverflow.com/users/10893', 'pm_score': 2, 'selected': False, 'text': '<p>Found a <a href="http://msdn.microsoft.com/en-us/magazine/cc163486.aspx" rel="nofollow noreferrer">nice article that covers this here</a>:</p>\n\n<blockquote>\n  <p>Most applications do not require administrator privileges at run time. If your application doesn\'t maintain cross-session state while it executes and doesn\'t do something like modifying the local security policy, it should be just fine running with a standard-user token. Sometimes certain parts of your application will require administrator privileges, and you should separate out those pieces into a separate process. I\'ll get into that a little later. </p>\n</blockquote>\n\n<p>Looks like the article is talking about using C++, so I found <a href="http://buildingsecurecode.blogspot.com/2007/06/how-to-display-uac-shield-icon-inside.html" rel="nofollow noreferrer">another article</a> that covers how to call this code using P/Invoke. So should be doable from .NET.</p>\n'}, {'answer_id': 78773, 'author': 'uzbones', 'author_id': 14551, 'author_profile': 'https://Stackoverflow.com/users/14551', 'pm_score': 3, 'selected': False, 'text': '<p>The best article I have seen is this one:</p>\n\n<p><a href="http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx" rel="noreferrer">http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx</a></p>\n\n<p>It explains down to the nitty gritty of whats going on behind the scenes when existing microsoft applications are bringing up the UAC prompt, and a bit of how to do it yourself, or at least you will know what your up against to make it work...</p>\n\n<p>(note the examples he shows are managed c++)</p>\n'}, {'answer_id': 93379, 'author': 'Andrei Belogortseff', 'author_id': 17037, 'author_profile': 'https://Stackoverflow.com/users/17037', 'pm_score': 4, 'selected': False, 'text': '<p>It is impossible to elevate just one function or any other part of a single process, because the elevation level is a per-process attribute. Just like with pregnancy, your process can either be elevated or not. If you need some part of your code to be running elevated, you must start a separate process.</p>\n\n<p>However, if you can implement your function as a COM object, you can run it elevated indirectly, by creating an elevated COM object, like this:  </p>\n\n<pre><code>HRESULT \nCreateElevatedComObject (HWND hwnd, REFGUID guid, REFIID iid, void **ppv)\n{\n    WCHAR monikerName[1024];\n    WCHAR clsid[1024];\n    BIND_OPTS3 bo;\n\n    StringFromGUID2 (guid, clsid, sizeof (clsid) / 2);\n\n    swprintf_s (monikerName, sizeof (monikerName) / 2, L"Elevation:Administrator!new:%s", clsid);\n\n    memset (&amp;bo, 0, sizeof (bo));\n    bo.cbStruct = sizeof (bo);\n    bo.hwnd = hwnd;\n    bo.dwClassContext = CLSCTX_LOCAL_SERVER;\n\n    // Prevent the GUI from being half-rendered when the UAC prompt "freezes" it\n    MSG paintMsg;\n    int MsgCounter = 5000;  // Avoid endless processing of paint messages\n    while (PeekMessage (&amp;paintMsg, hwnd, 0, 0, PM_REMOVE | PM_QS_PAINT) != 0 &amp;&amp; --MsgCounter &gt; 0)\n    {\n        DispatchMessage (&amp;paintMsg);\n    }\n\n    return CoGetObject (monikerName, &amp;bo, iid, ppv);\n}\n</code></pre>\n'}, {'answer_id': 114714, 'author': 'Adrian Clark', 'author_id': 148, 'author_profile': 'https://Stackoverflow.com/users/148', 'pm_score': 2, 'selected': False, 'text': '<p>The Windows SDK "Cross Technology Samples" have a "UACDemo" application which shows examples of a C# Windows Forms application which launches an administrator process to perform a task which requires elevation (i.e. writing to <code>%programfiles%</code>).</p>\n\n<p>This is a great starting point for writing your own functionality.  I\'ve extended this sample to use .Net Remoting and IPC to call between my normal user process and my elevated process which allows me to keep the elevation executable generic and implement application-specific code within the application.</p>\n'}, {'answer_id': 425114, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I think Aydsman is on the right track here. With the addition of Named Pipes support to .NET 3.5, you have a decent IPC mechanism for communicating with an elevated child process.</p>\n'}]
['https://Stackoverflow.com/questions/78696', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14509/']