[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Mark Rockman

2/28/2007 2:29:00 AM

So, I need to call AdjustTokenPrivileges because I'm writing a file backup
application. This is Win32 code that interfaces with a COM component,
complete with HRESULTs and all that. The DLL that I make requires that I
name Advapi32.lib as a linker input dependency.

Uaing P/Invoke, I call into the DLL from C#. This works great on the
development machine.

The problem is that when I publish the C# application (using the Publish
menu item of Visual Studio), news about the DLL doesn't get put into the
manifest. Apparently, this has something to do with with the Advapi32.lib
dependency and something missing from the target machine's WinSxS folder.
The application fails on the target machine with a miserable, uninformative
message, as follows:

Unable to load DLL <name-of-DLL>; This application has failed to start
because the application configuration is incorrect. Reinstalling the
application may fix this problem. (Exception from HRESULT: 0x800736B1)

This is hair pulling time.

Here is the DLL that fails to load:

/*

Required to perform backup operations.

This privilege causes the system to grant all read access control to any
file,

regardless of the access control list (ACL) specified for the file.

Any access request other than read is still evaluated with the ACL.
This privilege is required by the RegSaveKey and RegSaveKeyExfunctions.

The following access rights are granted if this privilege is held:

READ_CONTROL

ACCESS_SYSTEM_SECURITY

FILE_GENERIC_READ

FILE_TRAVERSE

User Right: Back up files and directories.

*/

// This is the main DLL file.









#include <windows.h>

#include <stdio.h>

// The following example can be used to enable or disable the

// backup privilege. By making the indicated substitutions, you can

// also use this example to enable or disable the restore privilege

// Use the following statement to enable the privilege:

// hr = ModifyPrivilege(SE_BACKUP_NAME, TRUE);

// Use the following statement to disable the privilege:

// hr = ModifyPrivilege(SE_BACKUP_NAME, FALSE);

// Use SE_RESTORE_NAME for the restore privilege.

// The main function in this example enables the backup privilege.







HRESULT _stdcall ModifyPrivilege(

IN LPCTSTR szPrivilege,

IN BOOL fEnable)

{

HRESULT hr = S_OK;

TOKEN_PRIVILEGES NewState;

LUID luid;

HANDLE hToken = NULL;

// Open the process token for this process.

if (!OpenProcessToken(GetCurrentProcess(),

TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,

&hToken ))

{

printf("Failed OpenProcessToken\n");

return ERROR_FUNCTION_FAILED;

}

// Get the local unique ID for the privilege.

if ( !LookupPrivilegeValue( NULL,

szPrivilege,

&luid ))

{

CloseHandle( hToken );

printf("Failed LookupPrivilegeValue\n");

return ERROR_FUNCTION_FAILED;

}

// Assign values to the TOKEN_PRIVILEGE structure.

NewState.PrivilegeCount = 1;

NewState.Privileges[0].Luid = luid;

NewState.Privileges[0].Attributes =

(fEnable ? SE_PRIVILEGE_ENABLED : 0);

// Adjust the token privilege.

if (!AdjustTokenPrivileges(hToken,

FALSE,

&NewState,

0,

NULL,

NULL))

{

printf("Failed AdjustTokenPrivileges\n");

hr = ERROR_FUNCTION_FAILED;

}

// Close the handle.

CloseHandle(hToken);

return hr;

}

extern "C" _declspec(dllexport) int _stdcall
GetBackupPrivilForSystemBackup(int togglePosition)

{

HRESULT hr;

BOOL arg;

if (togglePosition!=0)

arg = TRUE;

else

arg = FALSE;

hr = ModifyPrivilege(SE_BACKUP_NAME, arg);

if (!SUCCEEDED(hr))

return 0;

else

return 1;

}