Advertisement
Guest User

Untitled

a guest
Mar 31st, 2011
1,517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System;
  2.  
  3. var description = new scan_app;
  4. var param1 = null, param2 = null;
  5. var fso, f1, ts, ForReading, ForWriting, s, wsh, guid;
  6.  
  7.  
  8. function scan_app()
  9. {
  10.   var objArgs = Environment.GetCommandLineArgs();
  11. //  write_log(get_ini_value("config.ini", "logfile"), objArgs);
  12.   switch (objArgs.length) //Анализируем параметры командной строки
  13.   {
  14.     case 3:
  15.       param1 = objArgs(1);
  16.       param2 = objArgs(2);
  17.       break;
  18.     case 2:
  19.       if (objArgs(1) == "-reg") reg_app(objArgs(0));
  20.       else
  21.       if (objArgs(1) == "-unreg") unreg_app();
  22.       else write_log(get_ini_value("config.ini", "logfile"), "Usage:\nscan.exe -reg\nscan.exe -unreg\nscan.exe param1 param2");
  23.       return;
  24.     default:
  25.       write_log(get_ini_value("config.ini", "logfile"), "Usage:\nscan.exe -reg\nscan.exe -unreg\nscan.exe param1 param2");
  26.       return;
  27.   }
  28.  
  29.   write_log(get_ini_value("config.ini", "logfile"), "Parameters:\n" + objArgs);
  30.  
  31.   switch (param2) //Запускаем приложения при получении нужных параметров
  32.   {
  33.     case get_ini_value("config.ini", "scan_flag"):
  34.       wsh = new ActiveXObject("WScript.Shell");
  35.       wsh.Run(get_ini_value("config.ini", "scan_app"),6);
  36.       break;
  37.     case get_ini_value("config.ini", "print_flag"):
  38.       wsh = new ActiveXObject("WScript.Shell");
  39.       wsh.Run(get_ini_value("config.ini", "print_app"),6);
  40.       break;
  41.     case get_ini_value("config.ini", "mail_flag"):
  42.       wsh = new ActiveXObject("WScript.Shell");
  43.       wsh.Run(get_ini_value("config.ini", "mail_app"),6);
  44.       break;
  45.     default:
  46.       write_log(get_ini_value("config.ini", "logfile"), "No valid parameters\n" + objArgs);
  47.   }
  48.  
  49.   return;
  50. }
  51.  
  52.  
  53. function reg_app(scan_app_path) //Регистрируем приложение в реестре
  54. {
  55.   guid = "{FC36F676-4675-4CC9-B705-3AC86F456214}";
  56.  
  57.   //Регистрируем Handler
  58.   var Root = "HKEY_LOCAL_MACHINE";
  59.   var key = "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\explorer\\AutoplayHandlers\\Handlers\\WIA_" + guid + "\\";
  60.   var valname = "Action";
  61.   var valx = "AppScan";
  62.   var WshShell = new ActiveXObject("WScript.Shell");
  63.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  64.  
  65.   valname = "CLSID";
  66.   valx = "WIACLSID";
  67.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  68.  
  69.   valname = "DefaultIcon";
  70.   valx = "sti.dll,0";
  71.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  72.  
  73.   valname = "InitCmdline";
  74.   valx = "/WiaCmd;" + scan_app_path + " /StiDevice:%1 /StiEvent:%2;";
  75.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  76.  
  77.   valname = "Provider";
  78.   valx = "AppScan";
  79.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  80.  
  81.   //Регистрируем приложение
  82.   key = "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\StillImage\\Registered Applications\\";
  83.   valname = "AppScan";
  84.   valx = scan_app_path + " /StiDevice:%1 /StiEvent:%2;";
  85.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  86.  
  87.   //Регистрируем обработчик событий от сканера
  88.   key = "\\SYSTEM\\CurrentControlSet\\Control\\StillImage\\Events\\STIProxyEvent\\" + guid + "\\";
  89.   valname = "Cmdline";
  90.   valx = scan_app_path + " /StiDevice:%1 /StiEvent:%2;";
  91.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  92.  
  93.   valname = "Desc";
  94.   valx = "Direct scan";
  95.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  96.  
  97.   valname = "Icon";
  98.   valx = "sti.dll,0";
  99.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  100.  
  101.   valname = "Name";
  102.   valx = "ScanApp";
  103.   WshShell.RegWrite(Root + key + valname, valx, "REG_SZ");
  104.  
  105.   write_log(get_ini_value("config.ini", "logfile"), "Application registered. Press scan button, choose ScanApp in menu to get button event id in log file.");
  106.  
  107.   return;
  108. }
  109.  
  110.  
  111. function unreg_app() //Удаляем приложение из реестра
  112. {
  113.   guid = "{FC36F676-4675-4CC9-B705-3AC86F456214}";
  114.  
  115.   var Root = "HKEY_LOCAL_MACHINE";
  116.   var key = "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\explorer\\AutoplayHandlers\\Handlers\\WIA_"+ guid +"\\";
  117.   var WshShell = new ActiveXObject("WScript.Shell");
  118.   WshShell.RegDelete(Root + key);
  119.  
  120.   key = "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\StillImage\\Registered Applications\\AppScan";
  121.   WshShell.RegDelete(Root + key);
  122.  
  123.   key = "\\SYSTEM\\CurrentControlSet\\Control\\StillImage\\Events\\STIProxyEvent\\" + guid + "\\";
  124.   WshShell.RegDelete(Root + key);
  125.  
  126.   write_log(get_ini_value("config.ini", "logfile"), "Application unregistered.");
  127.  
  128.   return;
  129. }
  130.  
  131.  
  132. function get_ini_value(iniFile, valueName) //Чтение параметра из ini файла
  133. {
  134.   var app_path = AppDomain.CurrentDomain.BaseDirectory;
  135.  
  136.   fso = new ActiveXObject("Scripting.FileSystemObject");
  137.   ForReading = 1;
  138.   ts = fso.OpenTextFile(app_path + iniFile, ForReading);
  139.  
  140.   while (!ts.AtEndOfStream)
  141.   {
  142.     s = ts.ReadLine();
  143.     if (s.IndexOf("$date$") > 0) //Если в строке есть патерн даты
  144.     {
  145.       var date = new Date();
  146.       var date_print = date.getYear() + ".";
  147.  
  148.       if (eval(date.getMonth()) < 10) date_print += "0"; date_print += date.getMonth() + ".";
  149.       if (eval(date.getDate()) < 10) date_print += "0"; date_print += date.getDate() + " ";
  150.       if (eval(date.getHours()) < 10) date_print += "0"; date_print += date.getHours() + "-";
  151.       if (eval(date.getMinutes()) < 10) date_print += "0"; date_print += date.getMinutes() + "-";
  152.       if (eval(date.getSeconds()) < 10) date_print += "0"; date_print += date.getSeconds();
  153.  
  154.       s = s.substring(0, s.IndexOf("$date$")) + date_print + s.substring(s.IndexOf("$date$") + 6, s.length);
  155.     }
  156.  
  157.     if (s.substring(0, s.IndexOf("=")) == valueName) return s.substring(s.IndexOf("=") + 1, s.length);
  158.   }
  159.  
  160.   ts.Close();
  161.  
  162.   return;
  163. }
  164.  
  165.  
  166. function write_log(logFileName, logstring) //Пишем в лог файл
  167. {
  168.   if (get_ini_value("config.ini", "debugflag") == "true")
  169.   {
  170.     var app_path = AppDomain.CurrentDomain.BaseDirectory;
  171.  
  172.     ForWriting = 2;
  173.     fso = new ActiveXObject("Scripting.FileSystemObject");
  174.     ts = fso.OpenTextFile(app_path + logFileName, ForWriting, true);
  175.     ts.WriteLine(logstring) ;
  176.     ts.Close();
  177.   }
  178.   return;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement