在经典 asp 上从 com interop dll 以编程方式运行 bat 文件时出现问题

发布于 2024-11-19 07:11:20 字数 422 浏览 3 评论 0原文

我面临着使用 system.diagnostics.process 对象运行 bat 文件的困境。我有在类库中运行bat 文件的代码,我将其编译为dll。我通过使用编译时注册 dll com 使其可互操作。我使用强密钥对其进行签名,导出类型库并使用 regasm 和 gacutil 命令将 dll 放入 GAC 中。然后,我在 dll 中创建了特定类的对象,该对象具有使用 vbscript 中的 server.createobject 方法执行 bat 文件的方法。然后我调用了bat执行的方法。方法被正常调用,但 cmd 提示符没有弹出,bat 文件也没有被执行。我检查了一下互操作 DLL 是否有问题,但该 DLL 在 VB6 代码中运行良好。有人可以帮我解决这个问题吗?我不确定 IIS 服务器上是否存在权限问题。或者无法通过 ASP 上的 vbscript 来执行 cmd 的 dll?

谢谢, 地理。

I am facing a predicament with running a bat file using the system.diagnostics.process object. I have the code for running a bat file in a class library which I compiled to a dll. I made the dll com interoperable by registering it using compile time. I signed it using strong key, exported the type library and put the dll into GAC using regasm and gacutil commands. I then created the object of the specific class in the dll that has the method for the bat file execution using the server.createobject method in vbscript. I then called the method for the bat execution. Method gets invoked alright but the cmd prompt is not popping up nor is the bat file being executed. I checked to see if its a problem with the interop dll but the dll worked fine with VB6 code. Can someone help me with this issue? I am not sure if its some permission issue on the IIS server. Or is cmd executions not possible via vbscript on ASP for dlls?

Thanks,
Geo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国际总奸 2024-11-26 07:11:20

显然,您可能需要运行 cmd,然后将输入注入其中:

// Get the full file path
string strFilePath = "c:\\temp\\test.bat";

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = "c:\\temp\\";

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;

// Write each line of the batch file to standard input
while(strm.Peek() != -1) {
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();

// Close the io Streams;
sIn.Close(); 
sOut.Close();

Apparently you may need to run cmd and then just inject input into it:

// Get the full file path
string strFilePath = "c:\\temp\\test.bat";

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = "c:\\temp\\";

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;

// Write each line of the batch file to standard input
while(strm.Peek() != -1) {
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();

// Close the io Streams;
sIn.Close(); 
sOut.Close();

http://codebetter.com/brendantompkins/2004/05/13/run-a-bat-file-from-asp-net/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文