Autor Beitrag
lucky41
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Sa 20.02.10 16:52 
Hallo zusammen,

ich habe folgenden Delphi Code gefunden.
und würde diesen gern in CSharp verwenden.
Kann mir einer den übersetzen oder ein Beispiel aus cSharp nennen.
Mi DSOFile habe ich es versucht ohne Erfolg.
OS = Windows 7. Es sind nur Office Files änderbar.

Ich danke Euch.
Gruß Lucky


ausblenden volle Höhe Delphi-Quelltext
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:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
unit storage;

interface

uses
  Windows, ComObj, ActiveX;

const
  FMTID_SummaryInformation          : TGUID =
    '{F29F85E0-4FF9-1068-AB91-08002B27B3D9}';
  FMTID_DocSummaryInformation       : TGUID =
    '{D5CDD502-2E9C-101B-9397-08002B2CF9AE}';
  FMTID_UserDefinedProperties       : TGUID =
    '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}';
  FMTID_AudioSummaryInformation     : TGuid =
    '{64440490-4C8B-11D1-8B70-080036B11A03}';
  FMTID_VideoSummaryInformation     : TGUID =
    '{64440491-4C8B-11D1-8B70-080036B11A03}';
  FMTID_ImageSummaryInformation     : TGUID =
    '{6444048f-4c8b-11d1-8b70-080036b11a03}';
  FMTID_MediaFileSummaryInformation : TGUID =
    '{64440492-4c8b-11d1-8b70-080036b11a03}';


  STGFMT_STORAGE     = 0;
  STGFMT_FILE        = 3;
  STGFMT_ANY         = 4;
  STGFMT_DOCFILE     = 5;

  // Summary Information
  PIDSI_TITLE        = $00000002;     // VT_LPSTR
  PIDSI_SUBJECT      = $00000003;
  PIDSI_AUTHOR       = $00000004;
  PIDSI_KEYWORDS     = $00000005;
  PIDSI_COMMENTS     = $00000006;
  PIDSI_TEMPLATE     = $00000007;
  PIDSI_LASTAUTHOR   = $00000008;
  PIDSI_REVNUMBER    = $00000009;
  PIDSI_EDITTIME     = $0000000A;     // VT_FILETIME (UTC)
  PIDSI_LASTPRINTED  = $0000000B;
  PIDSI_CREATE_DTM   = $0000000C;
  PIDSI_LASTSAVE_DTM = $0000000D;
  PIDSI_PAGECOUNT    = $0000000E;     // VT_I4
  PIDSI_WORDCOUNT    = $0000000F;
  PIDSI_CHARCOUNT    = $00000010;
  PIDSI_THUMBNAIL    = $00000011;     // VT_CF
  PIDSI_APPNAME      = $00000012;     // VT_LPSTR
  PIDSI_DOC_SECURITY = $00000013;     // VT_I4

  // Document Summary Information
  PIDDSI_CATEGORY    = $00000002;     // VT_LPSTR
  PIDDSI_PRESFORMAT  = $00000003;
  PIDDSI_BYTECOUNT   = $00000004;     // VT_I4
  PIDDSI_LINECOUNT   = $00000005;
  PIDDSI_PARCOUNT    = $00000006;
  PIDDSI_SLIDECOUNT  = $00000007;
  PIDDSI_NOTECOUNT   = $00000008;
  PIDDSI_HIDDENCOUNT = $00000009;
  PIDDSI_MMCLIPCOUNT = $0000000A;
  PIDDSI_SCALE       = $0000000B;     // VT_BOOL
  PIDDSI_HEADINGPAIR = $0000000C;     // VT_VARIANT | VT_VECTOR
  PIDDSI_DOCPARTS    = $0000000D;     // VT_LPSTR | VT_VECTOR
  PIDDSI_MANAGER     = $0000000E;     // VT_LPSTR
  PIDDSI_COMPANY     = $0000000F;
  PIDDSI_LINKSDIRTY  = $00000010;     // VT_BOOL

  // FMTID_MediaFileSummaryInfo
  PIDMSI_EDITOR      = $00000002;     // VT_LPWSTR
  PIDMSI_SUPPLIER    = $00000003;
  PIDMSI_SOURCE      = $00000004;
  PIDMSI_SEQUENCE_NO = $00000005;
  PIDMSI_PROJECT     = $00000006;
  PIDMSI_STATUS      = $00000007;     // VT_UI4
  PIDMSI_OWNER       = $00000008;     // VT_LPWSTR
  PIDMSI_RATING      = $00000009;
  PIDMSI_PRODUCTION  = $0000000A;     // VT_FILETIME (UTC)
  PIDMSI_COPYRIGHT   = $0000000B;     // VT_LPWSTR

  // FMTID_AudioSummaryInformation
  PIDASI_FORMAT        = $00000002;   // VT_BSTR
  PIDASI_TIMELENGTH    = $00000003;   // VT_UI4, milliseconds
  PIDASI_AVG_DATA_RATE = $00000004;   // VT_UI4,  Hz
  PIDASI_SAMPLE_RATE   = $00000005;   // VT_UI4,  bits
  PIDASI_SAMPLE_SIZE   = $00000006;   // VT_UI4,  bits
  PIDASI_CHANNEL_COUNT = $00000007;   // VT_UI4
  PIDASI_STREAM_NUMBER = $00000008;   // VT_UI2
  PIDASI_STREAM_NAME   = $00000009;   // VT_LPWSTR
  PIDASI_COMPRESSION   = $0000000A;   // VT_LPWSTR

  // FMTID_VideoSummaryInformation
  PIDVSI_STREAM_NAME   = $00000002;   // "StreamName", VT_LPWSTR
  PIDVSI_FRAME_WIDTH   = $00000003;   // "FrameWidth", VT_UI4
  PIDVSI_FRAME_HEIGHT  = $00000004;   // "FrameHeight", VT_UI4
  PIDVSI_TIMELENGTH    = $00000007;   // "TimeLength", VT_UI4, milliseconds
  PIDVSI_FRAME_COUNT   = $00000005;   // "FrameCount". VT_UI4
  PIDVSI_FRAME_RATE    = $00000006;   // "FrameRate", VT_UI4, frames/millisecond
  PIDVSI_DATA_RATE     = $00000008;   // "DataRate", VT_UI4, bytes/second
  PIDVSI_SAMPLE_SIZE   = $00000009;   // "SampleSize", VT_UI4
  PIDVSI_COMPRESSION   = $0000000A;   // "Compression", VT_LPWSTR
  PIDVSI_STREAM_NUMBER = $0000000B;   // "StreamNumber", VT_UI2

