Click here to Skip to main content
16,020,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a vbs script that works out a checksum, I need to use this checksum in a c# application.

I can echo the checksum using
WScript.Echo(LeftChar & RightChar)

To run this script for test in cmd prompt I am using :- cscript calcWaferIDChkSum1.3.vbs /wafer:178I102120

Where the script accepts the string argument to calculate its checksum

SMultiply=0
dim TMultiply, AMod, LBin, RBin, Bin, LeftChar, RightChar, Idx, CValue
MMod=0

Function DecimalToBinary(DecimalNum)
  
     Dim strBinary, lngNumber1, lngNumber2, strDigit 
     strBinary = "" 
     intDecimal = cInt(DecimalNum) 


	 While (intDecimal > 1) 
         lngNumber1 = intDecimal / 2 
         lngNumber2 = Fix(lngNumber1) 

        If (lngNumber1 > lngNumber2) Then 
             strDigit = "1" 
        Else 
             strDigit = "0" 
        End If 

        strBinary = strDigit & strBinary 
        intDecimal = Fix(intDecimal / 2)
		
     Wend 

     strBinary = "1" & strBinary 

  DecimalToBinary = strBinary
End Function

Function BinaryToDecimal(Binary)
  Dim n
  Dim s
  
  For s = 1 To Len(Binary)
    n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ (s - 1)))
  Next
  
  BinaryToDecimal = n

End Function

Function IsInvalidChar(IStr)
	dim flag
	
	flag = "F"
	
	For j=1 To Len(IStr)
		if Not(Asc(Mid(IStr,j,1))=45 OR Asc(Mid(IStr,j,1))=46 OR ( Asc(Mid(IStr,j,1)) >= 48 AND  Asc(Mid(IStr,j,1)) <= 57 ) OR ( Asc(Mid(IStr,j,1)) >= 65 AND  Asc(Mid(IStr,j,1)) <= 90 )) then
			flag = "T"
			exit for
		end if
	Next
	
	IsInvalidChar = flag
End Function

Set colArgs = WScript.Arguments.Named
'inputStr = trim(UCase(InputBox("Enter Wafer ID")))
inputStr = colArgs.Item("wafer")

'While (IsInvalidChar(inputStr) = "T")
	'inputStr = trim(UCase(InputBox("Invalid character, Enter Wafer ID:")))
'Wend  

if len(inputStr) <> 0 then
	PreValue = inputStr & "A0" 
	For i=1 To Len(PreValue)

		CValue = Asc(Mid(PreValue,i,1)) - 32
		Multiply = CValue * (8^(Len(PreValue)-i))
		TMultiply = CCur(TMultiply + Multiply)
		
	Next

	AMod = TMultiply - (59 * Int(TMultiply / 59))
	MMod = 59 - AMod
	Bin = DecimalToBinary(MMod)

	IF Len(Bin) <= 3 then
		LBin = 0
		RBin = Bin
	Else
		LBin = Mid(Bin,1,len(Bin)-3)
		RBin = Right(Bin, 3)
	End If

	LeftChar = Chr(BinaryToDecimal(LBin)+ 65)
	RightChar = BinaryToDecimal(RBin)

	'output = "CMD /C echo " & inputStr & LeftChar & RightChar & "| CLIP" 
	'Set WshShell = CreateObject("WScript.Shell")
	'WshShell.Run output, 0, true

	'MsgBox("Press Ctrl + v or right click and paste to get result - " & inputStr & LeftChar & RightChar)
	
	wafer = LeftChar & RightChar
	WScript.Echo(LeftChar & RightChar)
	
end if


What I have tried:

In c# I tried this:

string scriptName = @"calcWaferIDChkSumV3.vbs";
var process = new Process();
ProcessStartInfo ps = new ProcessStartInfo();

ps.FileName = "cscript.exe";
ps.Arguments = string.Format("\"{0}\" \"{1}\" ", scriptName, "/wafer:178I1021010");
ps.UseShellExecute = false;
string answer = "";
ps.RedirectStandardOutput = true;
ps.UseShellExecute = false;
process.StartInfo = ps;

process.Start();
process.BeginOutputReadLine();
process.WaitForExit();


But How can I obtain the checksum and use it in other functions in c# ?
Posted
Updated 8-Nov-17 4:34am
v3

1 solution

Well, the best method would be to scrap this code and rewrite the VBscript code in C#, thereby eliminating the dependency on VBScript.

If this is a vendor supplied script and you get updates to it, rewriting would be a pain in the ass every time they updated the script.

An alternative method would be to host the MS ScriptControl in your C# app and expose a C# class through COM, [ComVisible(true)], to the VBScript as an extension to the ScriptControl. You load and run the VBScript in the ScriptControl and pass data back and forth between your C# code and your VBScript code using the extension you write.

I tried it out a couple of years ago and it does work very nicely, but I don't have an example that would be short enough to post in the forums and don't have the time to write an entire article on the process.

This method is much more difficult to do but is extremely flexible, giving you the ability to add scripting support to your own applications. Though, to implement this you better know how to do research, know how to teach yourself new things, and definitely know how to experiment to learn how something works.
 
Share this answer
 
v2
Comments
datt265 8-Nov-17 11:41am    
Yes might be easier to write in c# but i am not used to vbs so it is difficult for me to convert. I tried online tools but they are not converting the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900