WvStreams
unipstoregen.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2003 Net Integration Technologies, Inc.
4  *
5  * A generator that exposes Windows protected storage.
6  */
7 #include "unipstoregen.h"
8 #include "wvmoniker.h"
9 #include "wvlinkerhack.h"
10 #include <string>
11 
12 WV_LINK(UniPStoreGen);
13 
14 
15 static const int MAX = 1024;
16 
17 using namespace PSTORECLib;
18 
19 typedef HRESULT (WINAPI *PStoreCreateInstancePtr)(IPStore **, DWORD, DWORD, DWORD);
20 
21 HRESULT UniPStoreGen::create_types(WvString type_name, WvString subtype_name)
22 {
23  HRESULT hRes;
24 
25  _PST_TYPEINFO myTypeInfo;
26  myTypeInfo.cbSize = strlen(type_name.cstr()) + 1;
27  myTypeInfo.szDisplayName = new wchar_t[myTypeInfo.cbSize];
28  mbstowcs(myTypeInfo.szDisplayName, type_name.cstr(), myTypeInfo.cbSize);
29 
30  _PST_TYPEINFO mySubTypeInfo;
31  mySubTypeInfo.cbSize = strlen(subtype_name.cstr()) + 1;
32  mySubTypeInfo.szDisplayName = new wchar_t[mySubTypeInfo.cbSize];
33  mbstowcs(mySubTypeInfo.szDisplayName, subtype_name.cstr(), mySubTypeInfo.cbSize);
34 
35  _PST_ACCESSRULESET myRuleSet;
36  myRuleSet.cbSize = sizeof(myRuleSet);
37  myRuleSet.cRules = 0;
38  myRuleSet.rgRules = 0;
39 
40  hRes = m_spPStore->CreateType( m_key, &m_type, &myTypeInfo, 0);
41 
42  if ((hRes != PST_E_OK) && (hRes != PST_E_TYPE_EXISTS))
43  {
44  m_log("CreateSubtype() returned: %s\n", hRes);
45  goto done;
46  }
47 
48  hRes = m_spPStore->CreateSubtype( m_key, &m_type, &m_subtype, &mySubTypeInfo, &myRuleSet, 0);
49  if ((hRes != PST_E_OK) && (hRes != PST_E_TYPE_EXISTS))
50  {
51  m_log("CreateSubtype() returned: %s\n", hRes);
52  goto done;
53  }
54 
55 done:
56  delete[] myTypeInfo.szDisplayName;
57  delete[] mySubTypeInfo.szDisplayName;
58  return hRes;
59 }
60 
61 // moniker is
62 // PST_KEY_CURRENT_USER:TYPENAME:TYPEGUID:SUBTYPE:SUBTYPEGUID
63 UniPStoreGen::UniPStoreGen(WvString _moniker) :
64  m_log(_moniker), m_key(-1)
65 {
66  // load the library and get an entry point function pointer
67  m_hPstoreDLL = LoadLibrary("pstorec.dll");
68  assert(m_hPstoreDLL);
69 
70  PStoreCreateInstancePtr pPStoreCreateInstance =
71  (PStoreCreateInstancePtr) GetProcAddress(m_hPstoreDLL, "PStoreCreateInstance");
72  assert(pPStoreCreateInstance);
73 
74  HRESULT hr = pPStoreCreateInstance(&m_spPStore, 0, 0, 0);
75  assert(SUCCEEDED(hr));
76 
77  // parse the moniker
78  char *moniker = _moniker.edit();
79  const char *seps = ":";
80  WvString _key = strtok(moniker, seps);
81  WvString type_name = strtok(NULL, seps);
82  WvString _type_guid = strtok(NULL, seps);
83  WvString subtype_name = strtok(NULL, seps);
84  WvString _subtype_guid = strtok(NULL, seps);
85 
86  if (!!_key && strcmp(_key, "PST_KEY_CURRENT_USER") == 0)
87  {
88  m_key = PST_KEY_CURRENT_USER;
89  }
90  else if (!!_key && strcmp(_key, "PST_KEY_LOCAL_MACHINE") == 0)
91  {
92  m_key = PST_KEY_LOCAL_MACHINE;
93  }
94 
95  if ((m_key >= 0) && !!type_name && !!_type_guid && !!subtype_name && !!_subtype_guid)
96  {
97  HRESULT hr;
98  hr = UuidFromString((unsigned char*)_type_guid.edit(), &m_type);
99  hr = UuidFromString((unsigned char*)_subtype_guid.edit(), &m_subtype);
100  int result = create_types(type_name, subtype_name);
101  assert(SUCCEEDED( result ) || (result == PST_E_TYPE_EXISTS));
102  }
103 }
104 
105 UniPStoreGen::~UniPStoreGen()
106 {
107  m_spPStore = 0;
108  if (m_hPstoreDLL)
109  {
110  FreeLibrary(m_hPstoreDLL);
111  m_hPstoreDLL = 0;
112  }
113 }
114 
116 {
117  return m_key >= 0;
118 }
119 
120 
122 {
123  HRESULT hRes;
124  WvString value = WvString::null;
125 
126  unsigned char *data;
127  unsigned long cbdata;
128 
129  WvString _name = key.last().printable();
130  WCHAR name[MAX];
131  mbstowcs(name, _name.cstr(), MAX);
132 
133  hRes = m_spPStore->ReadItem(
134  m_key,
135  &m_type,
136  &m_subtype,
137  name,
138  &cbdata,
139  &data,
140  NULL,
141  0
142  );
143 
144  if (hRes == PST_E_OK)
145  {
146  value.setsize(MAX);
147  wcstombs(value.edit(), (wchar_t*)data, MAX);
148  CoTaskMemFree(data);
149  }
150 
151  return value;
152 }
153 
154 void UniPStoreGen::set(const UniConfKey &key, WvStringParm value)
155 {
156  WCHAR name[MAX], data[MAX];
157  mbstowcs(name, key.last().printable().cstr(), MAX);
158  mbstowcs(data, value.cstr(), MAX);
159 
160  DWORD cbdata = DWORD((wcslen(data) + 1) * sizeof(WCHAR));
161 
162  HRESULT hRes = m_spPStore->WriteItem(
163  m_key,
164  &m_type,
165  &m_subtype,
166  name,
167  cbdata,
168  (unsigned char *)data,
169  NULL,
170  PST_CF_NONE,
171  0
172  );
173 
174  if (hRes == PST_E_OK)
175  {
176  delta(key, value);
177  }
178 }
179 
180 
181 void UniPStoreGen::setv(const UniConfPairList &pairs)
182 {
183  setv_naive(pairs);
184 }
185 
186 
188 {
189  return false;
190 }
191 
193 {
194  return false;
195 }
196 
198 {
199  return new NullIter();
200 }
201 
202 static IUniConfGen *creator(WvStringParm s, IObject*)
203 {
204  return new UniPStoreGen(s);
205 }
206 
207 #pragma warning(disable : 4073)
208 #pragma init_seg(lib)
209 WvMoniker<IUniConfGen> UniPStoreGenMoniker("pstore", creator);
WvString::edit
char * edit()
make the string editable, and return a non-const (char*)
Definition: wvstring.h:397
UniPStoreGen::haschildren
virtual bool haschildren(const UniConfKey &key)
Returns true if a key has children.
Definition: unipstoregen.cc:192
UniConfKey::printable
WvString printable() const
Returns the canonical string representation of the path.
Definition: uniconfkey.cc:212
UniPStoreGen::setv
virtual void setv(const UniConfPairList &pairs)
Stores multiple key-value pairs into the registry.
Definition: unipstoregen.cc:181
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
IUniConfGen
An abstract data container that backs a UniConf tree.
Definition: uniconfgen.h:39
UniPStoreGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unipstoregen.cc:121
WvFastString::cstr
const char * cstr() const
return a (const char *) for this string.
Definition: wvstring.h:267
UniPStoreGen::set
virtual void set(const UniConfKey &key, WvStringParm value)
Stores a string value for a key into the registry.
Definition: unipstoregen.cc:154
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
WvMoniker
A type-safe version of WvMonikerBase that lets you provide create functions for object types other th...
Definition: wvmoniker.h:61
UniConfGen::delta
void delta(const UniConfKey &key, WvStringParm value)
Call this when a key's value or children have possibly changed.
Definition: uniconfgen.cc:77
IObject
Definition: IObject.h:65
UniPStoreGen::iterator
virtual Iter * iterator(const UniConfKey &key)
Returns an iterator over the children of the specified key.
Definition: unipstoregen.cc:197
UniPStoreGen::exists
virtual bool exists(const UniConfKey &key)
Without fetching its value, returns true if a key exists.
Definition: unipstoregen.cc:187
UniConfGen::NullIter
An iterator that's always empty.
Definition: uniconfgen.h:357
UniPStoreGen
A generator that exposes Windows protected storage.
Definition: unipstoregen.h:42
UniConfKey::last
UniConfKey last(int n=1) const
Returns the path formed by the n last segments of this path.
Definition: uniconfkey.h:324
UniPStoreGen::isok
virtual bool isok()
Determines if the generator is usable and working properly.
Definition: unipstoregen.cc:115
UniConfGen::Iter
An abstract iterator over keys and values in a generator.
Definition: uniconfgen.h:323