Index: trunk/ab5.0/abdev/abdev/FileOperation.cpp
===================================================================
--- trunk/ab5.0/abdev/abdev/FileOperation.cpp	(revision 700)
+++ trunk/ab5.0/abdev/abdev/FileOperation.cpp	(revision 705)
@@ -27,8 +27,8 @@
 	DWORD dw;
 	char *buffer,temporary[MAX_PATH];
-	HANDLE hFile;
-
-	hFile=CreateFile(path.c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
+
+	ATL::CHandle hFile(CreateFile(path.c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL));
 	if(hFile==INVALID_HANDLE_VALUE){
+		hFile.Detach();
 		//"\"%s\" ファイルの読み込みに失敗しました。"
 		sprintf(temporary,STRING_ERROR_CANT_FILEOPEN,path.c_str());
@@ -41,31 +41,29 @@
 	ReadFile(hFile,buffer,i,&dw,0);
 	buffer[dw]=0;
-	CloseHandle(hFile);
 	return buffer;
 }
 char *ReadBuffer_NonErrMsg( const std::string &path ){
 	extern HANDLE hHeap;
-	int i;
 	DWORD dw;
-	char *buffer;
-	HANDLE hFile;
-
-	hFile=CreateFile(path.c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
-	if(hFile==INVALID_HANDLE_VALUE) return 0;
-	i=GetFileSize(hFile,0);
-	buffer=(char *)HeapAlloc(hHeap,0,i+1);
+
+	ATL::CHandle hFile(CreateFile(path.c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL));
+	if(hFile==INVALID_HANDLE_VALUE){
+		hFile.Detach();
+		return 0;
+	}
+	int i=GetFileSize(hFile,0);
+	char *buffer=(char *)HeapAlloc(hHeap,0,i+1);
 	ReadFile(hFile,buffer,i,&dw,0);
 	buffer[dw]=0;
-	CloseHandle(hFile);
 	return buffer;
 }
 _int8 WriteBuffer(const std::string &path,const char *buffer,int length, bool isEnableError)
 {
-	HANDLE hFile;
 	DWORD dw;
-	hFile=CreateFile(path.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
+	ATL::CHandle hFile(CreateFile(path.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL));
 	if(hFile==INVALID_HANDLE_VALUE){
+		hFile.Detach();
+
 		char temporary[MAX_PATH];
-
 		if( isEnableError ){
 			//"\"%s\" ファイルへの書き込みに失敗しました。"
@@ -76,5 +74,4 @@
 	}
 	if(length) WriteFile(hFile,buffer,length,&dw,NULL);
-	CloseHandle(hFile);
 	return 1;
 }
@@ -371,5 +368,4 @@
 	int WndNum,i2;
 	char temporary[MAX_PATH],str[MAX_PATH],str2[32];
-	HANDLE fh;
 	DWORD dummy;
 
@@ -404,11 +400,14 @@
 		if(SaveFileName){
 			lstrcpy(temporary,SaveFileName);
-			if((fh=CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL))==INVALID_HANDLE_VALUE){
-				sprintf(str,STRING_FILE_OVERWRIDE,temporary);
-				if(MessageBox(hOwner,str,APPLICATION_NAME,MB_YESNO|MB_ICONINFORMATION)==IDNO){
-					return 0;
+			{
+				ATL::CHandle fh(CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL));
+				if(fh==INVALID_HANDLE_VALUE){
+					fh.Detach();
+					sprintf(str,STRING_FILE_OVERWRIDE,temporary);
+					if(MessageBox(hOwner,str,APPLICATION_NAME,MB_YESNO|MB_ICONINFORMATION)==IDNO){
+						return 0;
+					}
 				}
 			}
-			CloseHandle(fh);
 
 			//ドキュメント セレクト コンボボックスから消去
@@ -478,13 +477,15 @@
 		////////////////////////
 
-		fh=CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
-		if(fh==INVALID_HANDLE_VALUE){
-			sprintf(str,STRING_ERROR_CANT_FILESAVE,temporary);
-			MessageBox(hOwner,str,STRING_ERROR,MB_OK|MB_ICONSTOP);
-			return 0;
-		}
-		WriteFile(fh,pBuf,strlen(pBuf),&dummy,NULL);
-		SetFileIdentityFromFile(*MdiInfo[WndNum], fh);
-		CloseHandle(fh);
+		{
+			ATL::CHandle fh(CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL));
+			if(fh==INVALID_HANDLE_VALUE){
+				fh.Detach();
+				sprintf(str,STRING_ERROR_CANT_FILESAVE,temporary);
+				MessageBox(hOwner,str,STRING_ERROR,MB_OK|MB_ICONSTOP);
+				return 0;
+			}
+			WriteFile(fh,pBuf,strlen(pBuf),&dummy,NULL);
+			SetFileIdentityFromFile(*MdiInfo[WndNum], fh);
+		}
 
 
@@ -501,12 +502,14 @@
 		if(SaveFileName){
 			lstrcpy(temporary,SaveFileName);
-			if((fh=CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL))==INVALID_HANDLE_VALUE){
-				sprintf(str,STRING_FILE_OVERWRIDE,temporary);
-				if(MessageBox(hOwner,str,APPLICATION_NAME,MB_YESNO|MB_ICONINFORMATION)==IDNO){
-					CloseHandle(fh);
-					return 0;
+			{
+				ATL::CHandle fh(CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL));
+				if(fh==INVALID_HANDLE_VALUE){
+					fh.Detach();
+					sprintf(str,STRING_FILE_OVERWRIDE,temporary);
+					if(MessageBox(hOwner,str,APPLICATION_NAME,MB_YESNO|MB_ICONINFORMATION)==IDNO){
+						return 0;
+					}
 				}
 			}
-			CloseHandle(fh);
 			MdiInfo[WndNum]->path = temporary;
 			i2=SendMessage(hDocCombo,CB_FINDSTRINGEXACT,0,(LONG_PTR)MdiInfo[WndNum]->title.c_str());
