5

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

enter image description here enter 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;

mjn
  • 36,362
  • 28
  • 176
  • 378
Gu.
  • 1,947
  • 4
  • 30
  • 49
  • 1
    Note that it will [cost 1,600 € a day](http://www.guardian.co.uk/business/2012/jan/20/greek-ancient-sites-for-rent?newsfeed=true) to take pictures in Delphi - SCNR :) – mjn Feb 05 '12 at 10:26
  • 3
    Changing user profile pic might be not so easy on Vista/Win7. [read this](http://social.msdn.microsoft.com/Forums/en-US/windowsuidevelopment/thread/c720975f-e8cd-4e33-983e-58269ad33f6c). Active Directory scripting [might help](http://stackoverflow.com/q/1978717/937125). – kobik Feb 05 '12 at 11:54

2 Answers2

11

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Norbert Willhelm
  • 2,579
  • 1
  • 23
  • 33
  • 1
    +1, cool, there's a lot of undocumented stuff on [`Airesoft`](http://undoc.airesoft.co.uk/index.php) pages. Great tip! – TLama Feb 05 '12 at 13:10
  • 1
    Could the downvoter please explain? What is wrong about my answer? – Norbert Willhelm Feb 05 '12 at 15:51
  • 3
    Downvote was a stray click from my mobile browser. Not sure how that happened. Now removed. That said, I have to say that I would be uneasy with using an undocumented feature like this, especially when there is a documented way to do that, that, incidentally yields identical results. – David Heffernan Feb 05 '12 at 16:08
  • @David, personally I'm not afraid of undocumented things because I have to say, some of the documented stuff is in a more misleading state than if it would be undocumented. But it's ***just my personal opinion*** and I'm talking about functions which have at least exports. – TLama Feb 05 '12 at 17:12
  • Ah, ye good ol' revenge downvoting :-) Good website to accompany ntinternals in bookmarks, BTW! – OnTheFly Feb 05 '12 at 18:07
4

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • IIRC, this image is present since XP – OnTheFly Feb 05 '12 at 11:18
  • 3
    @user539484: the image yes, the location no. XP doesn't use the C:\Users folder. Not sure about Vista. So you would at least have to use something like the "USERDATA" CSIDL or KNOWN FOLDER. And a search just revealed that my XP account folder doesn't have a .bmp file anywhere in it. So while the account should have some setting for the image to use, the image itself is not stored/copied to the account like it is for Win7. – Marjan Venema Feb 05 '12 at 11:35
  • @MarjanVenema, search for `facepalm.jpg` there :-P *XP doesn't use the C:\Users folder.* Isn't that because XP's profile root is elsewhere? – OnTheFly Feb 05 '12 at 12:22
  • @user539484: No need for facepalms there. Why do you think I mentioned USERDATA CSIDL etc.? Fact remains: the user folder on XP (whereever it is located) does NOT contain (a copy of) the profile picture as it does do on Win7. Maybe the facepalm search is better off in your hands? – Marjan Venema Feb 05 '12 at 13:34
  • 1
    I'm curious about the downvote. Is it because this is Win7 specific? Or is it because there is a better way to do this that I am not aware of? – David Heffernan Feb 05 '12 at 13:37
  • @MarjanVenema, did you follow required step to allow Control Panel to create a volatile copy? – OnTheFly Feb 05 '12 at 15:23
  • 2
    David, apparently @user539484 seems to know a lot more but (s)he's spoonfeeding it... – Marjan Venema Feb 05 '12 at 15:31
  • 2
    @user539484: why should I have? Where does the (quoted) doc say a user is supposed to that? If it is required for Vista/XP, then my remarks still stand and your comment that the information supposedly can be used for Vista/XP is incorrect or at least unreliable. – Marjan Venema Feb 05 '12 at 15:33
  • @MarjanVenema, go read it instead of making noise responses then. Your "research" results are void if you didnt even bother to familiarise yourself with premise which you are supposedly studying about. And yes, i'm smart. – OnTheFly Feb 05 '12 at 17:59
  • @user539484 I don't understand the point you are making. Perhaps I'm not smart. In which case could you explain what you mean? Since you are smart you will be able to explain it in a way that we can understand. – David Heffernan Feb 05 '12 at 18:04
  • Doh. Did you read an article before referring to OP? `The user's tile is not guaranteed to be present. If the user deletes the .bmp file, for instance manually or through a utility that deletes temporary files, that user tile is not automatically recreated until ...` – OnTheFly Feb 05 '12 at 18:17
  • @user Sure I read it. But so what? Do you have a better solution. Why do you have to be so cryptic? That's not smart. – David Heffernan Feb 05 '12 at 18:28
  • 1
    David, it seems we have a new DownvoterStepIntoTheLight aka PrematureOptimization in our midst. @user539484 at least displays almost all of that one's behavioural patterns. – Marjan Venema Feb 05 '12 at 18:58
  • Lies and slander, my dear. But whatever. Could your friend @MarjanVenema please stop addressing to **me** during your tender private chit-chat? Specific answer for the specific question is what this website about. For your other social *interrelations* you have a whole fakebook. – OnTheFly Feb 05 '12 at 19:45