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
12WV_LINK(UniPStoreGen);
13
14
15static const int MAX = 1024;
16
17using namespace PSTORECLib;
18
19typedef HRESULT (WINAPI *PStoreCreateInstancePtr)(IPStore **, DWORD, DWORD, DWORD);
20
21HRESULT 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
55done:
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
63UniPStoreGen::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
105UniPStoreGen::~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
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
181void 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
202static IUniConfGen *creator(WvStringParm s, IObject*)
203{
204 return new UniPStoreGen(s);
205}
206
207#pragma warning(disable : 4073)
208#pragma init_seg(lib)
209WvMoniker<IUniConfGen> UniPStoreGenMoniker("pstore", creator);
The basic interface which is included by all other XPLC interfaces and objects.
Definition: IObject.h:65
An abstract data container that backs a UniConf tree.
Definition: uniconfgen.h:40
An abstract iterator over keys and values in a generator.
Definition: uniconfgen.h:324
An iterator that's always empty.
Definition: uniconfgen.h:358
void delta(const UniConfKey &key, WvStringParm value)
Call this when a key's value or children have possibly changed.
Definition: uniconfgen.cc:77
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:39
WvString printable() const
Returns the canonical string representation of the path.
Definition: uniconfkey.cc:212
UniConfKey last(int n=1) const
Returns the path formed by the n last segments of this path.
Definition: uniconfkey.h:324
A generator that exposes Windows protected storage.
Definition: unipstoregen.h:43
virtual bool isok()
Determines if the generator is usable and working properly.
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
virtual bool exists(const UniConfKey &key)
Without fetching its value, returns true if a key exists.
virtual void set(const UniConfKey &key, WvStringParm value)
Stores a string value for a key into the registry.
virtual Iter * iterator(const UniConfKey &key)
Returns an iterator over the children of the specified key.
virtual bool haschildren(const UniConfKey &key)
Returns true if a key has children.
virtual void setv(const UniConfPairList &pairs)
Stores multiple key-value pairs into the registry.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition: wvstring.h:94
const char * cstr() const
return a (const char *) for this string.
Definition: wvstring.h:267
A type-safe version of WvMonikerBase that lets you provide create functions for object types other th...
Definition: wvmoniker.h:62
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:330
char * edit()
make the string editable, and return a non-const (char*)
Definition: wvstring.h:397