Board logo

标题: [讨论] .NET & Script [打印本页]

作者: electronixtar     时间: 2006-9-25 09:03    标题: [讨论] .NET & Script

废话不多说,bat cmd wmi wmic vbs js WSH其实就几个命令解释引擎程序而已:cmd.exe 和 cscript.exe ,而微软早已在系统中内置了另一个无比强大的“脚本”引擎—— .NET,前提是你安装了 .NET Framework(很少有人不安装这个的)。安装了之后,会有一个 vbc.exe csc.exe 在 C:\windows\Microsoft.NET\Framework\v1.1.4322\csc.exe 或者 C:\windows\Microsoft.NET\Framework\v2.0.50727\csc.exe ,Visual Studio编译就是用这两个文件来编译的。所以呢,我们可以在批处理/脚本里写.NET代码,然后调用vbc.exe(VB.NET的编译器)或csc.exe(C#的编译器)来实现超越bat或wsh的功能,例如GUI/多线程等。国内研究这个的批处理/脚本写手比较少,国外的暴多。今天发一个例子提提兴趣。没有勇气的不要看代码,看了代码的要回帖顶(-_-!!)

dotNetWrapper,干什么的呢?把你的vbs/js/hta等编译成exe,可以独立运行(只要安装了.NET Framework)的东东,采用vbs编写,调用VB.NET编译。

http://www.windowsitpro.com/WindowsScripting/Article/ArticleID/50569/50569.html

  Quote:
使用方法:dotNetWrapper.vbs 源脚本文件 语言 引擎exe 输出路径 [图标]


dotNetWrapper [Script File] [Script Type] [Script Engine] [Output File] [opt - Icon File]
"Script File (ex: C:\Scripts\myscript.vbs)
"Script Type (ex: .vbs)
"Script Engine (ex: cscript.exe)
"Output File (ex: C:\Programs\myscript.exe)
"Icon File --optional (ex. C:\Icons\mypicture.ico)"

DotNetWrapper.vbs
CODE:  [Copy to clipboard]
Option Explicit
On error resume next

Dim dotNetVersion, ScriptFile, ScriptType, LaunchWith, EXEFile, icofile
Dim fso, WshShell, sysroot, ScriptSource, ScriptContent, workingdir, vbfile, vbsource
Dim vbcPath, vbcArgs, strCMDLine, debugger, objArgs

dotNetVersion = "v1.1.4322"

Set objArgs = WScript.Arguments
If objArgs.Count < 4 Then
     WScript.Echo "Missing one or more arguments..." & vbCrLf & _
          "Correct Usage: dotNetWrapper [Script File] [Script Type] [Script Engine] [Output File] [opt - Icon File]" & vbcrlf & vbcrlf & _
          "Script File (ex: C:\Scripts\myscript.vbs)" & vbcrlf & _
          "Script Type (ex: .vbs)" & vbcrlf & _
          "Script Engine (ex: cscript.exe)" & vbcrlf & _
          "Output File (ex: C:\Programs\myscript.exe)" & vbcrlf & _
          "Icon File --optional (ex. C:\Icons\mypicture.ico)"
     WScript.Quit
End If
ScriptFile = objArgs(0)
ScriptType = objArgs(1)
LaunchWith = objArgs(2)
EXEFile = objArgs(3)
If objArgs(4) <> "" Then
     icofile = objArgs(4)
End If

Set fso = CreateObject ("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")

Set sysroot = fso.GetSpecialFolder(0)
If Not(fso.FileExists("C:\" & sysroot.name & "\Microsoft.Net\Framework\" & dotNetVersion & "\vbc.exe")) Then
     MsgBox "Unable to locate vbc.exe compiler. Confirm your version of .NET", 16, "ERROR"
     WScript.Quit
End If

Set ScriptSource = fso.OpenTextFile(ScriptFile, 1)
ScriptContent = ""
Do While Not ScriptSource.AtEndOfStream
          ScriptContent = ScriptContent & CHR(34) & Replace(EncodeScript(ScriptSource.readline), CHR(34), CHR(34) & " & CHR(34) & " & CHR(34)) & CHR(34) & " & vbcrlf & _" & vbcrlf
Loop
ScriptContent = ScriptContent & CHR(34) & CHR(34)
ScriptContent = "ScriptContent = " & CHR(34) & CHR(34) & " & _" & vbcrlf & ScriptContent

Set workingdir = fso.GetFile(ScriptFile)
         
vbfile = "Module Module1" & vbcrlf & _
     "Sub Main()" & vbcrlf & _
     "On Error Resume Next" & vbcrlf & _
     "Dim sPath As String" & vbcrlf & _
     "sPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)" & vbcrlf & _
     "Dim ScriptContent As String" & vbcrlf & _
     ScriptContent & vbcrlf & _
     "Dim oFile As System.IO.File" & vbcrlf & _
     "Dim oWrite As System.IO.StreamWriter" & vbcrlf & _
     "oWrite = oFile.CreateText(sPath & ""\compiledScript" & ScriptType & """)" & vbcrlf & _
     "If Err.Number <> 0 Then" & vbcrlf & _
     "MsgBox(""Unable to open Program. Please make sure you are running this locally."", MsgBoxStyle.Critical, ""Error"")" & vbcrlf & _
     "Exit Sub" & vbcrlf & _
     "End If" & vbcrlf & _
     "oWrite.WriteLine(EncodeScript(ScriptContent))" & vbcrlf & _
     "oWrite.Flush()" & vbcrlf & _
     "oWrite.Close()" & vbcrlf & _
     "System.Threading.Thread.Sleep(1000)" & vbcrlf & _
     "Shell(""" & LaunchWith & " "" & Chr(34) & sPath & ""\compiledScript" & ScriptType & """ & Chr(34), AppWinStyle.NormalFocus, False)" & vbcrlf & _
     "System.Threading.Thread.Sleep(1000)" & vbcrlf & _
     "oFile.Delete(sPath & ""\compiledScript" & ScriptType & """)" & vbcrlf & _
     "End Sub" & vbcrlf & _
     "Function EncodeScript(ByVal stringinfo As String)" & vbcrlf & _
     "Dim i As Int16" & vbcrlf & _
     "Dim newstr As String" & vbcrlf & _
     "Dim curchar As Int16" & vbcrlf & _
     "For i = 1 to len(stringinfo)" & vbcrlf & _
     "curchar = asc(mid(stringinfo,i,1))" & vbcrlf & _
     "If (curchar >= 66 and curchar <= 122) or (curchar >=194 and curchar <= 250) then" & vbcrlf & _
     "If curchar >= 66 and curchar <= 122 Then" & vbcrlf & _
     "newstr = newstr & chr(curchar+128)" & vbcrlf & _
     "else " & vbcrlf & _
     "newstr = newstr & chr(curchar-128)" & vbcrlf & _
     "End If" & vbcrlf & _
     "Else" & vbcrlf & _
     "newstr = newstr & chr(curchar)" & vbcrlf & _
     "end if" & vbcrlf & _
     "next" & vbcrlf & _
     "EncodeScript = newstr" & vbcrlf & _
     "End Function" & vbcrlf & _
     "End Module" & vbcrlf

     Set vbsource = fso.OpenTextFile(workingdir.ParentFolder & "\compiledscript.vb", 2, True)
     vbsource.Write vbfile
     Set vbsource = Nothing

     vbcPath = "C:\" & sysroot.name & "\Microsoft.NET\Framework\" & dotNetVersion & "\vbc.exe"
     vbcArgs = " /out:" & CHR(34) & exefile & CHR(34) & _
     " /nowarn /nologo /quiet /debug- /optimize+ /optionstrict- /optionexplicit- " & _
     "/imports:Microsoft.VisualBasic,System /t:winexe " & _
     CHR(34) & workingdir.ParentFolder & "\compiledscript.vb" & CHR(34) & " > " & _
     CHR(34) & workingdir.ParentFolder & "\debug.txt" & CHR(34)
     If icofile <> "" Then
          vbcargs = " /win32icon:" & CHR(34) & icofile.Value & CHR(34) & vbcArgs & CHR(34)
     End If

     strCMDLine = vbcPath & vbcArgs
     debugger = WshShell.Run("cmd /c " & strCmdLine, 1, True)
     If debugger <> 0 Then
          WshShell.Run CHR(34) & workingdir.ParentFolder & "\debug.txt" & CHR(34), 1, True
     Else
          MsgBox ".EXE Created Successfully!", 64, "Complete"
     End If

     fso.DeleteFile workingdir.ParentFolder & "\debug.txt"
     fso.DeleteFile workingdir.ParentFolder & "\compiledscript.vb"


Function EncodeScript(stringinfo)
     Dim i, curchar, newstr
     for i = 1 to len(stringinfo)
          curchar = asc(mid(stringinfo,i,1))
          If (curchar >= 66 and curchar <= 122) or (curchar >=194 and curchar <= 250) then
               If curchar >= 66 and curchar <= 122 Then
                    newstr = newstr & chr(curchar+128)
               else
                    newstr = newstr & chr(curchar-128)
               End If
          Else
               newstr = newstr & chr(curchar)
          End If
     Next
     EncodeScript = newstr
End Function
[ Last edited by electronixtar on 2006-9-25 at 20:19 ]
附件 1: DotNetWrapper.rar (2006-9-25 09:03, 10.4 K, 下载附件所需积分 10 点 ,下载次数: 57)

作者: pengfei     时间: 2006-9-25 09:25
看不懂, 沙发先~~!
作者: vkill     时间: 2006-9-25 09:31
基础不好,看懂的很少
作者: electronixtar     时间: 2006-9-25 20:18
先汗一个~~
作者: namejm     时间: 2006-9-25 20:42
  引用某个牛人的话:"除了认识27个字母外,其他的我都不懂"^_^
作者: electronixtar     时间: 2006-9-25 21:36
再寒~~~算了,以后这种天书帖子还是少发~
作者: kcdsw     时间: 2006-9-25 21:58
看懂了好些了  看来是相当的有前途啊
作者: electronixtar     时间: 2006-9-25 22:02
安慰了~~~kcdsw再多多看~~多多顶贴~~哈哈~
作者: piziliu2004     时间: 2006-9-25 23:36
有沒有.vbc.exe(VB.NET的编译器)或csc.exe(C#的编译器)的幫助文档,更多范例呢?
作者: electronixtar     时间: 2006-9-26 00:21
更多的范例就是100MB的大东东: .NET Framework SDK

http://www.microsoft.com/downloa ... p;DisplayLang=zh-cn




直接下载(10个包)

http://download.microsoft.com/do ... 8c8f5/SDKSetup1.exe  
http://download.microsoft.com/do ... c8f5/SDKSetup10.cab  
http://download.microsoft.com/do ... 8c8f5/SDKSetup2.cab  
http://download.microsoft.com/do ... 8c8f5/SDKSetup3.cab  
http://download.microsoft.com/do ... 8c8f5/SDKSetup4.cab  
http://download.microsoft.com/do ... 8c8f5/SDKSetup5.cab  
http://download.microsoft.com/do ... 8c8f5/SDKSetup6.cab  
http://download.microsoft.com/do ... 8c8f5/SDKSetup7.cab  
http://download.microsoft.com/do ... 8c8f5/SDKSetup8.cab
http://download.microsoft.com/do ... 8c8f5/SDKSetup9.cab


当然了,如果你安装了 Visual Studio .NET 2003 或者 Visual Studio 2005,或者 MSDN Library 2003Feb 或更高,就不用下载了,自带。

[ Last edited by electronixtar on 2006-9-26 at 00:27 ]
作者: yywd     时间: 2006-9-27 22:01
看了代码的,回帖顶
作者: fastslz     时间: 2006-10-17 07:20
哈哈~看天书了~

[ Last edited by fastslz on 2006-10-17 at 08:20 ]
作者: lxmxn     时间: 2006-10-17 11:46

  哈哈,我也是天书一个啊~

  看不懂,不过我要学着看~

  顶一个先~

  希望楼主多发点其它脚本方面的文章来看看。

作者: h2o     时间: 2007-11-4 00:38
好像编译bat会出错 有哪位朋友试过了吗?
编译完出来的exe不能用.(图标也不对)

我的环境,
.net v2.0.50727
vista
作者: sinbao     时间: 2007-11-22 22:07
楼主,其实你把原理简单的说一下就好了,贴了一大堆代码,除了唬人,意义似乎并不大...

---
我们可以在批处理/脚本里写.NET代码,然后调用vbc.exe(VB.NET的编译器)或csc.exe(C#的编译器)来实现超越bat或wsh的功能
---
上面这句话还能给我们点提示.
作者: 258692011     时间: 2008-2-19 01:18
看不懂
作者: muou     时间: 2009-12-23 12:12
除了认识27个字母外,其他的我都不懂"^_^
作者: ZJHJ     时间: 2009-12-23 12:36
这样讲不懂啊,发点简单实用的代码例子........才是正道
作者: sohu     时间: 2010-12-4 16:56
看不,
想学东西还真难。。。
作者: 624378168     时间: 2011-1-13 23:09
看天书了。。




欢迎光临 中国DOS联盟论坛 (http://cndos.fam.cx/forum/) Powered by Discuz! 2.5