summaryrefslogtreecommitdiff
path: root/examples/Registry/test_registry_update.cpp
blob: 5ea097cdbe9a54fb790f0a702de61ecc64d7c6cc (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Suppose this application belonged to AcmeSoft. AcmeSoft wants to
// keep track of the number of times this application is
// executed. They want two counters: one counts the number of
// executions per machine, the other keeps track of the number of
// executions per user.
//
// This application uses the ACE_Registry class to create and update
// entries in the LOCAL_MACHINE and CURRENT_USER predefined registries
// to store the counters.
// 
// Note that this application will not work with remote registries
// if used with the CURRENT_USER predefined registry.

#include "ace/Registry.h"


// Name for application's naming context
static ACE_Registry::Name application_context_name;

// Name for instance counter.
static ACE_Registry::Name counter_name;

// Protypes
static int update_counter (HKEY predefined,
			   u_long &current_counter);
static void setup_names ();

int 
main(int argc, char *argv[])
{
  int result;
  u_long current_counter = 0;

  // initialize name
  setup_names ();

  // Update counter per user
  result = ::update_counter (HKEY_CURRENT_USER,
			     current_counter);
  if (result == 0)
    {
      cout << "User counter: " << current_counter << endl;

      // Update counter per machine
      result = ::update_counter (HKEY_LOCAL_MACHINE,
				 current_counter);
      if (result == 0)
	cout << "Machine counter: " << current_counter << endl;
    }
      
  if (result != 0)
    ACE_DEBUG ((LM_DEBUG, "test failed\n"));
  else
    ACE_DEBUG ((LM_DEBUG, "test succeeded\n"));

  return 0;
}  

static int 
update_counter (HKEY predefined,
		u_long &current_counter)
{
  int result;
  ACE_Registry::Naming_Context parent_context;
  ACE_Registry::Naming_Context application_context;
  
  // Connect to predefined entry
  result = ACE_Predefined_Naming_Contexts::connect (parent_context,
						    predefined);
  if (result != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Predefined_Naming_Contexts::connect failed"), -1);
  
  // Find application context name
  result = parent_context.resolve_context (application_context_name, 
					   application_context);
  
  if (result != 0)
    // Failed to find: create a new context
    result = parent_context.bind_new_context (application_context_name,
					      application_context);
  
  if (result != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Contexts::bind/resolve_context failed"), -1);
  
  // Counter
  u_long counter = 0;
  // Represent counter as an ACE_Registry::Object
  ACE_Registry::Object object ((void *) &counter,
			       sizeof counter,
			       REG_DWORD);
  // Find counter
  result = application_context.resolve (counter_name, 
					object);
  
  if (result != 0)
    // Failed to find: create new binding for object
    {
      counter = 1;
      result = application_context.bind (counter_name,
					 object);
    }
  else
    // Counter was found
    {
      // increment counter
      counter++;
      // Update
      result = application_context.rebind (counter_name,
					   object);      
    }

  if (result != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Contexts::bind/resolve failed"), -1);  
  else
    current_counter = counter;      

  return 0;
}


static void
setup_names ()
{
  // Stupid implementation of STL is broken
  /*
  ::application_context_name.reserve (4);
  ::application_context_name [0].id_ = __TEXT ("Software");
  ::application_context_name [1].id_ = __TEXT ("AcmeSoft");
  ::application_context_name [2].id_ = __TEXT ("AcmeApplication");
  ::application_context_name [3].id_ = __TEXT ("1.0");
  
  ::counter_name.reserve (1);
  ::counter_name [0].id_ = __TEXT ("Instance Counter");
  */

  ACE_Registry::Name_Component component;

  component.id_ = __TEXT ("Software"),         ::application_context_name.push_back (component);
  component.id_ = __TEXT ("AcmeSoft"),         ::application_context_name.push_back (component);
  component.id_ = __TEXT ("AcmeApplication"),  ::application_context_name.push_back (component);
  component.id_ = __TEXT ("1.0"),              ::application_context_name.push_back (component);
  
  component.id_ = __TEXT ("Instance Counter"), ::counter_name.push_back (component);
}