Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with converting string to hex 1

Status
Not open for further replies.

jrogersnd

MIS
Oct 21, 2002
51
US
Hi everyone,

I'm a bit of a newbie when it comes to scripting, but I'm learning at a reasonable pace. I'm attempting to write a script that will set the Office XP username and user initials in the windows registry through Active Directory automatically when the user logs into the domain. I'm doing this using VBScript. Unfortunately I have found no easy way to convert to hexadecimal in order to put the registry key in the registry. Too bad Microsoft couldn't just put the values in the registry as string to be easier to manage.

Anyway, I start off by getting the username from login using the WScript.Network Username method - no problem. I then create the object for WScript.Shell for registry access. The problem I'm having is this: I convert the username from string to ascii code using this code...

' Create WSH objects
Set nw = CreateObject("WScript.Network")
Set WshShell = CreateObject("WScript.Shell")

'Get username
strUserName = nw.username

'Get length of username
max = len(strUserName)

'Start Processing username (Have to do it backwards because that's how it needs to go into the reg)
For intLoop = max to 1 step -1
'convert to ascii
asciiName = asc(mid(strUserName, intLoop, 1))
'convert to hex
hexName = hex(asciiName)
'concatenate hex string
newUserName = newUserName & hexName
Next

msgbox newUserName

What I do after this - I have no clue. How do I get the newUserName variable back as an integer? The registry writes I have no problem with - it's just the conversion back to integer that has me stuck.

I know in VB the function Val() will convert a string to numbers - barring any non-numerical characters other than &H or &O signifying Hexadecimal and Octal numbering systems, but I guess val() doesn't work with VBScript. Is this due to a limitation in VBScript or is this an oversight on my part? Any suggestions on how I could make this happen would be greatly appreciated.

Regards,

James Rogers
Systems Engineer
University of Notre Dame
MCSE MCSA MCDBA
 
Code:
newUserName = CInt(newUserName)

I think that should do it. Notorious P.I.G.
 
Thanks for the reply.

I already tried that actually. if I concatenate the string to lead it with a &H for the hexadecimal value then convert it to integer it takes the whole string literally as one number giving me a different value. If I try to convert it back to hex I get an even shorter hexadecimal number than I started out with. Maxes sense since each character is a 2 byte group. Like 6A in hex would be the letter 'j' in character representation.

in order to get it to work properly, I would have to find some way of getting the &H<hex string> to show up in my RegWrite command without supplying a variable. Since I don't have the use of the val() function I think I may be at an impass.

Why isn't there a CHex function that converts from string to hexadecimal representation? I guess there's really not much use for something like that outside of the Windows Registry.

Any more ideas?

--James
 
Hey everyone,

I found out that the WScript.Shell RegWrite method will only write a single DWORD value to a type of REG_BINARY. This would only allow a 4 character string of text in hexadecimal form, and I need twice that as our usernames on the domain are 8 characters in length. Looks like I just wasted a lot of time.

oh well...if nothing else - I learned something new :eek:)

--James
 
Not sure if this helps, but is this what you want to do?

hexNo = &quot;4A696D&quot; 'Value read from registry assigned to string
for x =1 to len(hexno) step 2
partNo = mid(hexno,x,2) 'chop up string
strName = strName & chr(&quot;&h&quot; & PartNo)
next
msgbox strName
msgbox clng(&quot;&h&quot; & hexno)
 
Unfortunately it doesn't matter what I try to do to format the hex string or convert it - the RegWrite method won't take any higher than a DWORD value.

Example:

set WshShell = CreateObject(&quot;WScript.Shell&quot;)

WshShell.RegWrite &quot;HKCU\Software\Microsoft\Office\10.0\Common\UserInfo\UserName&quot;, &H6a726a726a72 &quot;REG_BINARY&quot;

If the type is set as REG_BINARY it can only accept an integer of 8 bytes in length because the REG_MULTI_SZ (Variable Length) is not writable with this method.

If the hex number was &H6a726a72 then it would work because it will fit in a DWORD value. The only thing I know of that would be able to do it is WIN32 API. Thanks for the help.

--James
 
I know this is a bit messy but you could write the info into a reg file and then use wshShell.run to import the reg file,

A copy of my registry setting is shown below, all you would have to do is amend the string &quot;UserName&quot;.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Office\9.0\Common\UserInfo]
&quot;UserName&quot;=hex:46,00,72,00,69,00,64,00,61,00,79,00,53,00,00,00

Regards
Steve Friday
 
try this script

I have hard coded my name in strUserName
My Office version is 9.0 not 10 - -stroffpath

Ensure you backup your registry before testing - as no gaurantees are given!! - But it works on my machine


Dim refRegistry
Dim strMoniker

'set symbolic constant
Const HKEY_CURRENT_USER = &H80000001


'prepare variables for writing value
stroffPath = &quot;Software\Microsoft\Office\9.0\Common\UserInfo&quot;
strMachineName = &quot;.&quot;
strUserName = &quot;FridayS&quot;
max = len(strUserName)
For intLoop = 1 to max step 1
'convert to ascii
asciiName = asc(mid(strUserName, intLoop, 1))
if newnameasc = &quot;&quot; Then
newnameasc = asciiname
else
newnameasc = newnameasc & &quot;,&quot; & &quot;00&quot; & &quot;,&quot; & asciiname
end If
Next
updatename = Split(newNameasc,&quot;,&quot;)


