Menu

[r4761]: / branches / parsnip / Src / Main / CS.Utils.COM.pas  Maximize  Restore  History

Download this file

180 lines (157 with data), 4.5 kB

  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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Various utility routines and classes for working with COM.
}
unit CS.Utils.COM;
interface
uses
// Delphi
Classes,
ActiveX,
// Project
IntfCommon,
UIStringList;
type
/// <summary>Implementation of the IEnumString interface for TStrings objects
/// for use with COM.</summary>
/// <remarks>
/// <para>Uses the shell's task allocator to create the strings passed out
/// from the IEnumString.Next method. Callers must use the task allocator to
/// free these strings.</para>
/// <para>Based on code extracted from the uAutoComplete unit at
/// http://users.atw.hu/delphicikk/listaz.php?id=171</para>
/// </remarks>
TCOMEnumString = class(TInterfacedObject, IEnumString)
strict private
var
fStrings: TStringList;
fCurrentIdx: Integer;
public
constructor Create(AStrings: TStrings);
destructor Destroy; override;
/// <remarks>Method of IStringEnum.</remarks>
function Next(celt: Longint; out elt; pceltFetched: PLongint): HResult;
stdcall;
/// <remarks>Method of IStringEnum.</remarks>
function Skip(celt: Longint): HResult; stdcall;
/// <remarks>Method of IStringEnum.</remarks>
function Reset: HResult; stdcall;
/// <remarks>Method of IStringEnum.</remarks>
function Clone(out enm: IEnumString): HResult; stdcall;
end;
function TaskAllocWideString(const S: string): PWideChar;
{Allocates memory for a wide string using the Shell's task allocator and
copies a given string into the memory as a wide string. Caller is responsible
for freeing the buffer and must use the shell's allocator to do this.
@param S [in] String to convert.
@return Pointer to buffer containing wide string.
}
implementation
uses
// Delphi
SysUtils,
ComObj,
Windows;
function TaskAllocWideString(const S: string): PWideChar;
{Allocates memory for a wide string using the Shell's task allocator and
copies a given string into the memory as a wide string. Caller is responsible
for freeing the buffer and must use the shell's allocator to do this.
@param S [in] String to convert.
@return Pointer to buffer containing wide string.
}
var
StrLen: Integer; // length of string in bytes
begin
// Store length of string, allowing for terminal #0
StrLen := Length(S) + 1;
// Allocate buffer for wide string using task allocator
Result := CoTaskMemAlloc(StrLen * SizeOf(WideChar));
if not Assigned(Result) then
raise EOutOfMemory.Create('TaskAllocWideString: can''t allocate buffer.');
// Convert string to wide string and store in buffer
StringToWideChar(S, Result, StrLen);
end;
{ TCOMEnumString }
function TCOMEnumString.Clone(out enm: IEnumString): HResult;
begin
Result := E_NOTIMPL;
pointer(enm) := nil;
exit;
try
enm := TCOMEnumString.Create(fStrings);
Result := S_OK;
except
on E: Exception do
begin
Pointer(enm) := nil;
if E is EOutOfMemory then
Result := E_OUTOFMEMORY
else
Result := E_UNEXPECTED;
end;
end;
end;
constructor TCOMEnumString.Create(AStrings: TStrings);
begin
inherited Create;
fCurrentIdx := 0;
fStrings := TStringList.Create;
fStrings.Assign(AStrings);
end;
destructor TCOMEnumString.Destroy;
begin
fStrings.Free;
inherited;
end;
function TCOMEnumString.Next(celt: Integer; out elt; pceltFetched: PLongint):
HResult;
var
EltFetched: Integer;
EltStr: string;
begin
EltFetched := 0;
while (EltFetched < celt) and (fCurrentIdx < fStrings.Count) do
begin
EltStr := fStrings[fCurrentIdx];
TPointerList(elt)[EltFetched] := TaskAllocWideString(EltStr);
Inc(EltFetched);
Inc(fCurrentIdx);
end;
if pceltFetched <> nil then
pceltFetched^ := EltFetched;
if EltFetched = celt then
Result := S_OK
else
Result := S_FALSE;
end;
function TCOMEnumString.Reset: HResult;
begin
fCurrentIdx := 0;
Result := S_OK;
end;
function TCOMEnumString.Skip(celt: Integer): HResult;
begin
if (fCurrentIdx + celt) <= fStrings.Count then
begin
Inc(fCurrentIdx, celt);
Result := S_OK;
end
else
begin
fCurrentIdx := fStrings.Count;
Result := S_FALSE;
end;
end;
initialization
OleInitialize(nil);
finalization
OleUninitialize;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.