windows os stuff

-new subgroup
- avoid fied size types
- FOLDERID
-  registr get sub key
This commit is contained in:
vscosta 2014-11-10 01:51:33 +00:00
parent 24920a230f
commit 1650b12267
1 changed files with 311 additions and 35 deletions

View File

@ -26,6 +26,12 @@
#define __WINDOWS__ 1
#endif
/** @defgroup InputOutputNTIO WIN32 Specific Input/Output
* @ingroup InputOutput
*
* @{
*/
#ifdef __WINDOWS__
#define WINVER 0x0501
#if (_MSC_VER >= 1300) || __MINGW32__
@ -583,13 +589,25 @@ win_shell(term_t op, term_t file, term_t how)
succeed;
}
/** @pred win_shell(+Operation, +Document)
*
* This SWI Windows Built-in is a simplified version of win_sh/3 that
* opens a document using the WIN32 API ShellExecute() operation.
*/
static
PRED_IMPL("win_shell", 2, win_shell2, 0)
{ return win_shell(A1, A2, 0);
}
/** 2pred win_shell(+Operation, +Document)
*
* This SWI Windows Built-in uses the WIN32 API ShellExecute() command
* to either:
*
* + `open` _Document_ using the default Windows helper.
* + `print` _Document_
* + `explore`, `edit` or any operation accepted by [ShellExecute](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx).
*/
static
PRED_IMPL("win_shell", 3, win_shell3, 0)
{ return win_shell(A1, A2, A3);
@ -695,7 +713,7 @@ PRED_IMPL("win_add_dll_directory", 2, win_add_dll_directory, 0)
return PL_representation_error("file_name");
if ( load_library_search_flags() )
{ if ( (cookie = (*f_AddDllDirectoryW)(dirw)) )
return PL_unify_int64(A2, (int64_t)cookie);
return PL_unify_intptr(A2, (intptr_t)cookie);
return PL_error(NULL, 0, WinError(), ERR_SYSCALL, "AddDllDirectory()");
} else
return FALSE;
@ -706,9 +724,9 @@ PRED_IMPL("win_add_dll_directory", 2, win_add_dll_directory, 0)
static
PRED_IMPL("win_remove_dll_directory", 1, win_remove_dll_directory, 0)
{ int64_t icookie;
{ intptr_t icookie;
if ( PL_get_int64_ex(A1, &icookie) )
if ( PL_get_intptr_ex(A1, &icookie) )
{ if ( f_RemoveDllDirectory )
{ if ( (*f_RemoveDllDirectory)((DLL_DIRECTORY_COOKIE)icookie) )
return TRUE;
@ -845,43 +863,218 @@ ms_snprintf(char *buffer, size_t count, const char *fmt, ...)
#include <shlobj.h>
#endif
#if ONLY_SUPPORT_RECENT_WINDOWS
typedef struct folderid
{ KNOWNFOLDERID *csidl;
char *name;
} folderid;
static folderid *folderids;
// do nothing the first step
static void
in(REFKNOWNFOLDERID idp, char *name, int i)
{
if (folderids) {
folderids[i].csidl = (KNOWNFOLDERID *)idp;
folderids[i].name = name;
}
}
// initialize twice: first, just count, second
// incrementing i to point to the next free entry
static int
j(int i)
{
in( &FOLDERID_AccountPictures, "AccountPictures" , i++);
in( &FOLDERID_AddNewPrograms, "AddNewPrograms" , i++);
in( &FOLDERID_AdminTools, "AdminTools" , i++);
in( &FOLDERID_AppsFolder, "AppsFolder" , i++);
in( &FOLDERID_ApplicationShortcuts, "ApplicationShortcuts" , i++);
in( &FOLDERID_AppUpdates, "AppUpdates" , i++);
in( &FOLDERID_CDBurning, "CDBurning" , i++);
in( &FOLDERID_ChangeRemovePrograms, "ChangeRemovePrograms" , i++);
in( &FOLDERID_CommonAdminTools, "CommonAdminTools" , i++);
in( &FOLDERID_CommonOEMLinks, "CommonOEMLinks" , i++);
in( &FOLDERID_CommonPrograms, "CommonPrograms" , i++);
in( &FOLDERID_CommonStartMenu, "CommonStartMenu" , i++);
in( &FOLDERID_CommonStartup, "CommonStartup" , i++);
in( &FOLDERID_CommonTemplates, "CommonTemplates" , i++);
in( &FOLDERID_ComputerFolder, "ComputerFolder" , i++);
in( &FOLDERID_ConflictFolder, "ConflictFolder" , i++);
in( &FOLDERID_ConnectionsFolder, "ConnectionsFolder" , i++);
in( &FOLDERID_Contacts, "Contacts" , i++);
in( &FOLDERID_ControlPanelFolder, "ControlPanelFolder" , i++);
in( &FOLDERID_Cookies, "Cookies" , i++);
in( &FOLDERID_Desktop, "Desktop" , i++);
in( &FOLDERID_DeviceMetadataStore, "DeviceMetadataStore" , i++);
in( &FOLDERID_Documents, "Documents" , i++);
in( &FOLDERID_DocumentsLibrary, "DocumentsLibrary" , i++);
in( &FOLDERID_Downloads, "Downloads" , i++);
in( &FOLDERID_Favorites, "Favorites" , i++);
in( &FOLDERID_Fonts, "Fonts" , i++);
in( &FOLDERID_Games, "Games" , i++);
in( &FOLDERID_GameTasks, "GameTasks" , i++);
in( &FOLDERID_History, "History" , i++);
in( &FOLDERID_HomeGroup, "HomeGroup" , i++);
in( &FOLDERID_HomeGroupCurrentUser, "HomeGroupCurrentUser" , i++);
in( &FOLDERID_ImplicitAppShortcuts, "ImplicitAppShortcuts" , i++);
in( &FOLDERID_InternetCache, "InternetCache" , i++);
in( &FOLDERID_InternetFolder, "InternetFolder" , i++);
in( &FOLDERID_Libraries, "Libraries" , i++);
in( &FOLDERID_Links, "Links" , i++);
in( &FOLDERID_LocalAppData, "LocalAppData" , i++);
in( &FOLDERID_LocalAppDataLow, "LocalAppDataLow" , i++);
in( &FOLDERID_LocalizedResourcesDir, "LocalizedResourcesDir" , i++);
in( &FOLDERID_Music, "Music" , i++);
in( &FOLDERID_MusicLibrary, "MusicLibrary" , i++);
in( &FOLDERID_NetHood, "NetHood" , i++);
in( &FOLDERID_NetworkFolder, "NetworkFolder" , i++);
in( &FOLDERID_OriginalImages, "OriginalImages" , i++);
in( &FOLDERID_PhotoAlbums, "PhotoAlbums" , i++);
in( &FOLDERID_Pictures, "Pictures" , i++);
in( &FOLDERID_PicturesLibrary, "PicturesLibrary" , i++);
in( &FOLDERID_Playlists, "Playlists" , i++);
in( &FOLDERID_PrintHood, "PrintHood" , i++);
in( &FOLDERID_PrintersFolder, "PrintersFolder" , i++);
in( &FOLDERID_Profile, "Profile" , i++);
in( &FOLDERID_ProgramData, "ProgramData" , i++);
in( &FOLDERID_ProgramFiles, "ProgramFiles" , i++);
in( &FOLDERID_ProgramFilesX64, "ProgramFilesX64" , i++);
in( &FOLDERID_ProgramFilesX86, "ProgramFilesX86" , i++);
in( &FOLDERID_ProgramFilesCommon, "ProgramFilesCommon" , i++);
in( &FOLDERID_ProgramFilesCommonX64, "ProgramFilesCommonX64" , i++);
in( &FOLDERID_ProgramFilesCommonX86, "ProgramFilesCommonX86" , i++);
in( &FOLDERID_Programs, "Programs" , i++);
in( &FOLDERID_Public, "Public" , i++);
in( &FOLDERID_PublicDesktop, "PublicDesktop" , i++);
in( &FOLDERID_PublicDocuments, "PublicDocuments" , i++);
in( &FOLDERID_PublicDownloads, "PublicDownloads" , i++);
in( &FOLDERID_PublicGameTasks, "PublicGameTasks" , i++);
in( &FOLDERID_PublicLibraries, "PublicLibraries" , i++);
in( &FOLDERID_PublicMusic, "PublicMusic" , i++);
in( &FOLDERID_PublicPictures, "PublicPictures" , i++);
in( &FOLDERID_PublicRingtones, "PublicRingtones" , i++);
in( &FOLDERID_PublicUserTiles, "PublicUserTiles" , i++);
in( &FOLDERID_PublicVideos, "PublicVideos" , i++);
in( &FOLDERID_QuickLaunch, "QuickLaunch" , i++);
in( &FOLDERID_Recent, "Recent" , i++);
in( &FOLDERID_RecordedTVLibrary, "RecordedTVLibrary" , i++);
in( &FOLDERID_RecycleBinFolder, "RecycleBinFolder" , i++);
in( &FOLDERID_ResourceDir, "ResourceDir" , i++);
in( &FOLDERID_Ringtones, "Ringtones" , i++);
in( &FOLDERID_RoamingAppData, "RoamingAppData" , i++);
in( &FOLDERID_RoamingTiles, "RoamingTiles" , i++);
in( &FOLDERID_RoamedTileImages, "RoamedTileImages" , i++);
in( &FOLDERID_SampleMusic, "SampleMusic" , i++);
in( &FOLDERID_SamplePictures, "SamplePictures" , i++);
in( &FOLDERID_SamplePlaylists, "SamplePlaylists" , i++);
in( &FOLDERID_SampleVideos, "SampleVideos" , i++);
in( &FOLDERID_SavedGames, "SavedGames" , i++);
in( &FOLDERID_SavedSearches, "SavedSearches" , i++);
in( &FOLDERID_Screenshots, "Screenshots" , i++);
in( &FOLDERID_SEARCH_CSC, "SEARCH_CSC" , i++);
in( &FOLDERID_SearchHome, "SearchHome" , i++);
// in( &FOLDERID_SearchHistory, "SearchHistory" , i++);
in( &FOLDERID_SendTo, "SendTo" , i++);
in( &FOLDERID_SidebarDefaultParts, "SidebarDefaultParts" , i++);
in( &FOLDERID_SidebarParts, "SidebarParts" , i++);
in( &FOLDERID_StartMenu, "StartMenu" , i++);
in( &FOLDERID_Startup, "Startup" , i++);
in( &FOLDERID_SyncManagerFolder, "SyncManagerFolder" , i++);
in( &FOLDERID_SyncResultsFolder, "SyncResultsFolder" , i++);
in( &FOLDERID_SyncSetupFolder, "SyncSetupFolder" , i++);
in( &FOLDERID_System, "System" , i++);
in( &FOLDERID_SystemX86, "SystemX86" , i++);
in( &FOLDERID_Templates, "Templates" , i++);
in( &FOLDERID_UserPinned, "UserPinned" , i++);
in( &FOLDERID_UserProfiles, "UserProfiles" , i++);
in( &FOLDERID_UserProgramFiles, "UserProgramFiles" , i++);
in( &FOLDERID_UserProgramFilesCommon, "UserProgramFilesCommon" , i++);
in( &FOLDERID_UsersFiles, "UsersFiles" , i++);
in( &FOLDERID_UsersLibraries, "UsersLibraries" , i++);
in( &FOLDERID_Videos, "Videos" , i++);
in( &FOLDERID_VideosLibrary, "VideosLibrary" , i++);
in( &FOLDERID_Windows, "Windows" , i++);
in( NULL, NULL, i++);
return i;
};
static void
init_folderids(void)
{
int n = j(0);
folderids = ( folderid * )malloc( n*sizeof(folderid) );
j( 0 );
}
static int
unify_csidl_path(term_t t, REFKNOWNFOLDERID csidl)
{ wchar_t buf[MAX_PATH];
if ( SHGetKnownFolderPathW(csidl, 0, NULL, csidl, buf) )
{ wchar_t *p;
for(p=buf; *p; p++)
{ if ( *p == '\\' )
*p = '/';
}
return PL_unify_wchars(t, PL_ATOM, -1, buf);
} else
return PL_error(NULL, 0, WinError(), ERR_SYSCALL, "SHGetKnownFolderPath");
}
#else
typedef struct folderid
{ int csidl;
const char *name;
} folderid;
static const folderid folderids[] =
{ { CSIDL_COMMON_ALTSTARTUP, "common_altstartup" },
{ CSIDL_ALTSTARTUP, "altstartup" },
{ { CSIDL_ADMINTOOLS, "admintools" },
// { CSIDL_ALTSTARTUP, "altstartup" },
{ CSIDL_APPDATA, "appdata" },
{ CSIDL_CONTROLS, "controls" },
{ CSIDL_COOKIES, "cookies" },
{ CSIDL_DESKTOP, "desktop" },
{ CSIDL_COMMON_DESKTOPDIRECTORY, "common_desktopdirectory" },
{ CSIDL_DESKTOPDIRECTORY, "desktopdirectory" },
{ CSIDL_COMMON_FAVORITES, "common_favorites" },
{ CSIDL_FAVORITES, "favorites" },
{ CSIDL_FONTS, "fonts" },
{ CSIDL_HISTORY, "history" },
{ CSIDL_INTERNET_CACHE, "internet_cache" },
{ CSIDL_INTERNET, "internet" },
{ CSIDL_DRIVES, "drives" },
{ CSIDL_PERSONAL, "personal" },
{ CSIDL_NETWORK, "network" },
{ CSIDL_NETHOOD, "nethood" },
{ CSIDL_PERSONAL, "personal" },
{ CSIDL_PRINTERS, "printers" },
{ CSIDL_PRINTHOOD, "printhood" },
{ CSIDL_COMMON_ADMINTOOLS, "common_admintools" },
{ CSIDL_COMMON_APPDATA, "common_appdata" },
// { CSIDL_COMMON_DESKTOPDIRECTORY, "common_desktopdirectory" },
{ CSIDL_COMMON_DOCUMENTS, "common_documents" },
// { CSIDL_COMMON_FAVORITES, "common_favorites" },
{ CSIDL_COMMON_PROGRAMS, "common_programs" },
{ CSIDL_PROGRAMS, "programs" },
{ CSIDL_RECENT, "recent" },
{ CSIDL_BITBUCKET, "bitbucket" },
{ CSIDL_SENDTO, "sendto" },
{ CSIDL_COMMON_STARTMENU, "common_startmenu" },
{ CSIDL_STARTMENU, "startmenu" },
{ CSIDL_COMMON_STARTUP, "common_startup" },
{ CSIDL_STARTUP, "startup" },
{ CSIDL_TEMPLATES, "templates" },
// { CSIDL_COMMON_STARTMENU, "common_startmenu" },
// { CSIDL_COMMON_STARTUP, "common_startup" },
{ CSIDL_COOKIES, "cookies" },
// { CSIDL_BITBUCKET, "bitbucket" },
// { CSIDL_CONTROLS, "controls" },
// { CSIDL_DESKTOP, "desktop" },
// { CSIDL_DESKTOPDIRECTORY, "desktopdirectory" },
// { CSIDL_DRIVES, "drives" },
// { CSIDL_FAVORITES, "favorites" },
{ CSIDL_FLAG_CREATE, "flag_create" },
{ CSIDL_FLAG_DONT_VERIFY, "flag_dont_verify" },
// { CSIDL_FONTS, "fonts" },
{ CSIDL_HISTORY, "history" },
// { CSIDL_INTERNET, "internet" },
{ CSIDL_INTERNET_CACHE, "internet_cache" },
{ CSIDL_LOCAL_APPDATA, "local_appdata" },
{ CSIDL_MYPICTURES, "mypictures" },
// { CSIDL_NETHOOD, "nethood" },
// { CSIDL_NETWORK, "network" },
{ CSIDL_PERSONAL, "personal" },
// { CSIDL_PRINTERS, "printers" },
// { CSIDL_PRINTHOOD, "printhood" },
// { CSIDL_PROGRAMS, "programs" },
{ CSIDL_PROGRAM_FILES, "program_files" },
{ CSIDL_PROGRAM_FILES_COMMON, "program_files_common" },
//{ CSIDL_RECENT, "recent" },
// { CSIDL_SENDTO, "sendto" },
// { CSIDL_STARTMENU, "startmenu" },
{ CSIDL_SYSTEM, "system" },
// { CSIDL_TEMPLATES, "templates" },
{ CSIDL_WINDOWS, "windows" },
{ 0, NULL }
};
@ -890,7 +1083,11 @@ static int
unify_csidl_path(term_t t, int csidl)
{ wchar_t buf[MAX_PATH];
if ( SHGetSpecialFolderPathW(0, buf, csidl, FALSE) )
if ( SUCCEEDED(SHGetFolderPathW(NULL,
CSIDL_PERSONAL|CSIDL_FLAG_CREATE,
NULL,
0,
buf)) )
{ wchar_t *p;
for(p=buf; *p; p++)
@ -904,6 +1101,19 @@ unify_csidl_path(term_t t, int csidl)
}
#endif
/** @pred win_folder(?_KnowFolder_, -_Path_)
*
* This SWI Windows Built-in relates a Windows `known folder` with its
* corresponding file system _Path_. It can also be used to enumerate folderids/
*
* It is an interface to [SHGetKnownFolderPath](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762204(v=vs.85).aspx).
* Note that in order to follow Microsoft
* documentation, YAP supports `Known Folderids` instead of special folderids,
* as used in SWI-Prolog. Also, names in YAP are obtained by removing
* the prefix `FOLDERID_`: no further processing is made to convert to lower caps.
*/
static
PRED_IMPL("win_folder", 2, win_folder, PL_FA_NONDETERMINISTIC)
{ GET_LD
@ -911,6 +1121,10 @@ PRED_IMPL("win_folder", 2, win_folder, PL_FA_NONDETERMINISTIC)
switch( CTX_CNTRL )
{ case FRG_FIRST_CALL:
#if ONLY_SUPPORT_RECENT_WINDOWS
if (!folderids)
init_folderids();
#endif
if ( PL_is_variable(A1) )
{ n = 0;
goto generate;
@ -1020,6 +1234,14 @@ reg_open_key(const wchar_t *which, int create)
#define MAXREGSTRLEN 1024
/** @pred win_registry_get_value(+_Key_, +_Name_, -_Value_)
*
* This SWI Windows Built-in consults the Windows registry for a
* subkey _Name_ of attribute _Key_ and obtains its value _Value_.
*
* It is an interface to [RegQueryValueEx](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724911(v=vs.85).aspx), and it allows using the predefined
* keys such as `HKEY_LOCAL_MACHINE`.
*/
static
PRED_IMPL("win_registry_get_value", 3, win_registry_get_value, 0)
{ GET_LD
@ -1063,6 +1285,58 @@ PRED_IMPL("win_registry_get_value", 3, win_registry_get_value, 0)
return FALSE;
}
/** @pred win_registry_get_subkey(+_Key_, -_Name_)
*
* This non-deterministic Windows Built-in consults the Windows registry for all
* subkeys _Name_ of attribute _Key_. It can be used to enumerate and rebuild
* the registry.
*
* This built-in is an interface to [RegEnumKeyEx](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724862(v=vs.85).aspx)
*/
static
PRED_IMPL( "win_registry_get_subkey", 2, win_registry_get_subkey, PL_FA_NONDETERMINISTIC)
{ GET_LD
int n;
DWORD SubKeys;
wchar_t data[MAXREGSTRLEN/sizeof(wchar_t)];
size_t klen;
DWORD namlen = sizeof(data)*sizeof(wchar_t);
wchar_t *k;
HKEY key;
term_t Key = A1;
term_t Name = A2;
switch( CTX_CNTRL )
{ case FRG_FIRST_CALL:
n = 0;
break;
case FRG_REDO:
n = (int)CTX_INT+1;
break;
default:
succeed;
}
if ( !PL_get_wchars(Key, &klen, &k, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
if ( !(key=reg_open_key(k, FALSE)) )
return PL_error(NULL, 0, NULL, ERR_EXISTENCE, PL_new_atom("key"), Key);
DEBUG(9, Sdprintf("key = %p, name = %s\n", key, name));
if ( RegQueryInfoKey(key, NULL, NULL, NULL, &SubKeys, NULL, NULL, NULL,NULL,NULL,NULL,NULL ) != ERROR_SUCCESS ) {
return PL_error(NULL, 0, NULL, ERR_EXISTENCE, PL_new_atom("key"), Key);
}
if ( RegEnumKeyExW( key, n, data, &namlen, NULL, NULL, NULL, NULL ) != ERROR_SUCCESS) {
return PL_error(NULL, 0, NULL, ERR_EXISTENCE, PL_new_atom("key"), Key);
}
RegCloseKey(key);
int rc = PL_unify_wchars(Name, PL_ATOM,
namlen, (wchar_t*)data);
if (rc && rc == SubKeys-1) succeed;
ForeignRedoInt(n);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Get the local, global, trail and argument-stack defaults from the
@ -1124,6 +1398,7 @@ BeginPredDefs(win)
PRED_DEF("win_shell", 2, win_shell2, 0)
PRED_DEF("win_shell", 3, win_shell3, 0)
PRED_DEF("win_registry_get_value", 3, win_registry_get_value, 0)
PRED_DEF("win_registry_get_subkey", 2, win_registry_get_subkey,PL_FA_NONDETERMINISTIC)
PRED_DEF("win_folder", 2, win_folder, PL_FA_NONDETERMINISTIC)
#ifdef EMULATE_DLOPEN
PRED_DEF("win_add_dll_directory", 2, win_add_dll_directory, 0)
@ -1133,3 +1408,4 @@ EndPredDefs
#endif /*__WINDOWS__*/
/** @} */