Windows Specific Exif Tag In Photos

Windows Specific Exif Tag In Photos

Tuesday, May 2, 2023 by Leon

While writing a custom batch application for a client to update standard EXIF tags for photos based on directory names I came across a particular challenge.

He asked if I could update the XPKeywords, XPAuthor, and XPTitle tags so that if someone downloaded one of his photos, right clicked on it, and chose Properties->Details the tags would display.

Since they are listed in the Exiv2 standard tags list I thought it would be a piece of cake. However it turns out that Windows doesn’t want a text string, it wants it in a byte array encoded in UCS2 (which I’ve never heard of before). Google/Github wasn’t much help, but I did find one reference to it, and through trial and error I came up with the following code which gets the job done. I’m not sure if this is the right way to do it, or the most efficient, but it works.

Basically, it wants a string comprised of the ascii value of each letter, separated by a zero, with three zeros at the end. Here is the C++ snippet that I’m using:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Note: allTags is a string with each tag separated by a space

string XPString;
string::iterator it; 

for ( it = allTags.begin() ; it != allTags.end() ; it++ ) { 
   if ( XPString.length() > 0 ) { 
       XPString += " 0 ";
   }   
   XPString += fmt::format("{:d}", *it);    
}   

XPString += " 000";
exifData[ "XPKeywords" ] = XPString;

Note: the above code uses the fmt C++ string formatting library. Even though I’m using c++20, since I’m cross compiling for some reason the new c++20 format library wasn’t available.


Logo by Pixel Perfect / Flat Icon