Hey, Scripting Guy! How Can I Determine the Currency Symbol in Use on a Computer?
Hey, Scripting Guy! Is there any way to determine the currency symbol that’s in use on a computer?
Hey, IR. Before we get started today we wanted to mention that the Scripting Son picked up his second win in as many pitching starts the other day as the Juanita Rebels rolled past Franklin High School. The Scripting Son is now 2-0 for the season, with an earned-run average of 1.93.
Oh, and he’s also hitting .333 this year. Not that we’re trying to brag or anything.
Scratch that; we are trying to brag a little. In fact, the Scripting Guy who writes this column is so proud of the Scripting Son that he decided to bestow on his son scripting’s highest honor: he’s going to let the Scripting Son write today’s Hey, Scripting Guy! column. Take it away, son!
What? Where are you – wait, I don’t want to – Dad, come back here, I don’t want to do this, this is stupid, this – oh, man.
Um, hi, everyone. My dad kind of dropped this on me so you’re going to have to bear with me for a second. As far as I know, IR, there’s no WMI class or COM object that can tell you which currency symbol is currently in use on a computer. That’s probably the real reason my Dad is “letting” me write this column; he didn’t know how to answer your question.
But that’s all right. As any high school kid knows you can determine the currency symbol currently in use on a computer by using WMI to read that value from the registry:
Const HKEY_CURRENT_USER = &H80000001 strComputer = "." Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "Control Panel\International" strValueName = "sCurrency" objRegistry.GetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue Msgbox strValue
Admittedly, I’ve never read any of my dad’s articles, but if I know him he probably starts off by telling some long and pointless story. Along the way I’m sure he also stops to tell jokes that no one else thinks are funny. You want to know the truth? Most of the time no one has any idea what he’s even talking about. Like the time we went to Idaho for Vandal Preview Friday, and he starts going off on steroids in baseball and how he doesn’t see any difference between taking steroids and sleeping in a hyperbaric chamber, and he just kept going on and on and on until I finally said, “Would you just give it a rest? No one cares about your dumb opinions.” And that was true: no one else in the car did care about his dumb opinions.
No one else in the world, for that matter.
You know, there are probably days when you guys read his column and you just kind of shake your head and think, “Whoa.” Well, you ought to try living with him.
Anyway, unlike my dad, I’ll just get right to the point here. This script thing starts off by defining a constant named HKEY_CURRENT_USER and setting the value to &H80000001; we’ll use this constant to tell the script which registry hive to work with.
Note. I’ll give my dad a break here; HKEY_CURRENT_USER is really more the kind of name my mom would come up with. True story: she wanted to name me Iones but Dad talked her out of that.
Editor’s Note: To everyone out there named Iones, there’s absolutely nothing wrong with your name and the Scripting Son did not mean to imply that there is. (Okay, yes he did, but apparently the Scripting Editor is going to have to make the same efforts to keep the son from getting us into a lot of trouble as she does to keep his dad from getting us into trouble.)
After we define the constant we connect to the WMI service on the local computer. We then – just a second, please. You said you were going to let me write this all by myself; I don’t need your help. Well, if you’d quit bothering me I would tell them that. If you wanted to write today’s column then just write it. Otherwise, leave me alone.
Sorry. As my dad has … helpfully … pointed out, this script can also be run against a remote computer. All you need to do is assign the name of that remote computer to the variable strComputer (which, coincidentally enough, is yet another name my mom wanted to give me):
strComputer = "atl-fs-001"
By the way, there are two things you should keep in mind when it comes to our WMI connection string. First, we connect to the root\default namespace. That’s a little unusual; for most system administration scripts we connect to the root\cimv2 namespace. Also, note that we bind directly to the StdRegProv class. That’s because WMI’s methods for working with the registry are all “static” methods; that means that they work on the class itself and not on individual instances of the class. I don’t know whether that’s good or not; that’s just the way it is.
Just a second – because I’m not stupid, Dad, that’s how I know stuff like that. Geez, everyone knows about WMI namespaces and static methods. Don’t you have anything else to do? Hey, look: some kids are playing in our yard! Why don’t you go yell at them?
That should keep him busy for awhile.
OK. After we connect to the WMI service we use these two lines of code to assign values to a pair of variables:
strKeyPath = "Control Panel\International" strValueName = "sCurrency"
The variable strKeyPath identifies the registry key (in the HKEY_CURRENT_USER registry hive) that we want to work with; the variable sCurrency is the registry value we want to read.
Speaking of which – Dad, don’t touch my stuff. Just leave it alone, OK? Thank you.
Sorry. Anyway, to retrieve the currency symbol from the registry all we have to do now is call the GetStringValue method, passing this method the constant HKEY_CURRENT_USER and the variables strKeyPath, strValueName, and strValue:
objRegistry.GetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue
Excuse me again. No, Dad, I did not make a mistake. We have to pass GetStringValue a third variable; in this case, that’s a variable named strValue. (Which, by the way, is my middle name. Dad had to compromise on that one.) strValue happens to be an out parameter: we supply a variable name, and GetStringValue assigns the value read from the registry to this out parameter. In other words, GetStringValue will retrieve the currency symbol from the registry, then assign that symbol to the out parameter. That means we can determine the currency symbol simply by echoing back the value of strValue:
On our test machine, that message box looks a little something like this:
Did someone out there have a question? Why did we use a message box rather than just echoing back the value to the command window? Hey, Dad, why did we use a message box rather than just echoing back the value to the command window? Never mind; he has no idea. And yet he still thinks he’s the Scripting Guy.
Well, I’ll tell you why we did that: we did that because, depending on your computer settings, there’s a good chance that the Euro symbol won’t display properly in the command window. For example, on our test machine echoing back the Euro symbol gives us output that looks like this:
Cute, but not what we were hoping to see.
By the way, if you want to change the currency symbol you can use a script similar to this one:
Const HKEY_CURRENT_USER = &H80000001 strComputer = "." Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "Control Panel\International" strValueName = "sCurrency" strValue = "$" objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue
As you can see, this is very similar to the script for retrieving the currency symbol from the registry. In fact, there are only two differences. For one, we don’t use an out parameter in this script; instead, we assign the desired currency symbol the variable strValue:
strValue = "$"
Second, we use the SetStringValue method to write this new value to the registry:
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue
It’s as easy as that.
That’s all I have time for today; after all, we have practice this afternoon, even though it is spring break. But don’t worry; my dad will be back tomorrow.
Lucky you, huh?