type
  TMultipleArray     = record
    pidInfoType : cardinal;
    pidInfoStr  : pchar;
  end;
  TMultipleArrayList = array of TMultipleArray;


function GetFileSummaryInfo(const FileName: WideString;
  GUID_SummaryType: TGUID; PID_InfoType: cardinal): string;
function SetFileSummaryInfo(const FileName: WideString; GUID_InfoType: TGUID;
  PID_InfoType: cardinal; InfoStr: string): boolean;
function SetMultipleFileSummaryInfo(const FileName: WideString;
  GUID_InfoType: TGUID; MultipleArrayList: TMultipleArrayList): boolean;



implementation


const
  szOleDll                = 'ole32.dll';
  IID_IPropertySetStorage : TGUID = '{0000013A-0000-0000-C000-000000000046}';
type
  TStgOpenStorageEx       = function(const pwcsName: PWideChar;
    grfMode: DWORD; stgfmt: DWORD; grfAttrs: DWORD; pStgOptions: pointer;
    reserved2: pointer; riid: PGUID; out ppObjectOpen: IStorage): HRESULT;
    stdcall;
var
  StgOpenStorageEx        : TStgOpenStorageEx = nil;
  dll                     : dword = 0;
  Win2k                   : boolean = false;


function GetFileSummaryInfo(const FileName: WideString;
  GUID_SummaryType: TGUID; PID_InfoType: cardinal): string;
var
  hr          : HRESULT;
  Stg         : IStorage;
  PropSetStg  : IPropertySetStorage;
  PropStg     : IPropertyStorage;
  PropSpec    : TPropSpec;
  PropVariant : TPropVariant;
