用JS在页面调用本地可执行文件的方法(ACTIVEX)

用JS在页面调用本地可执行文件的方法,发现主要是用ACTIVEX控件实现的,主要有shell.aplication,Scripting.FileSystemObject和wscript.shell三种方式,其中Scripting.FileSystemObject主要是实现文档型文件的读写操作,由于项目需要执行EXE文件,这里就只转了一段用shell.aplication实现的代码。

<SCRIPT type="text/javascript" LANGUAGE="JavaScript">
function executeCommands(inputparms)
{
// Instantiate the Shell object and invoke its execute method.

var oShell = new ActiveXObject("Shell.Application");

var commandtoRun = "C:\\WINDOWS\\Notepad.exe";
if (inputparms != "")
{
var commandParms = document.Form1.filename.value;
}

// Invoke the execute method.
oShell.ShellExecute(commandtoRun, commandParms, "", "open", "1");
}
</SCRIPT>


<input type="Button" name="Button1" value="Run Notepad.exe"
onClick="executeCommands()" />

<BR><BR>
<input type="Button" name="Button2" value="Run Notepad.exe with
Parameters" onClick="executeCommands(' + hasPARMS + ')" />

以上代码经测试能成功实现,但是需要客户端IE把ACTIVEX相关的安全设置都起用,否则会报没有权限的JS错误。

ShellExecute函数的用法

<src="win.js"type="text/javascript">

ShellExecute函数原型及参数含义如下:

functionShellExecute(hWnd:HWND;Operation,FileName,Parameters,Directory:PChar;ShowCmd:Integer):HINST;stdcall;

●hWnd:用于指定父窗口句柄。当函数调用过程出现错误时,它将作为Windows消息窗口的父窗口。例如,可以将其设置为应用程序主窗口句柄,即Application.Handle,也可以将其设置为桌面窗口句柄(用GetDesktopWindow函数获得)。

●Operation:用于指定要进行的操作。其中“open”操作表示执行由FileName参数指定的程序,或打开由FileName参数指定的文件或文件夹;“print”操作表示打印由FileName参数指定的文件;“explore”操作表示浏览由FileName参数指定的文件夹。当参数设为nil时,表示执行默认操作“open”。

●FileName:用于指定要打开的文件名、要执行的程序文件名或要浏览的文件夹名。

●Parameters:若FileName参数是一个可执行程序,则此参数指定命令行参数,否则此参数应为nil或PChar(0)。

●Directory:用于指定默认目录。

●ShowCmd:若FileName参数是一个可执行程序,则此参数指定程序窗口的初始显示方式,否则此参数应设置为0。

若ShellExecute函数调用成功,则返回值为被执行程序的实例句柄。若返回值小于32,则表示出现错误。

特殊用法

如果将FileName参数设置为“http:”协议格式,那么该函数将打开默认浏览器并链接到指定的URL地址。若用户机器中安装了多个浏览器,则该函数将根据Windows9x/NT注册表中http协议处理程序(ProtocolsHandler)的设置确定启动哪个浏览器。

格式一:http://网站域名。

 如:ShellExecute(handle,‘open’,http://;

www.neu.edu.cn’,nil,nil,SW_SHOWNORMAL);

格式二:http://网站域名/网页文件名。

如:ShellExecute(handle,‘open’,http://;

www.neu.edu.cn/default.htm’,nil,nil,

SW_SHOWNORMAL);

如果将FileName参数设置为“mailto:”协议格式,那么该函数将启动默认邮件客户程序,如MicrosoftOutlook(也包括MicrosoftOutlookExpress)或NetscapeMessanger。若用户机器中安装了多个邮件客户程序,则该函数将根据Windows9x/NT注册表中mailto协议处理程序的设置确定启动哪个邮件客户程序。

格式一:mailto:

如:ShellExecute(handle,‘open’,‘mailto:’,nil,nil,SW_SHOWNORMAL);打开新邮件窗口。

格式二:mailto:用户账号@邮件服务器地址

如:ShellExecute(handle,‘open’,‘mailto:who@mail.neu.edu.cn’,nil,nil,SW_SHOWNORMAL);打开新邮件窗口,并自动填入收件人地址。若指定多个收件人地址,则收件人地址之间必须用分号或逗号分隔开(下同)。

格式三:mailto:用户账号@邮件服务器地址?subject=邮件主题&body=邮件正文

如:ShellExecute(handle,‘open’,‘mailto:who@mail.neu.edu.cn?subject=Hello&Body=Thisisatest’,nil,nil,SW_SHOWNORMAL);打开新邮件窗口,并自动填入收件人地址、邮件主题和邮件正文。若邮件正文包括多行文本,则必须在每行文本之间加入换行转义字符%0a。

如何打开一个应用程序?

ShellExecute(this->m_hWnd,"open","calc.exe","","",SW_SHOW);或

ShellExecute(this->m_hWnd,"open","notepad.exe","c:\\MyLog.log","",SW_SHOW);正如您所看到的,我并没有传递程序的完整路径。

应用如下:

如何打开一个同系统程序相关连的文档?

ShellExecute(this->m_hWnd,"open","c:\\abc.txt","","",SW_SHOW);

如何打开一个网页?

ShellExecute(this->m_hWnd,"open","http://www.google.com","","",SW_SHOW);

如何激活相关程序,发送EMAIL?

ShellExecute(this->m_hWnd,"open","mailto:nishinapp@yahoo.com","","",SW_SHOW);

如何用系统打印机打印文档?

ShellExecute(this->m_hWnd,"print","c:\\abc.txt","","",SW_HIDE);

如何用系统查找功能来查找指定文件?

ShellExecute(m_hWnd,"find","d:\\nish",NULL,NULL,SW_SHOW);

如何启动一个程序,直到它运行结束?

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c:\\MyProgram.exe";  
ShExecInfo.lpParameters = ""; 
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL; 
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

或:

PROCESS_INFORMATION ProcessInfo; STARTUPINFO StartupInfo; //This is an [in] parameterZeroMemory(&StartupInfo, sizeof(StartupInfo));

StartupInfo.cb = sizeof StartupInfo ; //Only compulsory fieldif(CreateProcess("c:\\winnt\\notepad.exe", NULL,     NULL,NULL,FALSE,0,NULL,    NULL,&StartupInfo,&ProcessInfo)){     WaitForSingleObject(ProcessInfo.hProcess,INFINITE);   

 CloseHandle(ProcessInfo.hThread);   

 CloseHandle(ProcessInfo.hProcess);
}  else{   
 MessageBox("The process could not be started...");
}

如何显示文件或文件夹的属性?

SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL;ShExecInfo.lpVerb = "properties";
ShExecInfo.lpFile = "c:\\"; //can be a file as wellShExecInfo.lpParameters = "";
 ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;ShExecInfo.hInstApp = NULL; 
ShellExecuteEx(&ShExecInfo);

http://blog.sina.com.cn/s/blog_532eb75f0100ggwj.html

http://blog.csdn.net/primer_programer/archive/2007/12/26/1968171.aspx

相关推荐