从VBS脚本获取值到C# [英] Get a value from a VBS script to C#

查看:159
本文介绍了从VBS脚本获取值到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个用于校验和的vbs脚本,我需要在ac#应用程序中使用此校验和。



我可以使用<$回显校验和p $ p> WScript.Echo(LeftChar& RightChar)



要在cmd提示符下运行此脚本进行测试我正在使用: - cscript calcWaferIDChkSum1.3.vbs / wafer :178I102120



脚本接受字符串参数来计算其校验和



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

函数DecimalToBinary(DecimalNum)

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


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

If(lngNumber1> lngNumber2)然后
strDigit =1
否则
strDigit =0
结束如果

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

Wend

strBinary =1& strBinary

DecimalToBinary = strBinary
结束函数

函数BinaryToDecimal(二进制)
Dim n
Dim s

对于s = 1到Len(二进制)
n = n +(Mid(二进制,Len(二进制) - s + 1,1)*(2 ^(s - 1)))
下一个

BinaryToDecimal = n

结束函数

函数IsInvalidChar(IStr)
dim标志

flag = F

对于j = 1到Len(IStr)
如果不是(Asc(Mid(IStr,j,1))= 45或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))然后
flag =T
退出
结束如果
下一个

IsInvalidChar = flag
结束函数

设置colArgs = WScript.Arguments.Named
'inputStr = trim(UCase(InputBox(输入晶圆ID) )))
inputStr = colArgs.Item(wafer)

'While(IsInvalidChar(inputStr)=T)
'inputStr = trim(UCase(InputBox( 无效的ch aracter,输入Wafer ID:)))
'如果len(inputStr)<>那么Wend

0然后
PreValue = inputStr& A0
对于i = 1到Len(PreValue)

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

下一个

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

如果Len(Bin)< = 3则
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
'设置WshShell = CreateObject(WScript.Shell)
'WshShell.Run输出,0,true

'MsgBox(按Ctrl + v或右键单击并粘贴以获得结果 - & inputStr& LeftChar& RightChar

wafer = LeftChar& RightChar
WScript.Echo(LeftChar& RightChar)

结束如果





我尝试了什么:



在c#中我试过这个:



 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();





但是如何获得校验和并将其用于c#中的其他函数?

解决方案

嗯,最好的方法是废弃此代码并在C#中重写VBscript代码,从而消除对VBScript的依赖。



如果这是供应商提供的脚本并且你得到它的更新,那么每次更新脚本时重写都会很麻烦。



另一种方法将在您的C#应用​​程序中托管MS ScriptControl并通过COM([ComVisible(true)]将C#类暴露给VBScript作为ScriptControl的扩展。您在ScriptControl中加载并运行VBScript,并使用您编写的扩展名在C#代码和VBScript代码之间来回传递数据。



我试了一下几年前,它确实工作得非常好,但我没有一个足够短的例子可以在论坛上发帖,也没有时间在整个过程中撰写整篇文章。



这种方法要做得更加困难,但非常灵活,使您能够为自己的应用程序添加脚本支持。但是,要实现这一点,你最好知道如何做研究,知道如何教自己新事物,并且肯定知道如何尝试学习有用的东西。


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# ?

解决方案

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.


这篇关于从VBS脚本获取值到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