begin
  Result      := '';
  hr          := S_FALSE;

  // for 9x and NT4 users
  if(not Win2k) or (@StgOpenStorageEx = nilthen
    hr := StgOpenStorage(pwidechar(FileName),nil,STGM_READ or
      STGM_SHARE_DENY_WRITE,nil,0,Stg)
  // for 2000, XP and higher
  else if(@StgOpenStorageEx <> nilthen
    hr := StgOpenStorageEx(pwidechar(FileName),STGM_READ or
      STGM_SHARE_DENY_WRITE,STGFMT_ANY,0,nil,nil,
      @IID_IPropertySetStorage,Stg);

  if(hr = S_OK) then
  begin
    PropSetStg := Stg as IPropertySetStorage;

    if(PropSetStg.Open(GUID_SummaryType,STGM_READ or
      STGM_SHARE_EXCLUSIVE,PropStg) = S_OK) then
    begin
      PropSpec.ulKind := PRSPEC_PROPID;
      PropSpec.propid := PID_InfoType;

      if(PropStg.ReadMultiple(1,@PropSpec,@PropVariant) = S_OK) and
        (PropVariant.vt = VT_LPSTR) and
        (PropVariant.pszVal <> nilthen
      Result := PropVariant.pszVal;
    end;
  end;
end;

function SetFileSummaryInfo(const FileName: WideString; GUID_InfoType: TGUID;
  PID_InfoType: cardinal; InfoStr: string): boolean;
var
  hr          : HRESULT;
  Stg         : IStorage;
  PropSetStg  : IPropertySetStorage;
  PropStg     : IPropertyStorage;
  PropSpec    : TPropSpec;
  PropVariant : TPropVariant;
begin
  Result      := false;
  hr          := S_FALSE;

  // 9x and NT4
  if(not Win2k) or (@StgOpenStorageEx = nilthen
    hr := StgOpenStorage(pwidechar(FileName),nil,STGM_SHARE_EXCLUSIVE or
      STGM_READWRITE,nil,0,Stg)
  // 2000, XP and higher
  else if(@StgOpenStorageEx <> nilthen
    hr := StgOpenStorageEx(pwidechar(FileName),STGM_SHARE_EXCLUSIVE or
    STGM_READWRITE,STGFMT_ANY,0,nil,nil,@IID_IPropertySetStorage,Stg);

  if(hr = S_OK) then
  begin
    PropSetStg := Stg as IPropertySetStorage;

    // try to open an existing Property set
    hr         := PropSetStg.Open(GUID_InfoType,STGM_READWRITE or
      STGM_SHARE_EXCLUSIVE,PropStg);

    // create a new set
    if(hr <> S_OK) then
      hr       := PropSetStg.Create(GUID_InfoType,GUID_InfoType,
        PROPSETFLAG_DEFAULT,STGM_CREATE or STGM_READWRITE or
        STGM_SHARE_EXCLUSIVE,PropStg);

    if(hr = S_OK) then
    begin
      PropSpec.ulKind    := PRSPEC_PROPID;
      PropSpec.propid    := PID_InfoType;
      PropVariant.vt     := VT_LPSTR;
      PropVariant.pszVal := pchar(InfoStr);

      if(PropStg.WriteMultiple(1,@PropSpec,@PropVariant,2) = S_OK) then
      begin
        Result := (PropStg.Commit(STGC_DEFAULT) = S_OK);
      end;
    end;
  end;
end;

function SetMultipleFileSummaryInfo(const FileName: WideString;
  GUID_InfoType: TGUID; MultipleArrayList: TMultipleArrayList): boolean;
var
  Stg         : IStorage;
  PropSetStg  : IPropertySetStorage;
  hr          : HRESULT;
  PropStg     : IPropertyStorage;
  PropSpec    : array of TPropSpec;
  PropVariant : array of TPropVariant;
  i           : integer;
begin
  Result      := false;
  hr          := S_FALSE;
  if(length(MultipleArrayList) = 0then exit;

  // 9x, NT4
  if(not Win2k) or (@StgOpenStorageEx = nilthen
    hr := StgOpenStorage(pwidechar(FileName),nil,STGM_SHARE_EXCLUSIVE or
      STGM_READWRITE,nil,0,Stg)
  // 2000, XP and higher
  else if(@StgOpenStorageEx <> nilthen
    hr := StgOpenStorageEx(pwidechar(FileName),STGM_SHARE_EXCLUSIVE or
    STGM_READWRITE,STGFMT_ANY,0,nil,nil,@IID_IPropertySetStorage,Stg);

  if(hr = S_OK) then
  begin
    PropSetStg := Stg as IPropertySetStorage;

    // try to open an existing Property set
    hr         := PropSetStg.Open(GUID_InfoType,STGM_READWRITE or
      STGM_SHARE_EXCLUSIVE,PropStg);

    // create a new set
    if(hr <> S_OK) then
      hr       := PropSetStg.Create(GUID_InfoType,GUID_InfoType,
        PROPSETFLAG_DEFAULT,STGM_CREATE or STGM_READWRITE or
        STGM_SHARE_EXCLUSIVE,PropStg);

    if(hr = S_OK) then
    begin
      // set default length
      SetLength(PropSpec,0);
      SetLength(PropVariant,0);

      // fill
      for i := 0 to length(MultipleArrayList) - 1 do
        if(MultipleArrayList[i].pidInfoType <> 0and
          (MultipleArrayList[i].pidInfoStr <> nilthen
        begin
          SetLength(PropSpec,length(PropSpec) + 1);
          PropSpec[length(PropSpec)-1].ulKind       := PRSPEC_PROPID;
          PropSpec[length(PropSpec)-1].propid       :=
            MultipleArrayList[i].pidInfoType;

          SetLength(PropVariant,length(PropVariant) + 1);
          PropVariant[length(PropVariant)-1].vt     := VT_LPSTR;
          PropVariant[length(PropVariant)-1].pszVal :=
            MultipleArrayList[i].pidInfoStr;
        end;

      // write
      if(length(PropSpec) > 0and
        (length(PropVariant) > 0and
        (length(PropSpec) = length(PropVariant)) then
      begin
        if(PropStg.WriteMultiple(length(PropSpec),@PropSpec[0],
          @PropVariant[0],2) = S_OK) then
        begin
          Result := (PropStg.Commit(STGC_DEFAULT) = S_OK);
        end;

        SetLength(PropSpec,0);
        SetLength(PropVariant,0);
      end;
    end;
  end;
end;


//
// Main
//
var
  os : TOSVersionInfo;

initialization
  // get OS
  ZeroMemory(@os,sizeof(os));
  os.dwOSVersionInfoSize := sizeof(os);

  // is it Windows 2000 or higher?
  Win2k                  := (GetVersionEx(os)) and
    (os.dwMajorVersion >= 5and
    (os.dwPlatformId = VER_PLATFORM_WIN32_NT);

  CoInitialize(nil);

  dll := LoadLibrary(szOleDll);
  if(dll <> 0then
  begin
    StgOpenStorageEx := GetProcAddress(dll,'StgOpenStorageEx');
    if(@StgOpenStorageEx = nilthen
    begin
      FreeLibrary(dll);
      dll := 0;
    end;
  end;
finalization
  if(dll <> 0then FreeLibrary(dll);
  CoUninitialize;

end.



Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Sa 20.02.10 18:42 
Hallo und :welcome:

Könntest du so freundlich sein und kurz beschreiben, was dieser Code erreichen soll? Das sieht sehr nach dutzendfachem Aufruf von WinAPI-Funktionen aus. Dies läuft unter .NET ziemlich anders ab; aber vielleicht gibt es interne Standardlösungen.

Mit Environment.OSVersion bekommst du jedenfalls Name und Version des Betriebssystems.

Gruß Jürgen
lucky41 Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: So 21.02.10 13:15 
Hallo,

danke erst mal für deine Antwort.
Ich möchte die erweiterten Attribute im ntfs Filesystem nutzen.
Hierbei möchte ich nicht nur Office, sondern alle berücksichtigen.
Beispiel Text, PDF usw. mit Titel, Author, Comment usw. bestücken.
Leider funktioniert bei mir DSOFile nur bei Office Dateien.

Ich las das nun DSOFile durch StgOpenStorageEx ersetzt werden sollte.
Leider bin ich da wohl doch zu unbedarft :-) .
Wichtig wäre mir die Funktion SetFileSummaryInfo und evtl. GetFileSummaryInfo.
Obwohl das auch anders ginge.
Lesen würde ja auch mit anderen Mittels funktionieren.
Danke Gruß lucky
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: So 21.02.10 15:30 
Hallo,

zum Thema habe ich hier Informationen und weiterführende Links gefunden.

Willst du diese Informationen etwa auch ändern? Wenn das möglich wäre, wäre es - so wird das dort bezeichnet - ein großes Sicherheitsloch.

Gruß Jürgen
lucky41 Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: So 21.02.10 18:31 
Hallo,

ich weiß. Es wäre jedoch eine sehr einfache Variante zusätzliche Info's zu Dateien einzupflegen.
z.B. einen Index. So könnte ich sehr schell damit suchen und erkennen ob die Datei verschoben wurde.
Muss ich mir halt doch noch DSOFile anschauen.

Hast Du bereits Win7 und evtl. schon mal DSOFile benutzt.

Danke
Gruß Lucky