Get user picture

一笑奈何 提交于 2021-02-17 20:51:51

问题


OS: Win7x64 (2008,2008r2). Lang: Delphi Xe2.

enter image description hereenter image description here

  1. How to receive a full path (and file name) to the image "user account picture"?
  2. How to set new picture?

Example on delphi plz.

Need: ...function GetCurrentUserPicture:string;

...function GetUserPicture(UserName:String):string;

...function SetUserNewPicture(UserName, ImageFileName:String):bool;


回答1:


There is an undocumented function in shell32.dll. On Windows XP its ordinal is 233, on Windows Vista and 7 its ordinal is 261.

Its function prototype (from Airesoft) is:

HRESULT WINAPI SHGetUserPicturePath (
    LPCWSTR pwszPicOrUserName, 
    DWORD sguppFlags, 
    LPWSTR pwszPicPath, 
    UINT picPathLen 
)

You can use this function to retrieve the path where the user picture is stored. Just pass the user name as pwszPicOrUserName, the buffer where you want to store the path to the picture as pwszPicPath and the size of the buffer in chars as picPathLen. You can set sguppFlags to 0 or to any of the other flags posssible.

There is also a undocumented function which you can use in order to set the user picture of a user. Its ordinal is 234 on Windows XP, 262 on Windows Vista and Windows 7.

Its function prototype (from Airesoft) is:

HRESULT WINAPI SHSetUserPicturePath ( 
    LPWSTR pwszAcctName, 
    DWORD reserved, 
    LPCWSTR pwszPictureFile 
)

Pass the name of the user whose picture should be changed as pwszAcctName and the path to the picture you want to set as pwszPictureFile. Set reserved to 0. You have to initialize COM prior to calling this function.

According to Microsoft you should not rely on undocumented function because they can be removed or changed with any patch which is installed on Windows.




回答2:


According to MSDN:

In Windows 7 or later, each user profile has an associated image presented as a user tile. These tiles appear to users on the User Accounts Control Panel item and its Manage Accounts subpage.. The image files for the default Guest and default User accounts also appear here if you have Administrator access rights.

....

The user's tile image is stored as C:\Users\<username>\Local\Temp folder as .bmp. Any slash characters () are converted to plus sign characters (+). For example, DOMAIN\user is converted to DOMAIN+user.

I could not find an API to obtain the image and since the official documentation is calling out this implementation detail I think that means that you are safe to rely on it. That is I think this is a supported way to obtain the tile image.



来源:https://stackoverflow.com/questions/9148220/get-user-picture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!