'attempt to retrieve remote StdRegProv
strMoniker = &quot;winMgmts:\\&quot; & strMachineName & &quot;\root\default:StdRegProv&quot;
Set refRegistry = GetObject(strMoniker)
If Err<>0 Then
WScript.Echo &quot;Unable to locate registry - on &quot; & strMachineName
WScript.Quit
End If


If refRegistry.SetBinaryValue(HKEY_CURRENT_USER, stroffPath , &quot;UserName&quot; ,updatename) = 0 Then

WScript.Echo &quot;name change on&quot; & strMachineName
Else
WScript.Echo &quot;Unable to set value &quot; & strMachineName
End If


Set refRegistry = Nothing Regards
Steve Friday
 
I am currently working on the WMI alternative that doesn't have the limitations that WSH does. I'm running into a problem with SetBinaryValue() though.

It seems as though SetBinaryValue() doesn't allow you to use a dynamic array to populate data to be pushed into the registry. If I do - I get this error:

Error: Invalid parameter
Code: 80041008
Source: SWbemObjectEx

When I use a static array (commented out below) it works fine.

see code:

Const HKEY_CURRENT_USER = &H80000001
set nw = CreateObject(&quot;WScript.Network&quot;)
strComputerName = nw.ComputerName
set oReg = GetObject(&quot;winmgmts:root\default:StdRegProv&quot;)
strUserName = nw.UserName
max = len(strUserName)
ReDim binaryArray(max)
For intLoop = max to 1 step -1
userName = asc(mid(strUserName,intLoop,1))
binaryArray(intLoop) = userName
Next

'testArray = Array(&quot;255&quot;, &quot;180&quot;, &quot;3&quot;, &quot;4&quot;)
officePath = &quot;Software\Microsoft\Office\10.0\Common\UserInfo\&quot;
userNameValue = oReg.SetBinaryValue(HKEY_CURRENT_USER, officePath, &quot;UserName&quot;, binaryArray)

I feel like i'm so close to getting it, but I'm starting to get discouraged.

--James
 
Once again, changed Office to 9.0 as I don't have 10.0



Const HKEY_CURRENT_USER = &H80000001
set nw = CreateObject(&quot;WScript.Network&quot;)
strComputerName = nw.ComputerName
set oReg = GetObject(&quot;winmgmts:root\default:StdRegProv&quot;)
strUserName = nw.UserName
max = len(strUserName)
'ReDim binaryArray(max)
msgbox strUserName
For intLoop = 1 to Max step 1
asciiName = asc(mid(strUserName, intLoop, 1))
if newnameasc = &quot;&quot; Then
newnameasc = asciiname
else
newnameasc = newnameasc & &quot;,&quot; & asciiname
end If
Next
binaryArray = Split(newNameasc,&quot;,&quot;)


'testArray = Array(&quot;255&quot;, &quot;180&quot;, &quot;3&quot;, &quot;4&quot;)
officePath = &quot;Software\Microsoft\Office\9.0\Common\UserInfo\&quot;
userNameValue = oReg.SetBinaryValue(HKEY_CURRENT_USER, officePath, &quot;UserName&quot;, binaryArray)
Regards
Steve Friday
 
Sorry about that Steve - I wasn't paying attention.

Your code worked for me as well and I greatly appreciate it, thanks a bunch!

here's the finished product:
Code:
Set nw = CreateObject(&quot;WScript.Network&quot;)

Const HKEY_CURRENT_USER = &H80000001

strOfficePath = &quot;Software\Microsoft\Office\10.0\Common\UserInfo&quot;
strMachineName = &quot;.&quot;
strUserName = nw.UserName
max = len(strUserName)
For intLoop = 1 to max step 1
	asciiName = asc(mid(strUserName, intLoop, 1))
	if newAsciiName = &quot;&quot;  and newAsciiInitials = &quot;&quot; Then
		newAsciiName = asciiName
		'newAsciiInitials = asciiName
	else
		newAsciiName = newAsciiName & &quot;,&quot; & &quot;00&quot; & &quot;,&quot; & asciiName
	end if
	If intLoop <= 2 then
		newAsciiInitials = newAsciiName & &quot;,&quot; & &quot;00&quot;
	End If
Next
updateName = Split(newAsciiName,&quot;,&quot;)
updateInitials = Split(newAsciiInitials,&quot;,&quot;)
strMoniker = &quot;winMgmts:\\&quot; & strMachineName & &quot;\root\default:StdRegProv&quot;
Set oReg = GetObject(strMoniker)
userNameSet = oReg.SetBinaryValue(HKEY_CURRENT_USER, strOfficePath, &quot;UserName&quot;, updateName)
userInitialsSet = oReg.SetBinaryValue(HKEY_CURRENT_USER, strOfficePath, &quot;UserInitials&quot;, updateInitials)

One quick question for ya Steve. Why can't you use a dynamic array for the data? It doesn't make much sense to script something that you have to manually input ascii characters into. Just my 2 cents.

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top