1. ;;;=============================================================
    2. ;;; ActiveX和脚本技术及其应用
    3. ;;; ------------------------------------------------------------
    4. ;;; 此文提到的程序,都通过验证。不排除某些特殊情况下不能通过。另
    5. ;;; 外,有些地方虽然是C:开头的函数,但是不建议运行整段程序,否则
    6. ;;; 可能无法通过。之所以列出来,仅仅为提供思路和举例说明。
    7. ;;;=============================================================
    8. (vl-load-com)
    9. ;;;Get the system special path 用来获取计算机系统的特别路径
    10. (defun GetSpecialPath (n / fso path)
    11. (setq fso (vlax-create-object "Scripting.FileSystemObject"))
    12. (setq path (vlax-get (vlax-invoke fso 'GetSpecialFolder n) 'path))
    13. (vlax-release-object fso)
    14. path
    15. )
    16. ;;;两种方法有不同。
    17. (defun GetSpecialFolder (name / wsh spec folder)
    18. (setq wsh (vlax-create-object "wscript.shell"))
    19. (setq Spec (vlax-get wsh 'SpecialFolders))
    20. (setq folder (vlax-invoke spec 'item name))
    21. (vlax-release-object spec)
    22. (vlax-release-object wsh)
    23. folder
    24. )
    25. ;;;Get some special folders 用来获取计算机的所有特别路径
    26. (defun GetAllSpecialFolders (/ wsh spec count folders)
    27. (setq wsh (vlax-create-object "wscript.shell"))
    28. (setq Spec (vlax-get wsh 'SpecialFolders))
    29. (setq count (vlax-invoke spec 'count))
    30. (repeat count
    31. (setq count (1- count))
    32. (setq folders (cons (vlax-invoke spec 'item count) folders))
    33. )
    34. (vlax-release-object spec)
    35. (vlax-release-object wsh)
    36. folders
    37. )
    38. ;;;=============================================================
    39. ;;;import type library 获得输人类型库
    40. ;;;=============================================================
    41. (defun C:GetLibrary (/ path)
    42. (setq path (strcat (GetSpecialPath 1) "\\wshom.ocx")) ;for Wscript.shell
    43. (if (not wc-Alias)
    44. (vlax-import-type-library
    45. :tlb-filename path
    46. :methods-prefix "wm-"
    47. :properties-prefix "wp-"
    48. :constants-prefix "wc-"
    49. )
    50. )
    51. (setq path (strcat (GetSpecialPath 1) "\\msscript.ocx")) ;for ScriptControl
    52. (if (not sc-Connected)
    53. (vlax-import-type-library
    54. :tlb-filename path
    55. :methods-prefix "sm-"
    56. :properties-prefix "sp-"
    57. :constants-prefix "sc-"
    58. )
    59. )
    60. (setq path (strcat (getSpecialPath 1) "\\scrrun.dll")) ;for Shell.application
    61. (if (not fc-Alias)
    62. (vlax-import-type-library
    63. :tlb-filename path
    64. :methods-prefix "fm-"
    65. :properties-prefix "fp-"
    66. :constants-prefix "fc-"
    67. )
    68. )
    69. (setq path (strcat (getSpecialPath 1) "\\shell32.dll")) ;for Shell.application
    70. (if (not ac-ssfwindows)
    71. (vlax-import-type-library
    72. :tlb-filename path
    73. :methods-prefix "am-"
    74. :properties-prefix "ap-"
    75. :constants-prefix "ac-"
    76. )
    77. )
    78. ;;(VL-REGISTRY-READ "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Office\\14.0\\Word\\InstallRoot")
    79. (if (not mswc-wd70) ; check for a WinWord constant
    80. (vlax-import-type-library
    81. :tlb-filename "C:\\Program Files\\Microsoft Office\\Office14\\msword.olb" ;依据自己的安装路径和版本
    82. :methods-prefix "mswm-"
    83. :properties-prefix "mswp-"
    84. :constants-prefix "mswc-"
    85. )
    86. )
    87. )
    88. ;;;=============================================================
    89. ;;; 利用wscript发送键
    90. ;;;=============================================================
    91. (defun c:sendkey ()
    92. (setq wsh (vlax-create-object "WScript.Shell"))
    93. ;;Send keys
    94. (wm-sendkeys wsh "C{ENTER}0,0{ENTER}100{ENTER}") ;Draw a circle in CAD. 在CAD里面画一个圆
    95. (WM-SENDKEYS wsh (chr 1)) ;Ctrl + A
    96. (WM-SENDKEYS wsh (chr 15)) ;Ctrl + O
    97. (WM-SENDKEYS wsh (chr 22)) ;Ctrl + V
    98. ;; if your system can read Chinese,these will be interesting.
    99. (WM-SENDKEYS wsh "赌") ;Open My computer 打开我的电脑
    100. (WM-SENDKEYS wsh "品") ;Open Calc.exe 打开计算器
    101. (WM-SENDKEYS wsh "血") ;Open Search 打开搜索
    102. (WM-SENDKEYS wsh "恋") ;Open Media Player 打开媒体播放器
    103. (WM-SENDKEYS wsh "爽") ;Open homepage 打开主页
    104. (vlax-release-object wsh)
    105. (princ)
    106. )
    107. ;;;=============================================================
    108. ;;; 测试scriptcontrol,wscript.shell
    109. ;;;=============================================================
    110. (defun C:test1 (/ scr wsh DESKTOPPATH ENV I LINK OBJENV OBJPRO OBJVAR OBJWMI SPEC STR URL)
    111. (C:GetLibrary)
    112. ;;Create a scriptControl instance. 创建脚本对象
    113. ;;(setq scr (vlax-create-object "MSScriptControl.ScriptControl.1"))
    114. (setq scr (vlax-create-object "ScriptControl"))
    115. ;;put its language as "VBS" 赋予脚本语言为vbs
    116. (vlax-put Scr "language" "vbs")
    117. (sp-put-Language scr "VBS") ;the same as upper 两者方法相同
    118. ;;Create a Wscript.shell 创建Wscript.shell对象
    119. (setq wsh (vlax-create-object "WScript.Shell"))
    120. ;;Pop up a simple box 简单的对话框
    121. (wm-Popup wsh "Hello,World!")
    122. ;;Input box 输入框
    123. (vlax-invoke scr 'ExecuteStatement "str=InputBox(\"Input your string:\", \"Input Box\")")
    124. (sm-ExecuteStatement scr "str=InputBox(\"Input your string:\", \"Input Box\")")
    125. ;;Eval a WSH variale. 对脚本变量的求值
    126. (vlax-invoke scr 'eval "str") ;get the string you input.
    127. (princ (strcat "\n脚本中str的数值是:" (sm-eval scr "str")))
    128. ;;Create a URL shortcut 创建一个快捷链接
    129. (setq Spec (wp-get-SpecialFolders wsh))
    130. (setq deskTopPath (wm-item spec "DeskTop"))
    131. (setq url (wm-CreateShortcut wsh (strcat deskTopPath "\\MyTest.URL")))
    132. (wp-put-TargetPath url "http://www.theswamp.org/")
    133. (wm-save url)
    134. ;;Create a shortcut and assign a shortcut key 创建快捷方式或者快捷键
    135. (setq link (wm-CreateShortcut wsh (strcat DeskTopPath "\\MyTest.lnk")))
    136. (wp-put-TargetPath link "http://www.theswamp.org/")
    137. (wp-put-WindowStyle link 1)
    138. (wp-put-Hotkey link "Ctrl+Alt+T")
    139. (wp-put-IconLocation link "shell32.dll,14")
    140. (wp-put-Description link "The desciption for Mytest")
    141. (wp-put-WorkingDirectory link "c:\\")
    142. (wm-save link)
    143. ;;Run command 运行命令
    144. (wm-run wsh "cmd.exe /C dir c:\\temp\\*.* /a /s >>c:\\1.txt")
    145. ;;Get a WshEnvironment 获取系统环境变量
    146. (Setq env (wp-get-Environment wsh "System"))
    147. ;;Get the special path of system. 获取某些特殊路径
    148. (alert (wp-get-item env "WINDIR"))
    149. (alert (wm-ExpandEnvironmentStrings wsh "%windir%"))
    150. (alert (wp-get-Item env "TMP"))
    151. (alert (wp-get-Item env "TEMP"))
    152. ;;Add or remove an Environment variable 添加或者移除环境变量
    153. (alert "Add a test var to the system!")
    154. (wp-put-item env "TestVar" "Windows Script Host")
    155. (alert "Remove the test var from the system!")
    156. (wm-remove env "TestVar")
    157. ;;List the Environment variables 列表环境变量
    158. (setq i 0)
    159. (repeat (wm-count env)
    160. (princ (wp-get-item env i)) ;wouldn't display 不会显示
    161. (setq i (1+ i))
    162. )
    163. ;;Registration table
    164. ;;Regread ,RegWrite,RegDelete 读写注册表
    165. (vlax-invoke wsh 'RegRead "HKCU\\Software\\AutoDesk\\AutoCAD\\R16.2\\curver") ;ensure your CAD is autocad 2006 根据自己的CAD版本确定
    166. ;;Maybe you would like this way: 利用vbs的脚本来获取
    167. (setq str
    168. "Set WshShell = CreateObject(\"WScript.Shell\")
    169. Msgbox \"Environment.item: \"& WshShell.Environment.item(\"WINDIR\")
    170. Msgbox \"ExpandEnvironmentStrings: \"& WshShell.ExpandEnvironmentStrings(\"%windir%\")
    171. set oEnv=WshShell.Environment(\"System\")
    172. Msgbox \"Adding ( TestVar=Windows Script Host ) to the System type environment\"
    173. oEnv(\"TestVar\") = \"Windows Script Host\"
    174. Msgbox \"removing ( TestVar=Windows Script Host ) from the System type environment\"
    175. oEnv.Remove \"TestVar\"
    176. for each sitem in oEnv
    177. strval=strval & sItem & vbcrlf
    178. next
    179. Msgbox \"System Environment:\" & vbcrlf & vbcrlf & strval
    180. strval=\"\"'
    181. set oEnv=WshShell.Environment(\"Process\")
    182. for each sitem in oEnv
    183. strval=strval & sItem & vbcrlf
    184. next
    185. Msgbox \"Process Environment:\" & vbcrlf & vbcrlf & strval
    186. strval=\"\"
    187. set oEnv=WshShell.Environment(\"User\")
    188. for each sitem in oEnv
    189. strval=strval & sItem & vbcrlf
    190. next
    191. Msgbox \"User Environment:\" & vbcrlf & vbcrlf & strval
    192. strval=\"\"
    193. set oEnv=WshShell.Environment(\"Volatile\")
    194. for each sitem in oEnv
    195. strval=strval & sItem & vbcrlf
    196. next
    197. Msgbox \"Volatile Environment:\" & vbcrlf & vbcrlf & strval
    198. strval=\"\"
    199. set oEnv = nothing
    200. set WshShell = nothing
    201. "
    202. )
    203. (vlax-invoke Scr 'ExecuteStatement str)
    204. ;;Get some information from OS 获取操作系统信息
    205. (setq str "Set mc=GetObject(\"Winmgmts:\")")
    206. (SM-EXECUTESTATEMENT scr str)
    207. (setq objWMI (vla-eval scr "mc"))
    208. ;;another way 另一种方法
    209. (setq objENv (vlax-invoke objWMI 'get "Win32_Environment"))
    210. (setq objvar (vlax-invoke objenv 'SpawnInstance_))
    211. (setq objPro (vlax-get objvar 'Properties_))
    212. ;; get more usage 获取更多用法
    213. (vlax-dump-object objvar T)
    214. (vlax-for n objpro
    215. (vlax-dump-object n T)
    216. )
    217. ;; 测试环境比变量的查询、增加和删除
    218. (vlax-put (vlax-invoke objpro 'item "name") 'value "TestValue")
    219. (vlax-put (vlax-invoke objpro 'item "UserName") 'value "System")
    220. (vlax-put (vlax-invoke objpro 'item "VariableValue") 'value "This is a test")
    221. (vlax-put objvar 'name "MyTest")
    222. (vlax-put objvar 'UserName "System")
    223. (vlax-put objvar 'VariableValue "This is a test")
    224. (vlax-invoke objvar 'put_)
    225. (vlax-for obj (vlax-invoke objWMI 'ExecQuery "Select * from Win32_Environment Where Name = 'Path'")
    226. (princ (strcat "\nName is:" (vlax-get obj 'name)))
    227. (princ (strcat "\nUser Name is:" (vlax-get obj 'username)))
    228. (princ (strcat "\nVariable value is:" (vlax-get obj 'variablevalue)))
    229. )
    230. ;; 最终释放
    231. (and ObjPro (vlax-release-object ObjPro))
    232. (and objvar (vlax-release-object objvar))
    233. (and objENv (vlax-release-object objENv))
    234. (and objWMI (vlax-release-object objWMI))
    235. (and scr (vlax-release-object scr))
    236. (and wsh (vlax-release-object wsh))
    237. (princ)
    238. )
    239. ;;;=============================================================
    240. ;;; 用WMI来获取系统软硬件等信息.
    241. ;;;=============================================================
    242. (defun C:Test2 (/ SVR WMI)
    243. ;; ProcessorId 获取处理器ID的子程序
    244. (defun C:CPUID (/ WMI SVR CPU s)
    245. (setq WMI (vlax-create-object "WbemScripting.SWbemLocator"))
    246. (setq SVR (VLAX-INVOKE WMI 'ConnectServer))
    247. (setq CPU (vlax-invoke SVR 'InstancesOf "Win32_Processor"))
    248. (vlax-for item CPU
    249. (setq s (cons (vlax-get item 'ProcessorId) s))
    250. )
    251. (vlax-release-object CPU)
    252. (vlax-release-object SVR)
    253. (vlax-release-object WMI)
    254. (reverse s)
    255. )
    256. (defun CPUID(/ scr str objwmi objcpu)
    257. (setq scr (vlax-create-object "ScriptControl"))
    258. (vlax-put scr 'language "VBS")
    259. (setq str "Set mc=GetObject(\"Winmgmts:\")")
    260. (vlax-invoke scr 'EXECUTESTATEMENT str)
    261. (setq objWMI (vla-eval scr "mc"))
    262. (setq objCPU (vlax-invoke objWMI 'InstancesOF "Win32_Processor"))
    263. (vlax-for obj objCPU
    264. (alert (vlax-get obj 'ProcessorId))
    265. )
    266. (vlax-release-object objCPU)
    267. (vlax-release-object objWMI)
    268. (vlax-release-object scr)
    269. (princ)
    270. )
    271. ;;可通过下面两种方式获得ISWbemServicesEx实例:(我已注释掉方式1)
    272. ;;(setq str "Set mc=GetObject(\"Winmgmts:\")")
    273. ;;(SM-EXECUTESTATEMENT scr str)
    274. ;;(setq SVR (vla-eval scr "mc"))
    275. (setq WMI (vlax-create-object "WbemScripting.SWbemLocator"))
    276. (setq SVR (VLAX-INVOKE WMI 'ConnectServer))
    277. ;;you can get more details by these ways:
    278. (foreach p (list
    279. "Win32_ComputerSystem"
    280. "Win32_Service"
    281. "Win32_LogicalMemoryConfiguration"
    282. "Win32_Process"
    283. "Win32_Processor"
    284. "Win32_OperatingSystem"
    285. "Win32_WMISetting"
    286. "__NAMESPACE"
    287. "win32_baseboard"
    288. "win32_videocontroller"
    289. "win32_DiskDrive"
    290. "win32_physicalMemory"
    291. "Win32_Environment"
    292. "Win32_ProcessStartTrace"
    293. "Win32_PnpDevice"
    294. "Win32_SoundDevice"
    295. "Win32_ProductCheck"
    296. "Win32_NetworkAdapter"
    297. "Win32_CDROMDrive"
    298. "Win32_DesktopMonitor"
    299. "Win32_NetworkAdapterConfiguration"
    300. )
    301. (vlax-for n (vlax-invoke SVR 'InstancesOf p)
    302. (alert (vlax-invoke n 'GetObjectText_))
    303. )
    304. )
    305. ;;Just collect some simple information:
    306. ;;Get User name.用户名
    307. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_ComputerSystem")
    308. (princ "\nUser name is:")
    309. (princ (vlax-get n 'name))
    310. )
    311. ;;Get the running process 进程
    312. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_Process")
    313. (princ (strcat "\n" (vlax-get n 'name)))
    314. )
    315. ;;Get the information of CPU 处理器
    316. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_Processor")
    317. (princ (vlax-get n 'name))
    318. )
    319. ;;Get the Total of Computer System 计算机系统
    320. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_ComputerSystem")
    321. (princ (/ (read (vlax-get n 'TotalPhysicalMemory)) 1048576))
    322. (princ "M")
    323. )
    324. ;;Get the information of physical memory 物理内存
    325. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_PhysicalMemory")
    326. (princ "\n")
    327. (princ (vlax-get n 'Description))
    328. (princ "\n")
    329. (princ (vlax-get n 'DeviceLocator))
    330. (princ "\n")
    331. (princ (vlax-get n 'speed))
    332. )
    333. ;;Get the information of Video Controller 显卡
    334. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_VideoController")
    335. (princ "\n")
    336. (princ (vlax-get n 'Caption))
    337. (princ "\n")
    338. (princ (vlax-get n 'VideoModeDescription))
    339. )
    340. ;;Get the information of Disk drive 磁盘
    341. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_DiskDrive")
    342. (princ "\nThe Caption is:")
    343. (princ (vlax-get n 'Caption))
    344. (princ "\nThe size is:")
    345. (princ (/ (read (vlax-get n 'size)) 1073741824))
    346. (princ "G")
    347. )
    348. ;;Get the information of Sound Device 声卡
    349. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_SoundDevice")
    350. (princ "\nThe product Name is:")
    351. (princ (vlax-get n 'ProductName))
    352. )
    353. ;;Network Adapter 网卡配置信息
    354. (princ "\nThe Mac Address is: ")
    355. (vlax-for obj (vlax-invoke SVR 'InstancesOF "Win32_NetworkAdapterConfiguration")
    356. (if(/= (vlax-get obj 'IPEnabled) 0)
    357. (princ (vlax-get obj 'MacAddress))
    358. )
    359. )
    360. ;;Get the information of Network Adapter 网络适配器
    361. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_NetworkAdapter")
    362. (princ "\nThe Description is:")
    363. (princ (vlax-get n 'Description))
    364. (princ "\nThe MAC address is")
    365. (princ (vlax-get n 'MACAddress))
    366. )
    367. ;;Get the information of FloppyDrive --haha,do you have a floppy drive? 软驱
    368. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_FloppyDrive")
    369. (princ "\nThe caption is:")
    370. (princ (vlax-get n 'Caption))
    371. )
    372. ;;Get the information of CD/DVD ROM 光驱
    373. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_CDROMDrive")
    374. (princ "\nThe Drive name is:")
    375. (princ (vlax-get n 'Name))
    376. (princ "\nThe description is:")
    377. (princ (vlax-get n 'Description))
    378. )
    379. ;;Get the information of Desktop Monitor 显示器
    380. (vlax-for n (vlax-invoke SVR 'InstancesOf "Win32_DesktopMonitor")
    381. (princ "\nScreen Width:")
    382. (princ (vlax-get n 'ScreenWidth))
    383. (princ "\nScreen Height:")
    384. (princ (vlax-get n 'ScreenHeight))
    385. )
    386. ;;Printer 打印机
    387. (vlax-for obj (vlax-invoke SVR 'InstancesOF "Win32_Printer")
    388. (vlax-dump-object obj T)
    389. (alert (vlax-get obj 'Name))
    390. (vlax-get obj 'PaperSizesSupported)
    391. (alert (vlax-invoke obj 'GetObjectText_))
    392. )
    393. (vlax-release-object SVR)
    394. (vlax-release-object WMI)
    395. (princ)
    396. )
    397. ;;;=============================================================
    398. ;;; Shell.Application的一些例子函数和应用
    399. ;;; some small applications of Shell.application
    400. ;;;=============================================================
    401. (defun C:test3 (/ *SHELL BIN EXEC FILE MARK OPFLAG PATH ROOT)
    402. (setq path (strcat (getSpecialPath 1) "\\shell32.dll"))
    403. (if (not ac-ssfwindows)
    404. (vlax-import-type-library
    405. :tlb-filename path
    406. :methods-prefix "am-"
    407. :properties-prefix "ap-"
    408. :constants-prefix "ac-"
    409. )
    410. )
    411. (setq *SHELL (vlax-create-object "Shell.Application"))
    412. (am-CascadeWindows *SHELL) ;Cascade Windows 层叠窗口
    413. (am-ControlPanelItem *SHELL "inetcpl.cpl" ) ;Open a control panel(internet) 打开控制面板
    414. (am-settime *SHELL) ;Open the time setting dialog 打开时间和日期设置对话框
    415. (am-TrayProperties *SHELL) ;日期和时间 属性
    416. (am-explore *SHELL "c:\\") ;Explore C: 打开 C 盘
    417. (am-FindComputer *SHELL) ;Search a computer 搜索计算机
    418. (am-findPrinter *SHELL "canno") ;Search a printer 搜索打印机
    419. (am-GetSystemInformation *SHELL "ProcessorSpeed") ;Get the processor speed(in windows 7 or vista) 处理器速度
    420. (am-GetSystemInformation *SHELL "PhysicalMemoryInstalled") ;the capacity of physical memory 物理内存容量
    421. (am-GetSystemInformation *SHELL "IsOS_Professional") ;Check the Operating system version is professional or not. 是否是专业版
    422. (am-filerun *SHELL) ;Open "Windows Run" 打开运行窗口
    423. (am-ShutdownWindows *SHELL) ;Shutdown windows 关机对话框
    424. (am-findfiles *SHELL) ;Searh files 搜索文件
    425. (am-toggledesktop *SHELL) ;toggle desktop 显示桌面
    426. (am-IsServiceRunning *SHELL "Spooler") ;check a service is running or not(e.g,spooler service) 检测某项服务(打印机)是否在运行
    427. (am-WindowsSecurity *SHELL) ;Windows Security Windows安全
    428. (am-AddToRecent *SHELL "c:\\1.txt") ;Add to recent 添加到最近打开文档
    429. (am-namespace *SHELL "c:\\") ;return a folder object 返回所打开的Folder对象
    430. (am-BrowseForFolder *SHELL ;BrowseForFolder 选择文件夹对话框
    431. (vla-get-hwnd (vlax-get-acad-object) )
    432. "Select a folder"
    433. 64
    434. )
    435. (am-BrowseForFolder shapp 0 "我的电脑" 16 17) ;打开文件浏览对话框,并获得文件夹对象
    436. (am-open *SHELL "c:\\") ;Open a folder. 打开某个目录
    437. ;;Here is an example,to get some details of a picture file. 获得图像的详细信息,包括分辨率等等
    438. (defun GetInfoOfPic(*SHELL path name / info root file i l)
    439. (setq root (am-namespace *SHELL path))
    440. (setq file (am-ParseName root name))
    441. (setq i 0)
    442. (repeat 256
    443. (setq info (am-GetDetailsOf root file i))
    444. (if (/= info "")
    445. (progn
    446. (princ (strcat "\nIndex " (itoa i) ": " info))
    447. (setq l (cons info l))
    448. )
    449. )
    450. (setq i (1+ i))
    451. )
    452. (reverse l)
    453. )
    454. (getInfoOfPic *SHELL "D:\\temp" "1.jpg")
    455. ;; 下面是一个小小的程序,用来获得某个目录下的文件夹和文件名
    456. ;; Here is a program to get a foler and its subdirectories and files name.
    457. (defun BrowseFolder(*SHELL fp / root items count i item path name)
    458. (setq root (am-namespace *SHELL fp))
    459. (setq items (am-items root))
    460. (setq count (ap-get-Count items))
    461. (setq i 0)
    462. (repeat count
    463. (setq item (am-item items i))
    464. (setq path (ap-get-path item))
    465. (setq name (ap-get-name item))
    466. (if (= (ap-get-IsFolder item) :vlax-true)
    467. (progn
    468. (princ (strcat "\n---Folder:" path))
    469. (BrowseFolder *SHELL path)
    470. )
    471. (princ (strcat "\nFile name:" name))
    472. )
    473. (setq i (1+ i))
    474. )
    475. )
    476. (BrowseFolder *SHELL "C:\\Program Files (x86)\\AutoCAD 2006") ;此处路径根据你的情况而定
    477. ;;to Create a new foler 创建文件夹
    478. (setq root (am-namespace *SHELL "d:\\"))
    479. (am-NewFolder root "Test")
    480. ;;to copy a file 拷贝文件
    481. (setq file (am-ParseName root "1.jpg"))
    482. (am-copyhere (am-namespace *SHELL "c:\\") file 16)
    483. ;;to move a file (e.g,move a file to recyle bin) 移动一个文件
    484. (setq opFlag 1108) ;FOF_ALLOWUNDO | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMATION
    485. (setq bin (am-namespace *SHELL 10)) ;Recyle bin
    486. (am-movehere bin "c:\\1.dwg" opFlag) ;move to recyle bin
    487. ;;Get some special folder. 得到某些特殊文件夹
    488. (am-NameSpace *SHELL "shell:PrintersFolder")
    489. (am-NameSpace *SHELL "shell:personal")
    490. (am-NameSpace *SHELL "shell:drivefolder")
    491. ;;(am-ShowBrowserBar *SHELL "{C4EE31F3-4768-11D2-BE5C-00A0C9A83DA1}" :vlax-true);;???
    492. ;;Verbs and File Associations 执行跟一个文件或者文件夹相关联的
    493. (am-doit (am-item (am-verbs (ap-get-self root)) 0))
    494. (am-doit (am-item (am-verbs file) 0))
    495. ;;Open some control panel options
    496. (am-ShellExecute *SHELL "Explorer.exe" "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") ;Open "My Computer" 打开我的电脑
    497. (am-ShellExecute *SHELL "Rundll32.exe" "shell32.dll,Control_RunDLL netcpl.cpl,,1") ;
    498. (am-ShellExecute *SHELL "Rundll32.exe" "shell32.dll,Control_RunDLL sysdm.cpl,,2" ) ;Open "System Property" 系统属性
    499. (am-ShellExecute *SHELL "Rundll32.exe" "shdocvw.dll,OpenURL %l") ;Internet shortcut ,IE8,IE7? Internet 快捷方式
    500. (am-ShellExecute *SHELL "Rundll32.exe" "msconf.dll,OpenConfLink") ;SpeedDial
    501. (am-ShellExecute *SHELL "Rundll32.exe" "zipfldr.dll,RouteTheCall") ;Zip file 压缩文件夹shdocvw.dll,OpenURL
    502. (am-ShellExecute *SHELL "Rundll32.exe" "netplwiz.dll,UsersRunDll") ;user account control panel 用户帐户
    503. (am-ShellExecute *SHELL "Rundll32.exe" "shell32.dll,Options_RunDLL 0") ;Open Folder Options 文件夹选项
    504. (am-ShellExecute *SHELL "Rundll32.exe" "shell32.dll,Options_RunDLL 1") ;Open Taskbar 显示任务栏和开始菜单
    505. (am-ShellExecute *SHELL "Rundll32.exe" "shell32.dll,Control_RunDLLAsUser") ;Open Control panel 控制面版
    506. (am-ShellExecute *SHELL "rundll32.exe" "kernel32.dll,Sleep 10000")
    507. (am-ShellExecute *SHELL "rundll32.exe" "kernel32.dll GetVersion")
    508. (am-shellExecute *SHELL "Rundll32.exe" "user32.dll,Messagebox 0 hahah aaa 0")
    509. (am-ShellExecute *SHELL "Rundll32.exe" "shell32.dll,SHCreateLocalServerRunDll {601ac3dc-786a-4eb0-bf40-ee3521e70bfb}");
    510. ;;Execute shell command.
    511. (am-ShellExecute *SHELL "cmd.exe")
    512. (setq root (am-namespace *SHELL "c:\\windows\\system32"))
    513. (setq exec (am-parsename root "CMD.exe"))
    514. (am-invokeverb exec)
    515. ;;Favorite,add to bookmark 加入到收藏夹
    516. (setq mark (vlax-create-object "Shell.UIHelper.1"))
    517. (vlax-invoke mark 'AddChannel "http://www.theswamp.org/")
    518. (vlax-invoke mark 'AddFavorite "http://www.theswamp.org/" "Theswamp")
    519. (vlax-invoke mark 'AddDesktopComponent "d:\\1.jpg" "image")
    520. ;;Get information from a special path 获取某些特别路径
    521. (defun GetInfo(*SHELL folds / objs i obj name lst prop)
    522. (setq objs (am-items (am-namespace *SHELL folds))) ;from ac-XXXXXX
    523. (setq i 0)
    524. (repeat (ap-get-count objs)
    525. (setq obj (am-item objs i))
    526. (setq name (ap-get-name obj))
    527. (setq prop (am-ExtendedProperty obj "type"))
    528. (setq lst (cons (cons name prop) lst))
    529. (setq i (1+ i))
    530. )
    531. (reverse lst)
    532. )
    533. ;;some examples: 一些例子用来获取某些特殊目录下的文件信息
    534. (getInfo *SHELL ac-ssffonts) ;Get the fonts installed.获取系统中安装的字体
    535. (getInfo *SHELL ac-ssfCONTROLS) ;Get the control panles.获取有哪些控制面板
    536. (getInfo *SHELL ac-ssfMYPICTURES) ;Get the pictures in "My Pictures" 获取我的图片
    537. (getInfo *SHELL ac-ssfDRIVES) ;Get the Drivers in "My computer" 获取系统的磁盘信息
    538. (getInfo *SHELL ac-ssfnetwork) ;Get the information of "network" 获得网上邻居
    539. (getInfo *SHELL ac-ssfsystem) ;Get the files from system folder.系统文件夹信息
    540. (getInfo *SHELL ac-ssfRecent) ;Get the Recent opened files 获得最近打开
    541. ;;this function to get the windows that opened by "Explore"
    542. (defun GetWindows(*SHELL / i l lst obj winobj)
    543. (vlax-invoke *SHELL 'windows)
    544. (vlax-get (vlax-invoke *SHELL 'windows) 'count)
    545. (setq winobj (vlax-invoke *SHELL 'windows))
    546. (setq i 0)
    547. (repeat (vlax-get winobj 'count)
    548. (setq obj (vlax-invoke winobj 'item i))
    549. (setq lst (list
    550. (vlax-get obj 'toolbar)
    551. (vlax-get obj 'StatusText)
    552. (vlax-get obj 'FullName)
    553. (vlax-get obj 'LocationURL)
    554. (vlax-get obj 'Path)
    555. )
    556. )
    557. (setq l (cons lst l))
    558. (setq i (1+ i))
    559. )
    560. (reverse l)
    561. )
    562. (GetWindows *SHELL)
    563. ;;最后不要忘记释放
    564. (vlax-release-object mark)
    565. (vlax-release-object root)
    566. (vlax-release-object file)
    567. (vlax-release-object exec)
    568. (vlax-release-object *SHELL)
    569. (princ)
    570. )
    571. ;;;=============================================================
    572. ;;;Get filesystemObject
    573. ;;;=============================================================
    574. (defun c:test4 ( / FSO PATH)
    575. (setq path (strcat (getSpecialPath 1) "\\scrrun.dll"))
    576. (if (not fc-Alias)
    577. (vlax-import-type-library
    578. :tlb-filename path
    579. :methods-prefix "fm-"
    580. :properties-prefix "fp-"
    581. :constants-prefix "fc-"
    582. )
    583. )
    584. (setq fso (vlax-create-object "Scripting.FileSystemObject"))
    585. ;;Print a foler and its subfolders
    586. (defun showSubFolder (folder)
    587. (vlax-for subfolder (fp-get-SubFolders folder)
    588. (princ (strcat "\n" (fp-get-Path subfolder)))
    589. (ShowSubFolder subFolder)
    590. )
    591. )
    592. ;;Get a folder and all of its subfolders(recursively)
    593. (defun GetSubFolder (fso path / l)
    594. (defun GetSubFolder1 (folder / p)
    595. (vlax-for subfolder (fp-get-SubFolders folder)
    596. (setq p (fp-get-Path subfolder))
    597. (setq l (cons p (GetSubFolder1 subFolder)))
    598. )
    599. l
    600. )
    601. (setq l (list path))
    602. (if (fm-folderExists fso path)
    603. (reverse (getSubFolder1 (fm-getFolder fso path)))
    604. )
    605. )
    606. (showSubFolder (fm-GetFolder fso "C:\\temp"))
    607. (getSubFolder fso "C:\\temp")
    608. (defun GetNumOfDrives(fso / drives i)
    609. (setq drives (vlax-get fso 'drives))
    610. (setq i 0)
    611. (vlax-for drive drives
    612. (vlax-dump-object drive)
    613. (setq i (1+ i))
    614. )
    615. (princ "\nThe count of disks is: ")
    616. (princ i)
    617. i
    618. )
    619. (GetNumOfDrives fso)
    620. ;; Read stream
    621. (defun ReadStream (path format / fso file str res size)
    622. ;;path the full name of a file
    623. ;;iomode 1 ;; 1 = read, 2 = write, 8 = append
    624. ;;format 0 ;; 0 = ascii, -1 = unicode, -2 = system default
    625. (setq fso (vlax-create-object "Scripting.FileSystemObject"))
    626. (setq file (vlax-invoke fso 'getfile path))
    627. (setq str (vlax-invoke fso 'OpenTextFile path 1 format))
    628. (setq size (vlax-get file 'Size))
    629. (setq res (vlax-invoke str 'read size))
    630. (vlax-invoke str 'close)
    631. (if str (vlax-release-object str))
    632. (if file (vlax-release-object file))
    633. (if fso (vlax-release-object fso))
    634. res
    635. )
    636. ;;Write stream
    637. (defun WriteStream (path text format / fso str file res)
    638. (setq fso (vlax-create-object "Scripting.FileSystemObject"))
    639. (setq str (vlax-invoke fso 'CreateTextFile path -1 format))
    640. (setq file (vlax-invoke fso 'getFile path))
    641. (vlax-invoke str 'Write text)
    642. (vlax-invoke str 'close)
    643. (setq res (vlax-get file 'size))
    644. (if str (vlax-release-object str))
    645. (if file (vlax-release-object file))
    646. (if fso (vlax-release-object fso))
    647. res
    648. )
    649. (writeStrem "C:\\test1.txt" (readStream "c:\\1.txt" -2) -2)
    650. (and fso (vlax-release-object fso))
    651. (princ)
    652. )
    653. ;;;=============================================================
    654. ;;; Manage user account 账户管理
    655. ;;; need run as Administrator 需要以管理员身份运行此程序
    656. ;;; maybe invalid in window 7 or vista vista以上版本可能无效
    657. ;;;=============================================================
    658. (defun c:User(/ PATH NEWUSR USROBJ)
    659. (setq path (strcat (GetSpecialPath 1) "\\shgina.dll"))
    660. (if (Not Uc-ILEU_ALPHABETICAL)
    661. (vlax-import-type-library
    662. :tlb-filename path
    663. :methods-prefix "Um-"
    664. :properties-prefix "Up-"
    665. :constants-prefix "Uc-"
    666. )
    667. )
    668. ;;add an account,and set password or something
    669. ;;then remove this account.
    670. (setq usrObj (vlax-create-object "Shell.users"))
    671. (setq newusr (um-create usrobj "test"))
    672. (up-put-setting newusr "AccountType" 3)
    673. (Um-changePassword newusr "111222" "")
    674. (um-remove usrObj "test")
    675. (vlax-release-object usrobj)
    676. (vlax-release-object newusr)
    677. (princ)
    678. )
    679. ;;;=============================================================
    680. ;;;Common File Dialog 公用对话框(包括文件对话框,颜色对话框等)
    681. ;;;=============================================================
    682. (defun c:FDLG(/ DLG PATH DLGOBJ FN FSOOBJ FT)
    683. (setq path (strcat (GetSpecialPath 1) "\\comdlg32.ocx"))
    684. (if (not dc-cdlalloc)
    685. (vlax-import-type-library
    686. :tlb-filename path
    687. :methods-prefix "dm-"
    688. :properties-prefix "dp-"
    689. :constants-prefix "dc-"
    690. )
    691. )
    692. (setq dlg (vlax-create-object "MSComDlg.CommonDialog")) ;UserAccounts.CommonDialog
    693. (dp-put-MaxFileSize dlg 10000)
    694. (dp-put-filter dlg "All Files (*.*)|*.*|Lisp Files(*.lsp)|*.lsp|DWG Files (*.dwg)|*.dwg")
    695. ;put the file filter
    696. (dm-ShowOpen dlg)
    697. (princ (strcat "\nThe file you opened is:\n" (dp-get-filename dlg)))
    698. ;;Another way
    699. (setq path (strcat (GetSpecialPath 1) "\\safrcdlg.dll")) ;safrcdlg.dll
    700. (if (not Fdp-get-FileName)
    701. (vlax-import-type-library
    702. :tlb-filename path
    703. :methods-prefix "Fdm-"
    704. :properties-prefix "Fdp-"
    705. :constants-prefix "Fdc-"
    706. )
    707. )
    708. ;;just for open (simple)
    709. (setq dlgobj (vlax-create-object "SAFRCFileDlg.FileOpen")) ;"SAFRCFileDlg.FileOpen"
    710. (Fdp-put-FileName dlgobj "C:\\")
    711. (Fdm-OpenFileOpenDlg dlgobj)
    712. (princ "\nThe file you opened is:\n")
    713. (princ (Fdp-get-FileName dlgobj))
    714. (vlax-release-object dlgobj)
    715. ;;Open for save
    716. (setq dlgobj (vlax-create-object "SAFRCFileDlg.FileSave")) ;"SAFRCFileDlg.FileSave"
    717. (setq FSOobj (vlax-create-object "Scripting.FileSystemObject"))
    718. (Fdp-put-FileName dlgobj "test")
    719. (Fdp-put-fileType dlgobj ".txt")
    720. (if (Fdm-OpenFileSaveDlg dlgobj)
    721. (progn
    722. (setq FN (Fdp-get-FileName dlgobj))
    723. (setq FT (Fdp-get-FileType dlgobj))
    724. (princ (strcat "\nThe file you will save:\n" FN FT))
    725. (vlax-invoke FSOobj 'CreateTextFile (strcat FN FT))
    726. )
    727. )
    728. (vlax-release-object dlgobj)
    729. (vlax-release-object FSOobj)
    730. (princ)
    731. )
    732. ;;;=============================================================
    733. ;;;Get or set clipboard by Form2.0 利用form2.0来设置或获取剪切板
    734. ;;;=============================================================
    735. (defun c:Form (/ BOX CTR FMO STR)
    736. (setq path (strcat (GetSpecialPath 1) "\\FM20.dll"))
    737. (if (not FMc-fmActionCopy)
    738. (vlax-import-type-library
    739. :tlb-filename path
    740. :methods-prefix "FMm-"
    741. :properties-prefix "FMp-"
    742. :constants-prefix "FMc-"
    743. )
    744. )
    745. ;;get text for clipboard
    746. (setq fmo (vlax-create-object "Forms.form.1")) ;Create a Form instance
    747. (setq ctr (FMP-GET-CONTROLs fmo)) ;the controls of this from
    748. (setq box (fmm-add ctr "Forms.textbox.1")) ;add a textbox control
    749. (Fmp-put-MultiLine box :vlax-true)
    750. (if (= (FMp-get-CanPaste box) :vlax-true) ;if can be pasted
    751. (progn
    752. (FMm-Paste box) ;paste into textbox
    753. (alert (fmp-get-text box)) ;show the text
    754. )
    755. )
    756. ;;set text for clipboard
    757. (setq str "Hello,theswamp!\nI Love you!")
    758. (Fmp-put-text box str) ;Set the text of clipboard
    759. (Fmp-put-SelStart box 0)
    760. (Fmp-put-SelLength box (Fmp-get-textlength box))
    761. (Fmm-copy box) ;copy it into textbox
    762. ;;release object
    763. (vlax-release-object box)
    764. (vlax-release-object ctr)
    765. (vlax-release-object fmo)
    766. (princ)
    767. )
    768. ;;;=============================================================
    769. ;;;Get or set clipboard by other way 剪切板的其他方式
    770. ;;;=============================================================
    771. (defun C:Clipboard (/ CLIP DOC IE SEL STR WORD WSH)
    772. ;; by InternetExplorer 用IE
    773. (setq IE (vlax-create-object "InternetExplorer.Application"))
    774. (vlax-invoke IE 'navigate "about:blank")
    775. (setq Clip (vlax-get (vlax-get (vlax-get ie 'Document) 'parentWindow) 'clipboardData))
    776. (vlax-invoke clip 'setdata "text" "This is a test!")
    777. (princ (vlax-invoke clip 'GetData "text"))
    778. (vlax-release-object IE)
    779. ;;works in windows 7 用wscript
    780. (setq wsh (vlax-create-object "Wscript.Shell"))
    781. (setq str "This is a test (by wscript.shll)")
    782. (vlax-invoke wsh 'run
    783. (strcat "CMD.exe /C echo " str " | clip")
    784. 0
    785. :vlax-false
    786. )
    787. (vlax-release-object wsh)
    788. ;;by Microsoft office word office
    789. ;;Set by Word.Application
    790. (setq word (vlax-create-object "Word.Application"))
    791. (setq doc (vlax-get word 'Documents))
    792. (setq sel (vlax-get word 'Selection))
    793. (vlax-invoke doc 'add)
    794. (vlax-put sel 'text "This is a test(by word)")
    795. (vlax-invoke sel 'copy)
    796. (vlax-invoke word 'quit 0)
    797. (vlax-release-object word)
    798. ;;Get by Word.Application
    799. (setq word (vlax-create-object "Word.Application"))
    800. (setq doc (vlax-get word 'Documents))
    801. (setq sel (vlax-get word 'Selection))
    802. (vlax-invoke doc 'add)
    803. (vlax-invoke sel 'Paste) ;word.Selection.PasteAndFormat(wdFormatPlainText)
    804. (vlax-invoke sel 'wholeStory)
    805. (princ "\nThe text in clipboard is:")
    806. (princ (vlax-get sel 'text))
    807. (vlax-release-object word)
    808. )
    809. ;;;=============================================================
    810. ;;; 获取局域网及公网地址和从网站获取信息
    811. ;;; Get your (IP) (local IP and internet IP)
    812. ;;; An example shows how to Get your IP and get text from an URL
    813. ;;;=============================================================
    814. ;;;方式1
    815. (defun c:getIP()
    816. (setq ws (vlax-create-object "MSWinsock.Winsock")) ;winsock object
    817. (princ "\nYour IP is:")
    818. (princ (vlax-get ws 'LocalIP)) ;Local IP
    819. (vlax-put ws 'Protocol 0)
    820. (vlax-put ws 'RemoteHost "www.baidu.com")
    821. (vlax-put ws 'RemotePort 80)
    822. (vlax-invoke ws 'connect)
    823. ;;(vlax-invoke ws 'connect "www.yhhe.net" 80)
    824. (setq Url "http://www.baidu.com/img/baidu_logo.gif")
    825. (setq Cmd (strcat "GET " url " HTTP/1.0\r\n\r\n"))
    826. (vlax-invoke ws 'SendData cmd)
    827. (vlax-get ws 'BytesReceived)
    828. (setq data (vlax-make-variant ""))
    829. (vlax-get ws 'state)
    830. (vlax-invoke ws 'getdata data vlax-vbString) ;???此处可能行不通
    831. (vlax-invoke ws 'close)
    832. (vlax-release-object ws)
    833. (princ)
    834. )
    835. ;;;方式2
    836. (defun c:getIP1()
    837. (setq ws (vlax-create-object "MSWinsock.Winsock"))
    838. (princ "\n本机的地址为:")
    839. (princ (vlax-get ws 'LocalIP))
    840. (vlax-dump-object ws T)
    841. (vlax-put ws 'Protocol 0)
    842. (vlax-put ws 'RemoteHost "www.ecranesoft.com")
    843. (vlax-put ws 'RemotePort 80)
    844. (vlax-invoke ws 'connect)
    845. ;;(vlax-invoke ws 'connect "www.yhhe.net" 80)
    846. (setq Url "http://www.ecranesoft.com/ape/book/fap/big5/pl/comx/winsock.html")
    847. (setq Cmd (strcat "GET " url " HTTP/1.0\r\n\r\n"))
    848. (vlax-invoke ws 'SendData cmd)
    849. (vlax-get ws 'BytesReceived)
    850. (setq data (vlax-make-variant vlax-vbString))
    851. (vlax-get ws 'state)
    852. (vlax-invoke ws 'getdata data vlax-vbString) ;;???
    853. (vlax-invoke ws 'close)
    854. (vlax-release-object ws)
    855. (princ)
    856. )
    857. ;;;方式3
    858. (defun C:getIp2 (/ path http url web objXML file str s1 s2)
    859. (setq path (strcat (getSpecialPath 1) "\\msxml6.dll"))
    860. (if (not xc-NODE_TEXT)
    861. (vlax-import-type-library
    862. :tlb-filename path
    863. :methods-prefix "xm-"
    864. :properties-prefix "xp-"
    865. :constants-prefix "xc-"
    866. )
    867. )
    868. (setq http (vlax-create-object "Msxml2.XMLHTTP")) ;Microsoft.XMLHTTP or MSXML2.ServerXMLHTTP
    869. (setq url "http://iframe.ip138.com/ic.asp") ;the link of URL 链接地址
    870. (xm-open http "GET" url :vlax-false) ;the open method 打开方式
    871. (xm-send http)
    872. (command "delay" 2000) ;暂停两秒,等待从网站返回信息
    873. (setq str (xp-get-responseText http)) ;get text from URL从地址中获取文本
    874. (setq s1 (vl-string-position (ascii "[") str))
    875. (setq s2 (vl-string-position (ascii "]") str))
    876. (princ "\nYour IP Address is:")
    877. (princ (substr str (+ s1 2) (- s2 s1 1)))
    878. (vlax-release-object http)
    879. ;;Get text from a Link
    880. (setq web (getstring "\nPlease enter URL:"))
    881. (setq objXML (vlax-create-object "MSXML2.ServerXMLHTTP"))
    882. (xm-open objXML "GET" web :vlax-false)
    883. (xm-send objXML)
    884. (setq str (XP-GET-RESPONSETEXT objXML))
    885. ;;(xp-get-responseXML http)
    886. ;;(xp-get-responseStream http)
    887. ;;(xp-get-responseBody http)
    888. (setq file (vl-filename-mktemp "c:\\1.html"))
    889. (setq file (open file "W"))
    890. (princ str file)
    891. (close file)
    892. (vlax-release-object objXML)
    893. (princ)
    894. )
    895. ;;;=============================================================
    896. ;;;Speak out your words. 让计算机开口说话,朗读文本之类
    897. ;;;=============================================================
    898. (defun c:voice(/ objTTS)
    899. (setq objTTS (vlax-create-object "SAPI.SpVoice"))
    900. (vlax-invoke objTTS 'speak "Hello,Welcome to China!")
    901. (vlax-release-object objTTS)
    902. (princ)
    903. )
    904. ;;;=============================================================
    905. ;;Get the screen size of your IE window 获取屏幕分辨率
    906. ;;;=============================================================
    907. (defun C:getscreenRes(/ IE screen)
    908. (setq IE (vlax-create-object "InternetExplorer.Application"))
    909. (vlax-invoke IE 'navigate "about:blank")
    910. (setq screen (vlax-get (vlax-get (vlax-get ie 'Document) 'parentWindow) 'screen))
    911. (princ (vlax-get screen 'height))
    912. (princ (vlax-get screen 'width))
    913. (vlax-release-object IE)
    914. (princ)
    915. )
    916. ;;;=============================================================
    917. ;;; 利用ADOBE读写二进制文件
    918. ;;;=============================================================
    919. (defun c:test (/ ARRAY BIN DAT DATA F L PATH S)
    920. ;;Read a Binary file
    921. (defun ReadBinary (FileName / stream arr)
    922. (setq stream (vlax-create-object "ADODB.Stream"))
    923. (vlax-put stream 'type 1) ;adTypeBinary
    924. (vlax-invoke stream 'open) ;adModeRead =1 adModeWrite =2 adModeReadWrite =3
    925. (vlax-invoke stream 'LoadFromFile filename)
    926. (setq Arr (vlax-invoke-method stream 'read (vlax-get stream 'SIZE)));read stream
    927. (vlax-invoke stream 'close)
    928. (vlax-release-object stream)
    929. (vlax-safearray->list (vlax-variant-value arr)) ;if a large size file ,it will take a long time in this step
    930. )
    931. ;;Write to a Binary file from a text stream
    932. (defun WriteBinary (FileName Array / stream)
    933. (setq stream (vlax-create-object "ADODB.Stream"))
    934. (vlax-put stream 'type 1) ;adTypeBinary
    935. (vlax-invoke stream 'open) ;adModeRead =1 adModeWrite =2 adModeReadWrite =3
    936. (vlax-invoke-method stream 'Write array) ;write stream
    937. (vlax-invoke stream 'saveToFile fileName 2) ;save
    938. (vlax-invoke stream 'close)
    939. (vlax-release-object stream)
    940. )
    941. (setq path (getfiled "Please select a binary file:" "c:/" "" 8 )) ;get file path
    942. (setq f (open "C:\\test.txt" "W"))
    943. (setq data (readBinary path))
    944. (princ data f)
    945. (close F)
    946. ;;(setq stream (vl-get-resource "test")) ;we can wrap this text file into .vlx file
    947. (setq f (open "C:\\test.txt" "R")) ;open for read
    948. (setq l "")
    949. (while (setq s (read-line f))
    950. (setq l (strcat l s))
    951. )
    952. (setq array (read l))
    953. (close f)
    954. (setq dat (vlax-make-safearray 17 (cons 0 (1- (length array))))) ;17 for unsigned char
    955. (vlax-safearray-fill dat array)
    956. (setq bin (vlax-make-variant dat))
    957. (writeBinary "C:\\test.jpg" bin) ;write binary file.
    958. )
    959. ;;;=============================================================
    960. ;;; WIA图像控件的利用
    961. ;;;=============================================================
    962. ;| 一下是VB的源码,可以参考
    963. Sub WIA_ARGB()
    964. Dim Img 'As ImageFile
    965. Dim IP 'As ImageProcess
    966. Dim v 'As Vector
    967. Dim i 'As Long
    968. Set Img = CreateObject("WIA.ImageFile")
    969. Set IP = CreateObject("WIA.ImageProcess")
    970. Img.LoadFile r & "\1.jpg"
    971. Set v = Img.ARGBData
    972. For i = 1 To v.Count Step 300
    973. v(i) = &HFFFF00FF 'opaque pink (A=255,R=255,G=0,B=255)
    974. Next
    975. '使用指定的位元組更新影像位元組。
    976. IP.Filters.Add IP.FilterInfos("ARGB").FilterID
    977. Set IP.Filters(1).Properties("ARGBData") = v
    978. Set Img = IP.Apply(Img)
    979. Img.SaveFile r & "\2.jpg"
    980. UserForm1.Image1.Picture = LoadPicture(r & "\1.jpg")
    981. UserForm1.Image2.Picture = LoadPicture(r & "\2.jpg")
    982. Kill r & "\2.jpg"
    983. End Sub
    984. ;;|;
    985. (defun c:img(/ path Img IPr vec cnt col old val fil i new)
    986. (setq path (strcat (getSpecialPath 1) "\\wiaaut.dll"))
    987. (if (not ic-actionEvent)
    988. (vlax-import-type-library
    989. :tlb-filename path
    990. :methods-prefix "im-"
    991. :properties-prefix "ip-"
    992. :constants-prefix "ic-"
    993. )
    994. )
    995. (setq Img (vlax-create-object "WIA.ImageFile"))
    996. (setq IPr (vlax-create-object "WIA.ImageProcess"))
    997. (im-loadfile Img "C:\\1.bmp")
    998. (setq vec (ip-get-ARGBData Img))
    999. (setq cnt (ip-get-count vec))
    1000. (setq col (vlax-make-variant -2147418367)) ;-2147418368 &HFFFF00FF
    1001. ;'opaque pink (A=255,R=255,G=0,B=255)
    1002. (setq i 1)
    1003. (repeat (/ cnt 3)
    1004. (setq old (ip-get-item vec i))
    1005. (setq val (vlax-variant-value old))
    1006. (setq val (- val))
    1007. (ip-put-item vec i val) ;4294967295
    1008. (setq i (+ i 3))
    1009. )
    1010. (setq fil (ip-get-Filters IPr))
    1011. (im-add fil (ip-get-filterID (ip-get-item (ip-get-filterinfos IPr) "ARGB")) 0)
    1012. (ip-put-value (ip-get-item (ip-get-Properties (ip-get-item fil 1)) "ARGBData") vec)
    1013. (setq new (im-apply IPr Img))
    1014. (im-savefile new "C:\\2.bmp")
    1015. (vlax-release-object Img)
    1016. (vlax-release-object IPr)
    1017. (vlax-release-object vec)
    1018. (vlax-release-object fil)
    1019. (vlax-release-object new)
    1020. )
    1021. ;;;=============================================================
    1022. ;;; Scriptlet.TypeLib的利用
    1023. ;;;=============================================================
    1024. ;;;Generate a GUID
    1025. (defun C:GUID (/ objSLTL str)
    1026. (setq objSLTL (vlax-create-object "Scriptlet.TypeLib"))
    1027. (setq str (vlax-get objSLTL 'GUID))
    1028. (vlax-release-object objSLTL)
    1029. str
    1030. )
    1031. ;;;=============================================================
    1032. ;;; XML的利用
    1033. ;;;=============================================================
    1034. (defun c:xml()
    1035. (setq path (strcat (getSpecialPath 1) "\\msxml6.dll")) ;;msxml3.dll
    1036. (if (not xc-NODE_TEXT)
    1037. (vlax-import-type-library
    1038. :tlb-filename path
    1039. :methods-prefix "xm-"
    1040. :properties-prefix "xp-"
    1041. :constants-prefix "xc-"
    1042. )
    1043. )
    1044. (setq path (strcat (getSpecialPath 1) "\\msadodc.ocx")) ;;msadox.dll
    1045. (if (not dc-ad3DBevel)
    1046. (vlax-import-type-library
    1047. :tlb-filename path
    1048. :methods-prefix "dm-"
    1049. :properties-prefix "dp-"
    1050. :constants-prefix "dc-"
    1051. )
    1052. )
    1053. (findfile "msado15.dll")
    1054. (if (not kc-adAddNew)
    1055. (vlax-import-type-library
    1056. :tlb-filename "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll"
    1057. :methods-prefix "km-"
    1058. :properties-prefix "kp-"
    1059. :constants-prefix "kc-"
    1060. )
    1061. )
    1062. )
    1063. (defun c:xml()
    1064. (setq wsMan (vlax-create-object "Wsman.Automation"))
    1065. (setq xmlFile (vlax-create-object "MSxml.DOMDocument"))
    1066. (setq session (vlax-invoke wsMan 'CreateSession))
    1067. (setq response (vlax-invoke session 'Get "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Service?Name=Spooler"))
    1068. (vlax-invoke xmlFile 'LoadXML response)
    1069. (vlax-invoke xmlfile 'save "C:/rawoutput.xml")
    1070. (vlax-release-object session)
    1071. (vlax-release-object xmlfile)
    1072. (vlax-release-object wsMan)
    1073. )
    1074. ;;;=============================================================
    1075. ;;; 获取外网地址--正则表达式和ServerXMLHTTP的综合运用
    1076. ;;;=============================================================
    1077. (defun C:GetIP (/ htt regexp text mathes address item0)
    1078. (setq Http (vlax-create-object "Msxml2.ServerXMLHTTP"))
    1079. (vlax-invoke-method http 'open "GET" "http://iframe.ip138.com/ic.asp")
    1080. (vlax-invoke-method http 'send)
    1081. (setq text (vlax-get-property http 'responseText))
    1082. (setq RegExp (vlax-create-object "VBScript.RegExp"))
    1083. (vlax-put-property RegExp 'pattern "((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))")
    1084. (vlax-put-property RegExp 'IgnoreCase 1)
    1085. (vlax-put-property RegExp 'Global 1)
    1086. (setq matches (vlax-invoke-method RegExp 'Execute text))
    1087. (setq item0 (vlax-get-property matches 'item 0))
    1088. (setq Address (vlax-get-property item0 'value))
    1089. (vlax-release-object item0)
    1090. (vlax-release-object matches)
    1091. (vlax-release-object regexp)
    1092. (vlax-release-object http)
    1093. (alert (strcat "你的IP地址是:" address))
    1094. (princ)
    1095. )
    1096. ;;;=============================================================
    1097. ;;; 其他控件的利用
    1098. ;;;=============================================================
    1099. ;;; ADO Recordset对象用于容纳一个来自数据库表的记录集。
    1100. ;;; 一个Recordset对象由记录和列(字段)组成。在 ADO 中,此对象是
    1101. ;;; 最重要且最常用于对数据库的数据进行操作的对象。
    1102. (defun c:kk(/ rr)
    1103. (setq rs (vlax-create-object "Adodb.recordset"))
    1104. (vlax-dump-object rs T)
    1105. (vlax-release-object rs)
    1106. (princ)
    1107. )
    1108. ;;; 利用DOS控制台做些事情,譬如列举文件目录到某个文本文件,等等.
    1109. (defun C:Cout(/ wsh exe)
    1110. (setq wsh (vlax-create-object "WScript.shell"))
    1111. (setq exe (vlax-invoke wsh 'exec "ipconfig"))
    1112. (wm-run wsh "cmd.exe /C dir c:\\temp\\*.* /a /s >>c:\\1.txt")
    1113. (alert (vlax-invoke (vlax-get exe 'stdout) 'readAll))
    1114. (setq exe (wm-exec wsh "cmd.exe /C dir c:\\temp\\*.* /a /s"))
    1115. (princ (vlax-invoke (vlax-get exe 'stdout) 'readAll))
    1116. (vlax-release-object exe)
    1117. (vlax-release-object wsh)
    1118. )
    1119. ;;; 一些综合应用.
    1120. (defun c:test()
    1121. (setq path (strcat (GetSpecialPath 1) "\\vbscript.dll")) ;"\\Wscript.exe"
    1122. (if (not RM-abs)
    1123. (vlax-import-type-library
    1124. :tlb-filename path
    1125. :methods-prefix "Rm-"
    1126. :properties-prefix "Rp-"
    1127. :constants-prefix "Rc-"
    1128. )
    1129. )
    1130. (setq regExp (vlax-create-object "Vbscript.RegExp"))
    1131. (setq wsh (vlax-create-object "wscript.shell"))
    1132. (setq scr (vlax-create-object "ScriptControl"))
    1133. (vlax-put scr 'language "VBS")
    1134. (vlax-make-variant 1)
    1135. (vlax-create-object "vbscript.GlobalObj")
    1136. (rm-abs (vlax-make-variant 1) -1)
    1137. (rm-inputbox scr "Dim x As VBScript_Global.GlobalObj")
    1138. (SM-EXECUTESTATEMENT scr "Function sss()
    1139. Dim x As new VBScript_Global.GlobalObj
    1140. end Function")
    1141. (princ (readStream "C:\\delete.dcl" T))
    1142. (vlax-release-object regExp)
    1143. )
    1144. ;;;FireWall等等控件均可测试。
    1145. (defun FHQ()
    1146. ;;(vlax-create-object "ToolsObject.TelnetTool")
    1147. ;;(vlax-create-object "RCBdyCtl.Setting")
    1148. (setq fwObj (vlax-create-object "HNetCfg.FwMgr"))
    1149. )