pinvoke - AdjustTokenPrivilege c# 0x80070057 -
i trying write uefi properties. appears need raise token privileges. have used http://www.pinvoke.net/default.aspx/advapi32.adjusttokenprivileges guide this.
i keep getting error e_invalid arguments
public static void enabledisableprivilege(string privilegename, bool enabledisable) { //gets process token handle using pinvoke var intokenhandle = intptr.zero; if (!openprocesstoken(process.getcurrentprocess().handle, tokenaccesslevels.adjustprivileges | tokenaccesslevels.query, out intokenhandle)) { marshal.throwexceptionforhr(marshal.gethrforlastwin32error()); return; } //enumerates current privileges of token handle var innewstate = new token_privileges { privilegecount = 1, privileges = new luid_and_attributes[1] }; //attempts lookup inputed privilege luid luid; if (!lookupprivilegevalue(null, privilegename, out luid)) { marshal.throwexceptionforhr(marshal.gethrforlastwin32error()); return; } //adds new privilige token whilst retaining other priviliges innewstate.privileges[0].luid = luid; innewstate.privileges[0].attributes = (uint)(enabledisable ? 2 : 0); //attempts set priviliges token handle. if (!adjusttokenprivileges(intokenhandle, false, ref innewstate, 256, intptr.zero, intptr.zero)) { marshal.throwexceptionforhr(marshal.gethrforlastwin32error()); return; } } /// <summary> /// luid 64-bit value guaranteed unique on system on generated /// </summary> [structlayout(layoutkind.sequential)] public struct luid { /// <summary> /// low order bits /// </summary> public uint lowpart { get; set; } /// <summary> /// high order bits /// </summary> public int highpart { get; set; } } /// <summary> /// luid_and_attributes structure represents locally unique identifier (luid) , attributes. /// </summary> [structlayout(layoutkind.sequential)] public struct luid_and_attributes { /// <summary> /// specifies high order bits , low order bits stucture /// </summary> public luid luid { get; set; } /// <summary> /// specifies attributes of luid. value contains 32 one-bit flags. /// </summary> public uint attributes { get; set; } } /// <summary> /// token_privileges structure contains information set of privileges access token. /// </summary> [structlayout(layoutkind.sequential)] public struct token_privileges { /// <summary> /// must set number of entries in privileges array. /// </summary> public uint privilegecount { get; set; } /// <summary> /// specifies array of luid_and_attributes structures. /// </summary> public luid_and_attributes[] privileges { get; set; } } [dllimport("advapi32", setlasterror = true)] [return: marshalas(unmanagedtype.bool)] public static extern bool openprocesstoken( intptr processhandle, tokenaccesslevels desiredaccess, out intptr tokenhandle); [dllimport("advapi32.dll", setlasterror = true)] [return: marshalas(unmanagedtype.bool)] public static extern bool adjusttokenprivileges( intptr tokenhandle, [marshalas(unmanagedtype.bool)]bool disableallprivileges, ref token_privileges newstate, uint bufferlengthinbytes, intptr previousstate, intptr returnlength); [dllimport("advapi32.dll", setlasterror = true)] [return: marshalas(unmanagedtype.bool)] public static extern bool lookupprivilegevalue( string lpsystemname, string lpname, out luid lpluid);
calling code: //enable privilege implementing following line in code: privileges.enabledisableprivilege("sesystemenvironmentprivilege", true);
im not sure have gone wrong here.
Comments
Post a Comment