WinXPにて ファイルのプロパティ→概要を書き換えるには?

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
なすがに

WinXPにて ファイルのプロパティ→概要を書き換えるには?

#1

投稿記事 by なすがに » 16年前

Windows XP Pro NTFS C言語(コンパイラはcygwinでgccを使用) にて、
ファイルのプロパティ→概要タブにある情報を読み書きしたいと考えています。
ググってみたところ、IPropertyStorageのメソッドを使えば安全に
書き換えができるというところまでわかりましたが、
具体的にどういったコードを書けばアクセスできるのかがわかりません。
必死にサンプルプログラムがないか探しましたが、見つかりませんでした。
そこで、
簡単なサンプルか、参考になるページを教えていただけませんでしょうか。

プログラムの経験としましては、C言語の基礎はできる程度です。
APIは使用したことがありません。

ほかに必要な情報などがありましたらご指摘くだされば補足いたします。
よろしくお願いいたします。

Justy

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#2

投稿記事 by Justy » 16年前

CodeProject -Access the Summary Information Property Set of a file using Visual C++.
ttp://www.codeproject.com/KB/files/SummInfoPropSetFile.aspx

 これなんてどうですか?
 書いてあるサンプルは C:\Document.txtにあるファイルの概要タブのタイトルを
書き換えるものになっています。

なすがに

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#3

投稿記事 by なすがに » 16年前

早速のご回答ありがとうございます。

教えていただいたページのサンプルコードをコンパイルしようとしたところ、以下のエラーが出ました。
stdafx.hがないとのことですが、これは本来何に付属しているものなのでしょうか。
使用したコンパイラは、cygwin付属のgccです。

/***************************************************************************************/
C:\Programming\myc\rwproperty>gcc -o proptest2 proptest2.c
proptest2.c:1:20: stdafx.h: No such file or directory
proptest2.c: In function `WinMain':
proptest2.c:62: error: `reinterpret_cast' undeclared (first use in this function)
proptest2.c:62: error: (Each undeclared identifier is reported only once
proptest2.c:62: error: for each function it appears in.)
proptest2.c:62: error: syntax error before "void"
proptest2.c:66: error: `throw' undeclared (first use in this function)
proptest2.c:66: error: syntax error before string constant
proptest2.c:75: error: structure has no member named `Create'
proptest2.c:82: error: syntax error before string constant
proptest2.c:102: error: structure has no member named `WriteMultiple'
proptest2.c:103: error: `PID_FIRST_USABLE' undeclared (first use in this function)
proptest2.c:106: error: syntax error before string constant
proptest2.c:109: error: structure has no member named `Release'
proptest2.c:116: error: structure has no member named `Open'
proptest2.c:121: error: syntax error before string constant
proptest2.c:126: error: structure has no member named `ReadMultiple'
proptest2.c:128: error: syntax error before string constant
proptest2.c:131: error: `new' undeclared (first use in this function)
proptest2.c:131: error: syntax error before "char"
proptest2.c:143: error: syntax error before string constant

Justy

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#4

投稿記事 by Justy » 16年前

 あー、このサンプルは C++か。
 んー、とりあえずCに直してみましたが、cygwinで通るかどうかはわかりません。
 うまくいかなかったら他の人の回答を待つか、自力で改良してみてください。

[color=#d0d0ff" face="monospace]
#include <stdio.h>
#define CINTERFACE
#define COBJMACROS
#include <windows.h>
#include <ole2.h>

#pragma comment( lib, "ole32.lib" )


const FMTID PropSetfmtid ={
/* F29F85E0-4FF9-1068-AB91-08002B27B3D9 */
0xf29f85e0,
0x4ff9,
0x1068,
{0xab, 0x91, 0x08, 0x00, 0x2b, 0x27, 0xb3, 0xd9 }
};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// TODO: Place code here.

IPropertySetStorage *pPropSetStg = NULL;
IPropertyStorage *pPropStg = NULL;
PROPSPEC propspec;
PROPVARIANT propWrite;
PROPVARIANT propRead;
HRESULT hr = S_OK;
char* str;

// Open a file and a property set within it.
hr = StgOpenStorageEx( L"C:\\Document.txt",
STGM_DIRECT|STGM_SHARE_EXCLUSIVE|
STGM_READWRITE,
STGFMT_ANY,
0,
NULL,
NULL,
&IID_IPropertySetStorage,
(void**)(&pPropSetStg) );


if( FAILED(hr) )
return 0;

hr = IPropertySetStorage_Create(pPropSetStg, &PropSetfmtid, NULL,
PROPSETFLAG_DEFAULT,
STGM_CREATE|STGM_READWRITE|
STGM_SHARE_EXCLUSIVE,
&pPropStg );
if( FAILED(hr) ) return 0;

propspec.ulKind = PRSPEC_PROPID;
propspec.propid = 0x00000002;

//specify the value of property
propWrite.vt = VT_LPWSTR;
propWrite.pwszVal = L"this value set through code";

hr = IPropertyStorage_WriteMultiple(pPropStg, 1, &propspec,
&propWrite, PID_FIRST_USABLE );
if( FAILED(hr) ) return 0;

IPropertyStorage_Release(pPropStg);
pPropStg = NULL;

//again open the property set
hr = IPropertySetStorage_Open(pPropSetStg, &PropSetfmtid,
STGM_READ|STGM_SHARE_EXCLUSIVE,
&pPropStg );
if( FAILED(hr) ) return 0;

// Read the property back and validate it
hr = IPropertyStorage_ReadMultiple(pPropStg, 1, &propspec, &propRead );
if( FAILED(hr) ) return 0;

str = (char *)malloc(wcslen(propRead.pwszVal) + 1);
wsprintfA ( str, "%S", propRead.pwszVal);
free(str);

if( hr == S_FALSE ) return 0;
else if( propWrite.vt != propRead.vt ) return 0;
else if( wcscmp( propWrite.pwszVal, propRead.pwszVal ) != 0 ) return 0;
else wprintf( L"Success\n" );

return 0;
}
[/color]

