[ttssh2-commit] [9424] GetSpecialFolderAlloc() を追加

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2021年 9月 19日 (日) 00:47:17 JST


Revision: 9424
          https://osdn.net/projects/ttssh2/scm/svn/commits/9424
Author:   zmatsuo
Date:     2021-09-19 00:47:17 +0900 (Sun, 19 Sep 2021)
Log Message:
-----------
GetSpecialFolderAlloc() を追加

- パス長の上限をなくした

Modified Paths:
--------------
    trunk/teraterm/ttpmacro/ttmlib.c
    trunk/teraterm/ttpmacro/ttmlib.h

-------------- next part --------------
Modified: trunk/teraterm/ttpmacro/ttmlib.c
===================================================================
--- trunk/teraterm/ttpmacro/ttmlib.c	2021-09-18 15:47:07 UTC (rev 9423)
+++ trunk/teraterm/ttpmacro/ttmlib.c	2021-09-18 15:47:17 UTC (rev 9424)
@@ -204,47 +204,32 @@
   return TRUE;
 }
 
-int DoGetSpecialFolder(int CSIDL, PCHAR dest, int dest_len)
+static int GetSpecialFolderAlloc(const char *type, char **dest)
 {
-	char Path[MAX_PATH] = "";
-	LPITEMIDLIST pidl;
-
-	if (SHGetSpecialFolderLocation(NULL, CSIDL, &pidl) != S_OK) {
-		return 0;
-	}
-
-	SHGetPathFromIDList(pidl, Path);
-	CoTaskMemFree(pidl);
-
-	strncpy_s(dest, dest_len, Path, _TRUNCATE);
-	return 1;
-}
-
-int GetSpecialFolder(PCHAR dest, int dest_len, PCHAR type)
-{
 	typedef struct {
-		char *name;
+		const char *name;
+		REFKNOWNFOLDERID rfid;
 		int csidl;
 	} SpFolder;
 
 	static const SpFolder spfolders[] = {
-		{ "AllUsersDesktop",   CSIDL_COMMON_DESKTOPDIRECTORY },
-		{ "AllUsersStartMenu", CSIDL_COMMON_STARTMENU },
-		{ "AllUsersPrograms",  CSIDL_COMMON_PROGRAMS },
-		{ "AllUsersStartup",   CSIDL_COMMON_STARTUP },
-		{ "Desktop",           CSIDL_DESKTOPDIRECTORY },
-		{ "Favorites",         CSIDL_FAVORITES },
-		{ "Fonts",             CSIDL_FONTS },
-		{ "MyDocuments",       CSIDL_PERSONAL },
-		{ "NetHood",           CSIDL_NETHOOD },
-		{ "PrintHood",         CSIDL_PRINTHOOD },
-		{ "Programs",          CSIDL_PROGRAMS },
-		{ "Recent",            CSIDL_RECENT },
-		{ "SendTo",            CSIDL_SENDTO },
-		{ "StartMenu",         CSIDL_STARTMENU },
-		{ "Startup",           CSIDL_STARTUP },
-		{ "Templates",         CSIDL_TEMPLATES },
-		{ NULL,                -1}
+		{ "AllUsersDesktop",   &FOLDERID_PublicDesktop, CSIDL_COMMON_DESKTOPDIRECTORY },
+		{ "AllUsersStartMenu", &FOLDERID_CommonStartMenu, CSIDL_COMMON_STARTMENU },
+		{ "AllUsersPrograms",  &FOLDERID_CommonPrograms, CSIDL_COMMON_PROGRAMS },
+		{ "AllUsersStartup",   &FOLDERID_CommonStartup, CSIDL_COMMON_STARTUP },
+		{ "Desktop",           &FOLDERID_Desktop, CSIDL_DESKTOPDIRECTORY },
+		{ "Favorites",         &FOLDERID_Favorites, CSIDL_FAVORITES },
+		{ "Fonts",             &FOLDERID_Fonts, CSIDL_FONTS },
+		{ "MyDocuments",       &FOLDERID_Documents, CSIDL_PERSONAL },
+		{ "NetHood",           &FOLDERID_NetHood, CSIDL_NETHOOD },
+		{ "PrintHood",         &FOLDERID_PrintHood, CSIDL_PRINTHOOD },
+		{ "Programs",          &FOLDERID_Programs, CSIDL_PROGRAMS },
+		{ "Recent",            &FOLDERID_Recent, CSIDL_RECENT },
+		{ "SendTo",            &FOLDERID_SendTo, CSIDL_SENDTO },
+		{ "StartMenu",         &FOLDERID_StartMenu, CSIDL_STARTMENU },
+		{ "Startup",           &FOLDERID_Startup, CSIDL_STARTUP },
+		{ "Templates",         &FOLDERID_Templates, CSIDL_TEMPLATES },
+		{ NULL,                NULL, -1}
 	};
 
 	const SpFolder *p;
@@ -251,12 +236,26 @@
 
 	for (p = spfolders; p->name != NULL; p++) {
 		if (_stricmp(type, p->name) == 0) {
-			return DoGetSpecialFolder(p->csidl, dest, dest_len);
+			wchar_t *folderW;
+			_SHGetKnownFolderPath(p->rfid, 0, NULL, &folderW);
+			*dest = ToU8W(folderW);
+			free(folderW);
+			return 1;
 		}
 	}
+	*dest = NULL;
 	return 0;
 }
 
+int GetSpecialFolder(PCHAR dest, int dest_len, const char *type)
+{
+	char *folder;
+	GetSpecialFolderAlloc(type, &folder);
+	strncpy_s(dest, dest_len, folder, _TRUNCATE);
+	free(folder);
+	return 1;
+}
+
 void BringupWindow(HWND hWnd)
 {
 	DWORD thisThreadId;

Modified: trunk/teraterm/ttpmacro/ttmlib.h
===================================================================
--- trunk/teraterm/ttpmacro/ttmlib.h	2021-09-18 15:47:07 UTC (rev 9423)
+++ trunk/teraterm/ttpmacro/ttmlib.h	2021-09-18 15:47:17 UTC (rev 9424)
@@ -44,7 +44,7 @@
 void TTMGetDir(PCHAR Dir, int destlen);
 void TTMSetDir(const char *Dir);
 int GetAbsPath(PCHAR FName, int destlen);
-int GetSpecialFolder(PCHAR dest, int dest_len, PCHAR type);
+int GetSpecialFolder(PCHAR dest, int dest_len, const char *type);
 void BringupWindow(HWND hWnd);
 int MessageBoxHaltScript(HWND hWnd);
 


ttssh2-commit メーリングリストの案内
Back to archive index