Toon posts:

[C++][WinXP] - WMI en ontvangen van registry events

Pagina: 1
Acties:

Verwijderd

Topicstarter
Oke het zit alsvolgt. Ik zou graag een applicatie in C++ maken die zodra er iets verandert in het register een event binnen krijgt met wat er dan verandert is. Ik heb hier heel lang naar gezocht op google en de msdn. Er staan overal voorbeeld van van VBScript etc.

Nu heb ik in de msdn al een voorbeeld gevonden in C++ hoe je moet werken met WMI. Dit compiled echter niet. Maar ik heb hier wel al een aantal dingen uitgehaald om COM en WMI te initializeren:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#define _WIN32_DCOM

#include <iostream>

using namespace std;

#include <windows.h>
#include <comdef.h>
#include <wbemcli.h>

int main(int argc, char **argv)
{
    HRESULT hres;

    // Initialize COM.
    hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if(FAILED(hres))
    {
        cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;
        return 1;                  // Program has failed.
    }

    // Initialize
    hres =  CoInitializeSecurity(
        NULL,
        -1,                          // COM negotiates authentication service
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication (recommended)
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  (recommended)
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities
        NULL                         // Reserved
        );


    if(FAILED(hres))
    {
        cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl;
        CoUninitialize();
        return 1;                      // Program has failed.
    }

    // Obtain the initial locator to Windows Management on a particular host computer.
    IWbemLocator *pLoc = 0;

    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0,
        CLSCTX_INPROC_SERVER,
        IID_IWbemLocator, (LPVOID *) &pLoc);

    if(FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object. Err code = 0x" << hex << hres << endl;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    IWbemServices *pSvc = 0;

    // Connect to the rootcimv2 namespace with the current user and obtain pointer pSvc
    //     to make IWbemServices calls.

    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\CIMV2"), // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current user's password
         0,                       // Locale. NULL indicates current locale             
         NULL,                    // Security flags.
         0,                       // Authority (e.g. Kerberos)
         0,                       // Context object (IWbemContext), if required
         &pSvc                    // Out: pointer to IWbemServices proxy
         );                              

    if(FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x"
             << hex << hres << endl;
        pLoc->Release();     
        CoUninitialize();
        return 1;                // Program has failed.
    }

    cout << "Connected to WMI" << endl;

    // Set the IWbemServices proxy so that impersonation of the user (client) occurs.
    hres = CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx authentication service
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx authorization service
       NULL,                        // Server principal name
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx authentication level
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx impersonation level
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities
    );

    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }


En het volgende om het af te sluiten:

code:
1
2
3
4
5
6
7
8
// Cleanup
    // ========

    pSvc->Release();
    pLoc->Release();
    CoUninitialize();

    return 0;   // Program successfully completed.



Nou is het de bedoeling dat er tussen die 2 stukken code iets komt te staan om mijn applicatie te registeren voor een WMI registry event en vervolgens registry events te ontvangen. En hier kom ik niet uit.

Ik heb wel al een query voorbeeld voor een verandering in de tree van local_machine.

SELECT * FROM RegistryTreeChangeEvent WHERE Hive = HKEY_LOCAL_MACHINE AND Rootpath = Software

Nou weet ik dat mensen mij graag naar de msdn willen verwijzen want daar staat alles in. Echter er staan geen concrete werkende voorbeelden in. En momenteel kom ik er zonder die voorbeelden niet echt uit.

Dus bij deze zou ik het op prijs stellen als iemand mij kan helpen met de code of als iemand mij kan verwijzen naar een pagina waar een werkend voorbeeld staat van een applicatie die registry events ontvangt in C++ (Borland of Visual).

Alvast bedankt!

ps: sorry voor de wat lange post.

Verwijderd

Topicstarter
Niemand die hier iets van weet?