[WinPrnDev] Using AddMonitor function on Windows 7

  • From: Aeliton Germano <aeliton@xxxxxxxxx>
  • To: winprndev@xxxxxxxxxxxxx
  • Date: Mon, 24 May 2010 22:01:59 -0300

 Hello,

I'm trying to install a port monitor from ddk code samples using
AddMonitor function, but I'm getting error. GetLastError returns 5
(ERROR_ACCESS_DENIED).
The msdn docs says that we have to get SeLoadDriverPrivilege, so I'm
using AdjustTokenPrivileges function to set it, but it doesn't work
either.

My function follows. It prints: "Monitor Install Failed with error 5"

void InstallMon()
{
    MONITOR_INFO_2 newMonitor = {L"Custom Monitor", NULL, L"ddklocalmon.dll"};
    HANDLE procToken;
    LUID luid;
    TOKEN_PRIVILEGES tp;

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY, &procToken))
    {
        cout << "Could not OpenProcessToken :"<< GetLastError() << endl;
        return;
    }

    if (!LookupPrivilegeValue(NULL, SE_LOAD_DRIVER_NAME, &luid))
    {
        cout << "Could not LookupPrivilegeValue :"<< GetLastError() << endl;
        return;
    }


    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    if (!AdjustTokenPrivileges(procToken, FALSE, &tp,
sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES) NULL, (PDWORD)NULL))
    {
        cout << "Could not AdjustTokenPrivileges :"<< GetLastError() << endl;
        return;
    }

    if (AddMonitor(NULL, 2, (BYTE*)&newMonitor))
    {
        cout << "Monitor Installed successfully" << endl;
    } else {
        cout << "Monitor Install Failed with error" << GetLastError() << endl;
    }
}

I appreciate any help.

Best Regards,

--
Aeliton G. da Silva
---
Questions? First check the UndocPrint pages at 
http://www.undocprint.org/winspool

To unsubscribe, visit the List Server page at 
//www.freelists.org/list/winprndev

Other related posts:

  • » [WinPrnDev] Using AddMonitor function on Windows 7 - Aeliton Germano