Solved

Write Binary Value on vbscript

Posted on 2014-09-24
28
424 Views
Last Modified: 2014-11-09
Hi
i need to write vb script that enter value in binary but i got error
(13, 1) Microsoft VBScript runtime error: Type mismatch

is there any way to to fix this problem


Set wshShell = CreateObject( "WScript.Shell" )
Set objNetwork = CreateObject("Wscript.Network")
strComputer = LCase (objNetwork.ComputerName)
strComputer1 = objNetwork.ComputerName

wshShell.RegWrite "HKCU\New\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" &  "\",""

wshShell.RegWrite "HKCU\New\" & strComputer1 & "\Test\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" &  "\Test\",""

wshShell.RegWrite "HKCU\New\" & strComputer1 & "\Test" & "\Password", strComputer , "REG_BINARY"
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" & "\Test" & "\Password", strComputer & "_For" , "REG_BINARY"


Set wshShell = Nothing
0
Comment
Question by:David Regev
  • 11
  • 8
  • 3
28 Comments
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 40343412
Hi David,

AFAIK you have to write it in Hex value:

Example for wb:WshShell.Regwrite "HKCU\New\Computer\MyKey", &H00123456, "REG_BINARY"

I assume this would be a stringvariable if you like to make it more flexible
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40343719
If you need a binary array in the VBScript environment, you will need to use ADODB.Stream

I have stored 'binary' data in registries as a string of hex characters.
I have seen other instances where binary data was Base64 encoded and the encoded string was stored.
0
 

Author Comment

by:David Regev
ID: 40345461
Hi aikimark

How do i use ADODB.Stream in my VB Script ?

Thx

David
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40345836
In my RTF conversion article (
http:A_14035-RichText-to-Text-conversion-fast-and-free.html ), I include this sample code for the VBScript developers.
Function StringToByteArray(parmString)
	Dim OStream
	Set oStream = CreateObject("ADODB.Stream")
	oStream.Open
	oStream.Type = 1		'=adTypeBinary
	oStream.WriteText parmString
	StringToByteArray = oStream.Read
End Function

Open in new window


MS ADODB.Stream reference:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms675032(v=vs.85).aspx
0
 

Author Comment

by:David Regev
ID: 40346220
I'm sorry , I'm new in this this issue i don't under stand how can i mix the vb script  with this function

i want to use the computer name ( strComputer ) on the password section or the computer name ( strComputer & "_For"  ) also in password section with binary value

but istill got this error message

Thx
Set wshShell = CreateObject( "WScript.Shell" )
Set objNetwork = CreateObject("Wscript.Network")
strComputer = LCase (objNetwork.ComputerName)
strComputer1 = objNetwork.ComputerName

wshShell.RegWrite "HKCU\New\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" &  "\",""

wshShell.RegWrite "HKCU\New\" & strComputer1 & "\Test\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" &  "\Test\",""

wshShell.RegWrite "HKCU\New\" & strComputer1 & "\Test" & "\Password", strComputer , "REG_BINARY"
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" & "\Test" & "\Password", strComputer & "_For" , "REG_BINARY"


Set wshShell = Nothing

Function StringToByteArray(parmString)
      Dim OStream
      Set OStream = CreateObject("ADODB.Stream")
      oStream.Open
      oStream.Type = 1            '=adTypeBinary
      oStream.WriteText parmString
      StringToByteArray = oStream.Read
End Function
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40346275
Try this:
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" & "\Test" & "\Password", StringToByteArray(strComputer & "_For") , "REG_BINARY"

Open in new window

0
 

Author Comment

by:David Regev
ID: 40346829
Hi aikimark

now i got this error -  ADODB.Stream: Operation is not allowed in this context.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40346849
What statement and what was the error number?
0
 

Author Comment

by:David Regev
ID: 40346903
Hi

thx for the response the error - Untitled.vbs(27, 2) ADODB.Stream: Operation is not allowed in this context.

Set wshShell = CreateObject( "WScript.Shell" )
Set objNetwork = CreateObject("Wscript.Network")
strComputer = LCase (objNetwork.ComputerName)
strComputer1 = objNetwork.ComputerName

wshShell.RegWrite "HKCU\New\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" &  "\",""

wshShell.RegWrite "HKCU\New\" & strComputer1 & "\Test\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" &  "\Test\",""

wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" & "\Test" & "\Password", StringToByteArray(strComputer & "_For") , "REG_BINARY"
'wshShell.RegWrite "HKCU\New\" & strComputer1 & "\Test" & "\Password", strComputer , "REG_BINARY"
'wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" & "\Test" & "\Password", strComputer & "_For" , "REG_BINARY"



Set wshShell = Nothing

Function StringToByteArray(parmString)
	Dim OStream
	Set OStream = CreateObject("ADODB.Stream")
	oStream.Open
	oStream.Type = 1		'=adTypeBinary
	oStream.WriteText parmString
	StringToByteArray = oStream.Read
End Function

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40347198
Is the computer name more than four characters?

Although the error occurs in the function, maybe it should be viewed in the context of the RegWrite method.  According to MS, RegWrite has an 8 byte limit.

RegWrite will write at most one DWORD to a REG_BINARY value. Larger values are not supported with this method.
0
 

Author Comment

by:David Regev
ID: 40347263
Hi aikimark
the computer name is more then 4 characters so i try to use other method that i find and still it doesn't work
 Const OverwriteIfExist = -1 
 Const FailIfExist      = 0 
 Const OpenAsASCII   =  0 
 Const OpenAsUnicode = -1 
 Const OpenAsDefault    = -2 


Set wshShell = CreateObject( "WScript.Shell" )
Set objNetwork = CreateObject("Wscript.Network")
Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set oShell = CreateObject("Wscript.Shell") 
sTmpFile = oShell.ExpandEnvironmentStrings("%TEMP%") & "\Test.reg" 
Set fFile = oFSO.CreateTextFile(sTmpFile, OverwriteIfExist, OpenAsASCII) 
                                     
strComputer = LCase (objNetwork.ComputerName)
strComputer1 = objNetwork.ComputerName

wshShell.RegWrite "HKCU\New\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" &  "\",""

wshShell.RegWrite "HKCU\New\" & strComputer1 & "\Test\",""
wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" &  "\Test\",""

'wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" & "\Test" & "\Password", StringToByteArray(strComputer & "_For"), "REG_BINARY"
'wshShell.RegWrite "HKCU\New\" & strComputer1 & "\Test" & "\Password", strComputer , "REG_BINARY"
'wshShell.RegWrite "HKCU\New\" & strComputer1 & "_For" & "\Test" & "\Password", strComputer & "_For" , "REG_BINARY"

 fFile.WriteLine "Windows Registry Editor Version 5.00" 
 fFile.WriteLine 
 fFile.WriteLine "[HKEY_CURRENT_USER\New\DUDURE_For\Test]"
 fFile.WriteLine """Password""=hex:" & strComputer 
 fFile.Close 
 ' Import the registry file 
 oShell.Run "regedit /s " & sTmpFile, 0, True 
oFSO.DeleteFile sTmpFile 

Set wshShell = Nothing

Function StringToByteArray(parmString)
	Dim OStream
	Set OStream = CreateObject("ADODB.Stream")
	oStream.Open
	oStream.Type = 1		'=adTypeBinary
	oStream.WriteText parmString
	StringToByteArray = oStream.Read
End Function

Open in new window

can you try this and see what wrong please and how should i fix it ?

David
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:aikimark
ID: 40347488
Do you need to know how to convert a plain text string into a hex (string) encoding?
0
 

Author Comment

by:David Regev
ID: 40347546
I need to use something that will work with my script
0
 

Author Comment

by:David Regev
ID: 40347562
Hi
What the script need to do is
1 . Create the registry key
2 . Create password registry key with binary value and the binary value will be the computer name
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40348863
please answer the question that I asked.
0
 

Author Comment

by:David Regev
ID: 40404335
this is not  a solution to your problem
0
 

Author Comment

by:David Regev
ID: 40417950
can you close this question  ?
0
 

Author Comment

by:David Regev
ID: 40417953
I've requested that this question be deleted for the following reason:

test
0
 

Author Comment

by:David Regev
ID: 40418048
I've requested that this question be deleted for the following reason:

i didnt got the write answer for this post please delete or close this case
0
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 40417959
aikimark give the right answer at least for 99%
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40421299
According to Murphey2, I've answered 99% of the question, but I can't tell which of my comments should be accepted.  With the lack of participation and English skills of the OP, I think this question is abandoned/lost.
0
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 40422257
ID: 40345836
Was a good solution, if David can't implement it, it will not make the solution wrong.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 113
Hibernate on windows 10 18 118
recursion example 16 111
Please help| Getting the syntax error in below query. 2 42
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now