Author Topic: Call windows API functions in VLISP  (Read 3509 times)

0 Members and 1 Guest are viewing this topic.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Call windows API functions in VLISP
« on: December 16, 2010, 06:28:07 AM »
I found a way to use windows API  functions in vlisp.Now you can see the effect:
My question is : Is there another better way to  applicate it? Any problems in here?
Code: [Select]
;;;Call windows API with "Excel.application"
(defun c:API (/ wsh oEx oBk Mod str)
  (vl-registry-write "HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\11.0\\Excel\\Security\\AccessVBOM" "1 REG_DWORD")
  (vl-registry-write "HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\12.0\\Excel\\Security\\AccessVBOM" "1 REG_DWORD")
  (vl-registry-write "HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\14.0\\Excel\\Security\\AccessVBOM" "1 REG_DWORD")
  
  (setq oEx (vlax-create-object "excel.application"))
  (setq obk (vlax-invoke (vlax-get oEx 'workbooks) 'add))
  (setq mod (vlax-invoke (vlax-get (vlax-get obk 'VBProject) 'VBComponents) 'add 1))
  (setq str
"Private Declare Function SetCursorPos Lib \"user32\" (ByVal x As Long, ByVal y As Long) As Long
Declare Function GetForegroundWindow Lib \"user32\" Alias \"GetForegroundWindow\" () As Long
Declare Function GetWindowDC Lib \"user32\" Alias \"GetWindowDC\" (ByVal hwnd As Long) As Long
Declare Sub Sleep Lib \"kernel32\" (ByVal dwMilliseconds As Long)
'Declare Function GetDC Lib \"user32\" Alias \"GetDC\" (ByVal hwnd As Long) As Long
Declare Function Ellipse Lib \"gdi32\" Alias \"Ellipse\" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

Sub MyMacro(x as Long, y as Long)
SetCursorPos 0, 0
Ellipse GetWindowDC(GetForegroundWindow),x,y,x+800,y+600
Sleep(2000)
End Sub"
  )
  (vlax-invoke (vlax-get mod 'CodeModule) 'AddFromString str)
  (vlax-invoke oEx 'run "MyMacro" 200 100)
  (vlax-put oEx 'DisplayAlerts 0)
  (vlax-invoke obk 'close)
  (vlax-invoke oEx 'quit)

  (vlax-release-object mod)
  (vlax-release-object obk)
  (vlax-release-object oEx)
  
  (princ)
)
« Last Edit: December 16, 2010, 08:40:35 AM by highflybird »
I am a bilingualist,Chinese and Chinglish.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Call windows API functions in VLISP
« Reply #1 on: December 16, 2010, 07:17:23 AM »
Ack! a lisp routine with a GDI leak! you lispers cease to amaze  8-)

Quote
After painting is complete, the ReleaseDC function must be called to release the device context. Not releasing the window device context has serious effects on painting requested by applications.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: Call windows API functions in VLISP
« Reply #2 on: December 16, 2010, 08:51:22 AM »
Ack! a lisp routine with a GDI leak! you lispers cease to amaze  8-)

Quote
After painting is complete, the ReleaseDC function must be called to release the device context. Not releasing the window device context has serious effects on painting requested by applications.
I was wondering  memory Leak problems.
Thanks your advice.Daniel.
Yes ,we will be cautious in these kinds of programming. But sometimes it's very interesting.
I am a bilingualist,Chinese and Chinglish.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: Call windows API functions in VLISP
« Reply #3 on: December 26, 2010, 01:45:11 AM »

Here is a sister's thread. http://www.theswamp.org/index.php?topic=36405.0
the question is :

1.How can I know the location of a VBA project?
if I created a vba project ,but  I didn't save it, so if I want to unload it,  then run :
(Command "vbaunload")
Unload VBA Project: acadproject
Invalid file name.

actually, the correct name is : Global1 ,etc.,how can I know its name?

2. when I unload a VBA project ,it will prompt "Save changes to Global1?",how  do I send a key "N"?

Use wscript.shell? sendkeys? But sometimes it doesn't work.
« Last Edit: December 26, 2010, 01:49:43 AM by highflybird »
I am a bilingualist,Chinese and Chinglish.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: Call windows API functions in VLISP
« Reply #4 on: December 26, 2010, 07:40:29 PM »
I solved these problems ,thank my friend nonsmall

But the sendkeys problem can't be avoided.
« Last Edit: December 27, 2010, 07:28:07 AM by highflybird »
I am a bilingualist,Chinese and Chinglish.