WScript.Shell 객체를 이용하면 외부 프로그램을 실행할 수가 있다.
그런데 외부 프로그램을 실행후 콘솔(커맨드)창의 실행 결과까지도 받을수가 있다.
주로 ASP에서 사용한다.(잘 동작한다.)
기존 객체 실행 프로그램은 wshell.run을 사용하는데 여기서는 wshell.exec를 사용하는것이 다르다.
exec를 사용하면 WshExecObject를 돌려주는데, 이 객체의 readall을 사용하면 결과 화면을 모두 얻어올 수 있다.
'객체 생성
dim wshell
set wshell = server.createobject("wscript.shell")
response.write cmd&"<br>"
'객체 생성 실패시 에러
if wshell is nothing then
response.write "S201:WScript.Shell Initialization Failure"
end If
Set oExec = wshell.exec(cmd)
'끝날때 까지 기다림
Do While oExec.Status = 0
Loop
'출력 메세지를 화면 출력
output = oExec.stdout.readall
response.write output
'리턴 코드
ret = oExec.ExitCode
response.write "ret="&ret&"<br>"
'객체 소멸
set wshell=nothing
Showing posts with label ASP. Show all posts
Showing posts with label ASP. Show all posts
Thursday, September 11, 2014
ASP 외부프로그램 실행
ASP에서 외부 프로그램을 실행하는 방법은 COM+을 만들어서 함수를 호출하던가 아니면 exe 프로그램을 만들어서 프로세스를 생성하는 방법이 있다. 그러나 윈도우 2008이나 호스팅업체에서 COM+을 이용을 하지 못할때 exe를 만들어서 사용하면 원하는 기능을 사용할 수 있다.
다음과 같이 WScript.Shell이라는 COM을 사용하면 exe를 호출할 수 있다. 리턴값을 받기 위해서는 ret=wshell.run(cmd, 0, true)할 때 마지막 parameter를 true로 주어야 한다. 리턴이 올때까지 기다린다는 뜻이다.
만약 호출하는 exe가 윈도우 프로그램이거나(윈도우 메세지 루프를 가져서 프로그램이 끝나지 않음) 또는 파일을 쓰는 (FILE* 등)프로그램이면 리턴이 되지 않고 멈춰버린다.
'절대패스로 지정해야함
exe = "C:\testweb\test.exe"
cmd = exe + "arg1 arg2 ..."
'객체 생성
dim wshell
set wshell = server.createobject("wscript.shell")
response.write cmd&"<br>"
'객체 생성 실패시 에러
if wshell is nothing then
response.write "S201:WScript.Shell Initialization Failure"
end if
'외부 프로그램 실행후 리턴값 받아서 화면에 출력
ret=wshell.run(cmd, 0, true)
response.write ret
'객체 소멸
set wshell=nothing
다음과 같이 WScript.Shell이라는 COM을 사용하면 exe를 호출할 수 있다. 리턴값을 받기 위해서는 ret=wshell.run(cmd, 0, true)할 때 마지막 parameter를 true로 주어야 한다. 리턴이 올때까지 기다린다는 뜻이다.
만약 호출하는 exe가 윈도우 프로그램이거나(윈도우 메세지 루프를 가져서 프로그램이 끝나지 않음) 또는 파일을 쓰는 (FILE* 등)프로그램이면 리턴이 되지 않고 멈춰버린다.
'절대패스로 지정해야함
exe = "C:\testweb\test.exe"
cmd = exe + "arg1 arg2 ..."
'객체 생성
dim wshell
set wshell = server.createobject("wscript.shell")
response.write cmd&"<br>"
'객체 생성 실패시 에러
if wshell is nothing then
response.write "S201:WScript.Shell Initialization Failure"
end if
'외부 프로그램 실행후 리턴값 받아서 화면에 출력
ret=wshell.run(cmd, 0, true)
response.write ret
'객체 소멸
set wshell=nothing
Subscribe to:
Posts (Atom)