Не искам да използвам Python под линукс, а под Win. И тъй като Python не може да се компилира до байт-код (има ембедери и пакетиращи програми, които правят exe-та) искам да използвам лесен за писане език, като AutoIt или друг компилируем език, като само добавя в директорията една DLL-библиотека и да използвам възможностите на някой от динамичните езици (в случая Python). Отделно от това, кода на Python ще ми е полезен, ако реша да направя програмата преносима, така че с малки промени да мога да я напасна да работи под Linux.
Намерих примерен код на един пич Валери Иванов, който прави почти каквото искам - закача Python25.dll и изпълнява комади към интерпретатора. Използва DllCall:
;============================
;APy © Valery Ivanov, 2006
#include <GUIConstants.au3>
#include <windowsconstants.au3>
#include <Array.au3>
Global $Title = "APy - autoexecute python lines"
local $res, $msg, $hWnd
;====================
; Python DLL variables
Global $PyDLL, $PyDLLPath = ".\python25.dll"
Global $Edit, $hSB
$hWnd = GUICreate($Title, 600, 500)
GUISetBkColor (0xFFEFD5)
;-------------
;Buttons
$Load = GUICtrlCreateButton("LoadFile", 50, 10, 100, 25)
$RunPython = GUICtrlCreateButton("Run", 150, 10, 100, 25)
;Create an edit box with no text in it
$Edit = GUICtrlCreateEdit("", 0, 50, 600, 430)
; See content of example below
;Example($Edit)
;StatusBar
$hSB = GuiCtrlSBarCreate($hWnd, 600, "50;40")
PythonDll()
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
;--------------------------------------------------------
Case $msg = $Load
$Filename = FileOpenDialog("Choose Python Script", @ScriptDir, "All Files (*.py)", "1")
$Text = FileRead($Filename)
GUICtrlSetData($Edit,$Text)
Case $msg = $RunPython
RunPython($Edit)
EndSelect
WEnd
;-----------------------------------
func PythonDll()
local $res, $result
$PyDLL = DllOpen ($PyDLLPath)
if @error then MsgBox(48, "", "Can't open PythonXX.dll")
$res = DllCall($PyDLL, "none", "Py_Initialize")
if @error then MsgBox(48, "", "Can't init PythonXX.dll")
endfunc
;-----------------------------------
; StatusBar creation
func GuiCtrlSBarCreate($hWnd, $Width, $Parts)
local $SB_SETPARTS = 0x404, $SBARS_SIZEGRIP = 0x0100, $SB_SETICON = 0x40F
local $i, $res, $hSB, $stParts, $hIcon, $W = 0
$res = DllCall("comctl32.dll", "long", "CreateStatusWindow", "long", BitOR($WS_CHILD, $WS_VISIBLE,$SBARS_SIZEGRIP), "str", "", "hwnd", $hWnd, "int", 0)
$hSB = $res[0]
$Part = StringSplit($Parts,";")
$dscParts = "int"
for $i = 1 to $Part[0] - 1
$dscParts &= ";int"
next
$stParts = DllStructCreate($dscParts)
$W = 0
for $i = 1 to $Part[0]
$W += $Width*$Part[$i]/100
DllStructSetData($stParts,$i,$W)
next
$res = DllCall("user32.dll", "long", "SendMessage", "hwnd", $hSB, "int", $SB_SETPARTS, "int", $Part[0], "ptr", DllStructGetPtr($stParts))
$stParts = 0
;Icon set
$hIcon = DllStructCreate("int")
$res = DllCall("shell32.dll", "int", "ExtractIconEx", "str", @SystemDir & "\shell32.dll", "int", 1, "hwnd", 0, "ptr", DllStructGetPtr($hIcon), "int", 1)
$res = DllCall("user32.dll", "int", "SendMessage", "hwnd", $hSB, "int", $SB_SETICON, "int", 0, "hwnd", DllStructGetData($hIcon, 1))
DllCall("user32.dll", "int", "DestroyIcon", "hwnd", DllStructGetPtr($hIcon))
$res = DllCall("shell32.dll", "int", "ExtractIconEx", "str", @SystemDir & "\shell32.dll", "int", 21, "hwnd", 0, "ptr", DllStructGetPtr($hIcon), "int", 1)
$res = DllCall("user32.dll", "int", "SendMessage", "hwnd", $hSB, "int", $SB_SETICON, "int", 1, "hwnd", DllStructGetData($hIcon, 1))
DllCall("user32.dll", "int", "DestroyIcon", "hwnd", DllStructGetPtr($hIcon))
$hIcon = 0
Return $hSB
endfunc
;-----------------------------------
func UpdateSB($PartNumber,$Text)
local $SB_SETTEXT = 0x401
DllCall("user32.dll", "long", "SendMessage", "hwnd", $hSB, "int", $SB_SETTEXT, "int", $PartNumber, "str", $Text)
endfunc
;-----------------------------------
func PythonExec($Text)
local $res
$res = DllCall($PyDLL, "int:cdecl", "PyRun_SimpleString","str",$Text)
if @error then MsgBox(48, "", "Can't execute command")
_ArrayDisplay($res)
return $res[0]
endfunc
;-----------------------------------
func RunPython($Edit)
local $LineCount, $res, $result, $Line
$Text = GUICtrlRead($Edit)
$Line = StringSplit($Text,@LF)
for $i = 1 to $Line[0]
$Command = StringStripCR($Line[$i])
if $Command <> "" then
UpdateSB(0,"Try Command: " & $Command)
UpdateSB(1,"Running....")
$res = PythonExec($Command)
if $res then
UpdateSB(1,"Result: command failed")
else
UpdateSB(1,"Result: command succeded")
endif
MsgBox(0,"RunPython","Executed command:" & $Command)
endif
next
endfunc