kokosan60

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#5

投稿記事 by kokosan60 » 16年前

>>Justyさん
このプログラムはwindows.hやWinMainを使用しているので
UNIX系のCygwinのgccでは通りません。

Justy

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#6

投稿記事 by Justy » 16年前


>このプログラムはwindows.hやWinMainを使用しているので
>UNIX系のCygwinのgccでは通りません

 cygwinでも windows.hとかもあったはずですし、
WinMainからの起動も -mwindowsのオプションで出来た、はずです。

 でも、たしかに、オプションの指定なしには通らないかもしれませんね。
 (pragma commentも機能しないでしょうし)

 つまり、
gcc -mwindows -mno-cygwin -lole32 -o proptest2 proptest2.c
 とかになるのかな
(うちには入れてないので確認はできませんが)

toyo

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#7

投稿記事 by toyo » 16年前

C:\cygwin\usr\include\w32api
にwindows.hその他ありますね

でもcygwinではIProperty~関数は使えないようです
VisualC++ではPropIdl.hで定義されてますがcygwinにはありませんでした

なすがに

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#8

投稿記事 by なすがに » 16年前

みなさん、たくさんのご回答本当にありがとうございます。
大変感謝しております。

Justyさんの書き直してくださったコードをコンパイルしようとしたところ、以下のようなエラーが出ました。
PID_FIRST_USABLE が宣言されていない、というのは、やはりcygwinではIProperty系の関数は使えないという
ことなのでしょうか。

/*****************************************************************/
C:\Programming\myc\rwproperty>gcc -mwindows -mno-cygwin -lole32 -o proptest3 proptest3.c
proptest3.c: In function `WinMain':
proptest3.c:60: error: `PID_FIRST_USABLE' undeclared (first use in this function)
proptest3.c:60: error: (Each undeclared identifier is reported only once
proptest3.c:60: error: for each function it appears in.)

Justy

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#9

投稿記事 by Justy » 16年前

 toyoさん、フォローありがとうございます。
 なるほど、PropIdl.hがない、と。
 

 このヘッダは Windows SDKの中にあるので、それを使えば
ひょっとしたら動くかもしれませんが……。

 cygwinだけで何とかするのは厳しいかもしれませんね。

なすがに

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#10

投稿記事 by なすがに » 16年前

PropIdl.h を探し、先のコンパイルエラーに関係のありそうなものを追加してみました。

typedef __RPC_unique_pointer IPropertySetStorage;
EXTERN_C const IID IID_IPropertySetStorage;
#define PID_FIRST_USABLE ( 0x2 )

この3行をグローバル領域に追記しました。
コンパイルエラーの行数は少なくなりましたが、意味を理解していないので何とも言えません。
申し訳ないです。

==================================================================
#include <stdio.h>
#define CINTERFACE
#define COBJMACROS
#include <windows.h>
#include <ole2.h>

typedef __RPC_unique_pointer IPropertySetStorage;
EXTERN_C const IID IID_IPropertySetStorage;
#define PID_FIRST_USABLE ( 0x2 )

#pragma comment( lib, "ole32.lib" )
const FMTID PropSetfmtid ={
/* F29F85E0-4FF9-1068-AB91-08002B27B3D9 */
0xf29f85e0,
0x4ff9,
…………
==================================================================

コンパイルエラー
==================================================================
$ gcc -o proptest4.out proptest4.c
proptest4.c:10: error: syntax error before "IPropertySetStorage"
proptest4.c:10: warning: useless keyword or type name in empty declaration
proptest4.c:10: warning: empty declaration
==================================================================

Justy

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#11

投稿記事 by Justy » 16年前

 cygwinを入れていないので、ほとんど想像でしかアドバイスできず、
この方向で進めても、解決しないか、解決してもそれまでに数十の問答を繰り返さないと
解決までいかないと思います。

 別の方が cygwin上で可能な別のアプローチを提案されるのを待つか、
この際 VisualStudioを入れてそちらを使ってみてはどうでしょうか。

なすがに

Re:WinXPにて ファイルのプロパティ→概要を書き換えるには?

#12

投稿記事 by なすがに » 16年前

わかりました。熱心なご回答ありがとうございました。
とりあえず、VC++でコンパイルして、このプログラムを外部的に利用する方向で考えてみます。

大変勉強になりました。ありがとうございました。

閉鎖

“C言語何でも質問掲示板” へ戻る