# AMathematica

## Recommended Posts

This is an example for Mathematica by Wolfram

It can't show pictures and symbols but maybe this will be in future updates.

File AMathematica.au3

```;=======================================================
; AMathematica by Valery Ivanov, 14 April 2009
;  for Mathematica by Wolfram -> http://www.wolfram.com/
;  **** Intended by
;      VCFE.C - Visual C++ Mathematica Front End
;  ****
;=======================================================
; IMPORTANT NOTE:
; 1. Mathematica should be installed
; 2. Variable \$LinkName = FULLPATH to MathKernel.exe
;   Here is installation Mathematica 5.0 on drive D:
;   Change path for your installation:
;=======================================================
Global \$LinkName = "d:\Program Files\Wolfram Research\Mathematica\5.0\MathKernel.exe"
;=======================================================
; Note:
; 1. Under Mathematica >= 6.0 function MLDisownString is obsolate.
;   Replace it to MLReleaseString like this
; \$res = DllCall(\$MLDLL, "int", "MLDisownString", "ptr", \$mlinkp, "ptr", \$stSymbolPtr)
; to
; \$res = DllCall(\$MLDLL, "int", "MLReleaseString", "ptr", \$mlinkp, "ptr", \$stPtr)
;   See text of func MLGetString below
; 2. I don't know how to make first evaluation without \$FirstEvaluation
;=======================================================
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>

Global \$InitialEvaluation = "Coefficient[(d - 4 + 3y + 4y^2)^2, y^3]"

;from WinUser.h
Global Const \$PM_NOREMOVE = 0x0000
Global Const \$PM_REMOVE = 0x0001

;ML messages
Global Const \$MLTerminateMessage = 1
Global Const \$MLInterruptMessage = 2
Global Const \$MLAbortMessage = 3
Global Const \$MLEndPacketMessage = 4
Global Const \$MLSynchronizeMessage = 5
Global Const \$MLImDyingMessage = 6
Global Const \$MLWaitingAcknowledgment = 7
Global Const \$MLMarkTopLevelMessage = 8
Global Const \$MLFirstUserMessage = 128
Global Const \$MLLastUserMessage = 255

;Package codes returned from ML
Global Const \$ILLEGALPKT      = 0
Global Const \$INPUTPKT      = 1
Global Const \$TEXTPKT        = 2
Global Const \$RETURNPKT    = 3
Global Const \$RETURNTEXTPKT   = 4
Global Const \$MESSAGEPKT      = 5
Global Const \$CALLPKT        = 7
Global Const \$INPUTNAMEPKT  = 8
Global Const \$OUTPUTNAMEPKT   = 9
Global Const \$SYNTAXPKT    = 10
Global Const \$DISPLAYPKT      = 11
Global Const \$DISPLAYENDPKT   = 12
Global Const \$EVALUATEPKT    = 13
Global Const \$ENTERTEXTPKT  = 14
Global Const \$ENTEREXPRPKT  = 15
Global Const \$RETURNEXPRPKT   = 16
Global Const \$SUSPENDPKT      = 17
Global Const \$RESUMEPKT    = 18
Global Const \$BEGINDLGPKT    = 19
Global Const \$ENDDLGPKT    = 20
Global Const \$INPUTSTRPKT    = 21
Global Const \$FIRSTUSERPKT  = 128
Global Const \$LASTUSERPKT    = 255

Global \$MLDLL
Global \$mlenvp = 0

Global \$Initstring = ""
\$Initstring &= '\$Display := OpenTemporary[]; \$DisplayFunction = (Module[{tmpstream = \$Display, tmpfile}, '
\$Initstring &= 'tmpfile = tmpstream[[1]]; Close[tmpstream]; '
\$Initstring &= 'DisplayEndPacket[tmpfile]]]; #1)&; SetOptions[{ContourGraphics, '
\$Initstring &= 'ContourPlot, DensityGraphics, DensityPlot, Graphics, Graphics3D, '
\$Initstring &= 'GraphicsArray, ListContourPlot, ListDensityPlot, ListPlot, '
\$Initstring &= 'ListPlot3D, ParametricPlot, ParametricPlot3D, Plot, Plot3D, '
\$Initstring &= 'SurfaceGraphics}, AspectRatio -> 2]; SetOptions[ "stdout", '
\$Initstring &= 'PageWidth->65]; Format[LineBreak[_]]:= "";' & @Cr & ' '
\$Initstring &= 'Format[Continuation[_]]:= ""; SetAttributes[MessagePacket, HoldFirst];'

;Main Window handle
Global \$hWnd
;Controls
Global \$LabelInput, \$LabelInputPrompt, \$EditInput
Global \$LabelOutput, \$LabelOutputPrompt, \$EditOutput, \$hEditOutput
Global \$LabelMessages, \$EditMessages
Global \$LabelMessages, \$EditMessages

Global \$EvaluateString = ""
Global \$ExpressionExpected = 0
Global \$StringExpected = 1
Global \$InputMode = \$ExpressionExpected

Global \$FirstFlag = 1

;MsgBox(0,"Initstring",\$Initstring)

\$MLDLL = DllOpen("ml32i2.dll")

\$hWnd = GUICreate("AMathematica © Valery Ivanov, 14 April 2009", 545,422,154,203, 0x00CF0000)

\$LabelInput = GUICtrlCreateLabel("Input:", 8,0,30,16,0x50020000,0x00000004)
\$LabelInputPrompt = GUICtrlCreateLabel("", 47,0,53,16,0x50020000,0x00000004)
GUICtrlSetColor(-1,0x0000ff)
\$EditInput = GUICtrlCreateEdit(\$InitialEvaluation, 8,16,525,57,0x50211084,0x00000204)
GUICtrlSetFont (-1,9,700,0,"Courier New")

\$LabelOutput = GUICtrlCreateLabel("Output:", 8,73,38,16,0x50020000,0x00000004)
\$LabelOutputPrompt = GUICtrlCreateLabel("", 47,73,53,16,0x50020000,0x00000004)
GUICtrlSetColor(-1,0x0000ff)
GUICtrlSetFont (-1,9,0,0,"Courier New")
\$EditOutput = GUICtrlCreateEdit('', 8,89,525,55,0x50211084,0x00000204)
\$hEditOutput = GUICtrlGetHandle(\$EditOutput)

\$Graphics = GUICtrlCreateLabel("Display:",8,145,53,16,0x50020000,0x00000004)
;\$GraphicWindowPic = GUICtrlCreatePic("", 8,161,339,190)
\$GraphicWindow = GUICtrlCreateLabel("", 8,161,339,190,0x50000006,0x00000204)

\$LabelMessages = GUICtrlCreateLabel("Messages:",353,145,145,16,0x50020000,0x00000004)
\$EditMessages = GUICtrlCreateEdit('', 354,161,177,190,0x50211084,0x00000204)
GUICtrlSetColor(-1,0xff0000)

\$Evaluate = GUICtrlCreateButton("&Evaluate",8,358,86,26,0x50010001,0x00000004)
GUICtrlSetState (\$Evaluate, \$GUI_DISABLE)
\$Abort = GUICtrlCreateButton("&Abort",96,358,86,26,0x58010000,0x00000004)
GUICtrlSetState (\$Abort, \$GUI_DISABLE)
\$Connect = GUICtrlCreateButton("&Connect",359,358,86,26,0x50010000,0x00000004)
\$Quit = GUICtrlCreateButton("&Quit",447,358,86,26,0x50010000,0x00000004)

\$FirstEvaluation = 1

GUISetState()

; Start the main loop
While 1
\$Msg = GUIGetMsg()
Select
Case \$Msg = \$GUI_EVENT_CLOSE
Disconnect()
Exit
Case \$Msg = \$Evaluate
if \$State <> "&Connect" then
;MsgBox(0,"Before Evaluate","\$EvaluateString = " & \$EvaluateString)
if \$EvaluateString <> "" then
GUICtrlSetState (\$Connect, \$GUI_DISABLE)
GUICtrlSetState (\$Evaluate, \$GUI_DISABLE)
GUICtrlSetState (\$Abort, \$GUI_ENABLE)
GUICtrlSetState (\$Quit, \$GUI_DISABLE)
if \$FirstEvaluation = 1 then
\$FirstEvaluation = 0
Evaluate(\$EvaluateString)
endif
Evaluate(\$EvaluateString)
GUICtrlSetState (\$Quit, \$GUI_ENABLE)
GUICtrlSetState (\$Abort, \$GUI_DISABLE)
GUICtrlSetState (\$Evaluate, \$GUI_ENABLE)
GUICtrlSetState (\$Connect, \$GUI_ENABLE)
endif
endif
Case \$Msg = \$Abort
MLPutMessage(\$MLAbortMessage)
; LookFor()
Case \$Msg = \$Connect
Connect()
Case \$Msg = \$Quit
MLPutMessage(\$MLAbortMessage);
Disconnect()
Exit
EndSelect
WEnd

;==========================================
func Connect()
local \$connectstring = "&Connect"
local \$disconnectstring = "&Disconnect"
local \$currentstring

;MsgBox(0,"","\$currentstring = " & \$currentstring)
if \$currentstring <> \$connectstring then
Disconnect()
GUICtrlSetState (\$Evaluate, \$GUI_DISABLE)
GUICtrlSetData (\$Connect, \$connectstring)
else
GUICtrlSetState (\$Connect, \$GUI_DISABLE)
GUICtrlSetState (\$Quit, \$GUI_DISABLE)
TryConnect()
Evaluate("")
MLPutFunction("EvaluatePacket", 1)
MLPutFunction("ToExpression", 1)
MLPutFunction(\$InitString, 1)
Evaluate(0);
GUICtrlSetState (\$Evaluate, \$GUI_ENABLE)
GUICtrlSetData (\$Connect, \$disconnectstring)
endif
GUICtrlSetState (\$Connect, \$GUI_ENABLE)
GUICtrlSetState (\$Quit, \$GUI_ENABLE)
endif
endfunc

;==========================================
func TryConnect()
local \$stErr

\$mlenvp = DllCall(\$MLDLL, "ptr", "MLInitialize", "ptr", 0)
if not @error then
\$mlenvp = \$mlenvp[0]
else
;MsgBox(0,"Error","MLInitialize can't be called...")
return
endif
\$stErr = DllStructCreate("long")
\$mlinkp = DllCall(\$MLDLL, "ptr", "MLOpenString", "ptr", \$mlenvp, "str", \$LaunchString, "long_ptr", DllStructGetPtr(\$stErr))
\$Err  = DllStructGetData(\$stErr, 1)
if not @error then
else
;MsgBox(0,"Error","MLInitialize can't be called..." & " \$Err = " & \$Err)
endif
\$stErr = 0
endfunc

;==========================================
func Disconnect()
if \$mlenvp then \$res = DllCall(\$MLDLL, "int", "MLDeinitialize", "ptr", \$mlenvp)
\$mlenvp = 0
endfunc

;==========================================
local \$packet
\$packet = MLNextPacket()
while \$packet <> \$RETURNPKT
\$packet = MLNextPacket()
wend
endfunc

;==========================================
func Evaluate(\$InputString)
local \$ExpressionExpected = 0
local \$StringExpected = 1
local \$FirstPiece = 1
local \$DialogLevel
local \$OutputString = ""
local \$SyntaxErrorPosition = -1
local \$OutputString = ""
local \$Done

if IsString(\$InputString) then
if \$InputMode = \$ExpressionExpected then
MLPutFunction("EnterTextPacket", 1)
else
MLPutFunction("TextPacket", 1)
\$InputMode = \$ExpressionExpected
endif
MLPutByteString(\$InputString)
MLEndPacket()
if CheckError() then return 0
endif

GUICtrlSetData(\$EditOutput, "")

\$Done = 0
while Not \$Done
while 1
if PeekMessage() then MsgBox(0,"","PeekMessage")
sleep(10)
MLFlush()
wend

\$Packet = MLNextPacket()
Select
case \$Packet = \$ILLEGALPKT
if ((Not MLClearError()) or (Not MLNewPacket())) then
\$Done = 1
endif

case ((\$Packet = \$INPUTNAMEPKT) or (\$Packet = \$INPUTPKT))
if MLGetString(\$OutputString) then
GUICtrlSetData (\$LabelInputPrompt, \$OutputString)
endif
\$Done = 1

case \$Packet = \$TEXTPKT
if MLGetString(\$OutputString) then
GUICtrlSetData (\$EditOutput, GUICtrlRead(\$EditOutput) & ReplaceSlashZeroOneTwo(\$OutputString) & @CrLf)
endif
\$Done = 1
endif

case \$Packet = \$OUTPUTNAMEPKT
if MLGetString(\$OutputString) then
GUICtrlSetData (\$LabelOutputPrompt, \$OutputString)
endif

case \$Packet = \$RETURNPKT
\$Done = 1

case \$Packet = \$RETURNTEXTPKT
if MLGetString(\$OutputString) then
\$OutputString = ReplaceSlashZeroOneTwo(\$OutputString)
GUICtrlSetData (\$EditOutput, GUICtrlRead(\$EditOutput) & \$OutputString  & @CrLf)
endif

case \$Packet = \$DISPLAYPKT
if \$FirstPiece = 1 then
GUICtrlSetData (\$GraphicWindow, "")
\$FirstPiece = 0
endif

if MLGetString(\$OutputString) then
;if \$OutputString then
;   GUICtrlSetData (\$GraphicWindow, GUICtrlRead(\$GraphicWindow) & @CrLf & \$OutputString)
;endif
endif

case \$Packet = \$DISPLAYENDPKT
if MLGetString(\$OutputString) then
endif
\$FirstPiece = 1

case \$Packet = \$SYNTAXPKT
\$SyntaxErrorPosition = MLGetInteger()
GUICtrlSetData(\$EditOutput, GUICtrlRead(\$EditOutput) & \$InputString & "Syntax Error! Position: " & \$SyntaxErrorPosition)

case \$Packet = \$INPUTSTRPKT
if MLGetString(\$OutputString) then
GUICtrlSetData (\$LabelInputPrompt, \$OutputString)
endif
\$InputMode = \$StringExpected;
\$Done = 1

if MLGetString(\$OutputString) then
\$Inputstring = \$OutputString
endif
\$InputMode = \$StringExpected;
else
GUICtrlSetData (\$LabelInputPrompt, \$InputString)
\$Done = 1
endif

case \$Packet = \$MESSAGEPKT
MLGetSymbol(\$OutputString)

MLGetString(\$OutputString)
GUICtrlSetData(\$EditMessages, GUICtrlRead(\$EditMessages)& @CrLf & "::" & @CrLf & \$OutputString & @CrLf)

case \$Packet = \$SUSPENDPKT
GUICtrlSetData (\$EditOutput, "***SUSPENDED***" & @CrLf)
MLNewPacket()
\$Done = 1

case \$Packet = \$RESUMEPKT
GUICtrlSetData (\$EditOutput, "***RESUMED***" & @CrLf)
MLNewPacket()

case \$Packet = \$BEGINDLGPKT
\$DialogLevel = MLGetInteger()
GUICtrlSetData(\$EditOutput, GUICtrlRead(\$EditOutput) & @CrLf & "Entering Dialog: " & \$DialogLevel)

case \$Packet = \$ENDDLGPKT
\$DialogLevel = MLGetInteger()
\$DialogLevel -= 1
GUICtrlSetData(\$EditOutput, GUICtrlRead(\$EditOutput) & @CrLf & "Leaving Dialog: " & \$DialogLevel)
endselect

MLNewPacket()
wend
return 1
endfunc

;===================================
func CheckError()
local \$result

\$result = MLError()
if \$result then
GUICtrlSetData (\$EditMessages, MLErrorMessage())
MLClose()
endif
return \$result
endfunc

;===================================
func MLClose()
local \$res
\$res = DllCall(\$MLDLL, "int", "MLClose", "ptr", \$mlinkp)
endfunc

;===================================
func MLErrorMessage()
local \$res, \$ErrorMsg
\$res = DllCall(\$MLDLL, "str", "MLErrorMessage", "ptr", \$mlinkp)
If @error then
;MsgBox(0,"Error","MLErrorMessage")
return ""
endif
return \$res[0]
endfunc

;===================================
func MLError()
local \$res
\$res = DllCall(\$MLDLL, "long", "MLError", "ptr", \$mlinkp)
If @error then
;MsgBox(0,"Error","MLError")
return 0
endif
return \$res[0]
endfunc

;===================================
func MLNextPacket()
local \$res
\$res = DllCall(\$MLDLL, "int", "MLNextPacket", "ptr", \$mlinkp)
If @error then
;MsgBox(0,"Error","MLNextPacket")
return 0
endif
;   MsgBox(0,"Success","MLNextPacket: \$PKTCode = " & \$res[0])
return \$res[0]
endfunc

;===================================
func MLPutMessage(\$MLMessage)
local \$res
\$res = DllCall(\$MLDLL, "int", "MLPutMessage", "ptr", \$mlinkp, "long", \$MLMessage)
if @error then
;MsgBox(0,"Error","MLPutMessage: \$MLMessage = " & \$MLMessage)
return 0
endif
return \$res[0]
endfunc

;===================================
func MLGetInteger()
local \$res, \$stInt, \$Int
\$stInt = DLLStructCreate("int")
\$res = DllCall(\$MLDLL, "int", "MLGetInteger", "ptr", \$mlinkp, "ptr", DllStructGetPtr(\$stInt))
If @error then
;MsgBox(0,"Error","MLGetInteger")
return 0
endif
\$Int = DllStructGetData(\$stInt, 1)
\$stInt = 0
return \$Int
endfunc

;===================================
func MLGetString(ByRef \$OutputString)
local \$stStringPtr, \$stString
local \$res, \$ret

\$stStringPtr  = DLLStructCreate("long_ptr StringPtr")
\$res = DllCall(\$MLDLL, "int", "MLGetString", "ptr", \$mlinkp, "ptr", DllStructGetPtr(\$stStringPtr))
If @error then MsgBox(0,"Error","MLGetString")
\$ret = \$res[0]

\$StringPtr  = DllStructGetData(\$stStringPtr,"StringPtr")
;MsgBox(0,"MLGetString","\$StringPtr = " & \$StringPtr)
\$stString = DLLStructCreate("char Text[2048]",\$StringPtr)
\$OutputString = DllStructGetData(\$stString,"Text")
;  MsgBox(0,"Output","MLGetString: \$OutString = " & \$OutString)

if \$ret then
;   MsgBox(0,"MLGetString","MLGetString: \$OutputString = " & \$OutputString)
\$res = DllCall(\$MLDLL, "int", "MLDisownString", "ptr", \$mlinkp, "ptr", \$stStringPtr)
; after version 6.0
; \$res = DllCall(\$MLDLL, "int", "MLReleaseString", "ptr", \$mlinkp, "ptr", \$stPtr)
If @error then MsgBox(0,"Error","MLReleaseString")
endif

\$stString = 0
\$stStringPtr = 0
return \$ret
endfunc

;===================================
func MLGetSymbol(ByRef \$OutputString)
local \$stSymbolPtr, \$stSymbol
local \$res, \$OutSymbol, \$ret

\$stSymbolPtr  = DLLStructCreate("long_ptr SymbolPtr")
\$res = DllCall(\$MLDLL, "int", "MLGetSymbol", "ptr", \$mlinkp, "ptr", DllStructGetPtr(\$stSymbolPtr))
If @error then MsgBox(0,"Error","MLGetSymbol")
\$ret = \$res[0]

\$SymbolPtr  = DllStructGetData(\$stSymbolPtr,"SymbolPtr")
;MsgBox(0,"MLGetSymbol","\$SymbolPtr = " & \$SymbolPtr)
\$stSymbol = DLLStructCreate("char Text[2048]",\$SymbolPtr)
\$OutputString  = DllStructGetData(\$stSymbol,"Text")
;  MsgBox(0,"Output","MLGetSymbol: \$OutSymbol = " & \$OutSymbol)

if \$ret then
\$res = DllCall(\$MLDLL, "int", "MLDisownString", "ptr", \$mlinkp, "ptr", \$stSymbolPtr)
; after version 6.0
; \$res = DllCall(\$MLDLL, "int", "MLReleaseString", "ptr", \$mlinkp, "ptr", \$stPtr)
If @error then MsgBox(0,"Error","MLReleaseString")
endif

\$stSymbol = 0
\$stSymbolPtr = 0

return \$ret
endfunc

;===================================
func MLGetByteString()
local \$res, \$stStringPtr, \$stString, \$OutString, \$length

\$stStringPtr = DllStructCreate("ptr")
\$res = DllCall(\$MLDLL, "int", "MLGetByteString", "ptr", \$mlinkp, "ptr", DllStructGetPtr(\$stStringPtr), "int_ptr", DllStructGetPtr(\$length))
If @error then MsgBox(0,"Error","MLGetByteString")

\$stString = DllStructCreate("char OutString[" & \$length+1 & "]",\$stStringPtr)
\$OutString = DllStructGetData(\$stString, "OutString")
;MsgBox(0,"Output","MLGetString: \$OutString = " & \$OutString)

if \$OutString then \$res = DllCall(\$MLDLL, "int", "MLDisownString", "ptr", \$mlinkp, "str", \$OutputString)
; after version 6.0
; \$res = DllCall(\$MLDLL, "int", "MLReleaseByteString", "ptr", \$mlinkp, "str", \$OutString, "int", \$length)

If @error then MsgBox(0,"Error","MLReleaseByteString")

\$stString = 0
\$stStringPtr = 0
return \$OutString
endfunc

;===================================
func MLPutFunction(\$FunctionName, \$argc)
local \$res
\$res = DllCall(\$MLDLL, "int", "MLPutFunction", "ptr", \$mlinkp, "str", \$FunctionName, "long", \$argc)
If @error then
;MsgBox(0,"Error","MLPutFunction")
return 0
endif
return \$res[0]
endfunc

;===================================
func MLPutByteString(\$String)
local \$res
\$res = DllCall(\$MLDLL, "int", "MLPutByteString", "ptr", \$mlinkp, "str", \$String, "long", StringLen(\$String))
If @error then
;MsgBox(0,"Error","MLPutByteString")
return 0
endif
return \$res[0]
endfunc

;===================================
func MLEndPacket()
local \$res
\$res = DllCall(\$MLDLL, "int", "MLEndPacket", "ptr", \$mlinkp)
If @error then
;MsgBox(0,"Error","MLEndPacket")
return 0
endif
return \$res[0]
endfunc

;===================================
func MLNewPacket()
local \$res
\$res = DllCall(\$MLDLL, "int", "MLNewPacket", "ptr", \$mlinkp)
If @error then
;MsgBox(0,"Error","MLNewPacket")
return 0
endif
return \$res[0]
endfunc

;===================================
func MLFlush()
local \$res
\$res = DllCall(\$MLDLL, "int", "MLFlush", "ptr", \$mlinkp)
If @error then
;MsgBox(0,"Error","MLFlush")
return 0
endif
return \$res[0]
endfunc

;===================================
local \$res
If @error then
return 0
endif
return \$res[0]
endfunc

;===================================
func MLClearError()
local \$res
\$res = DllCall(\$MLDLL, "int", "MLClearError", "ptr", \$mlinkp)
If @error then
;MsgBox(0,"Error","MLClearError")
return 0
endif
return \$res[0]
endfunc

;===================================
func ReplaceSlashZeroOneTwo(\$String)
local \$Pat1 = "\012";
local \$Pat2 = Chr(10) & Chr(9);
\$String = StringReplace(\$String,\$Pat1,"  " & @CrLf)
\$String = StringReplace(\$String,\$Pat2,@CrLf)
return \$String
endfunc

;===================================
func PeekMessage()
local \$stMsg, \$res

\$stMsg = DllStructCreate("hwnd hWnd;uint message;wparam wParam;lparam lParam;dword Time;ptr Point")
DllStructSetData(\$stMsg,1,\$hWnd)
\$res = DllCall("user32.dll","int","PeekMessage","ptr",DllStructGetPtr(\$stMsg),"hwnd",\$hWnd,"int",0,"int",0,"int",\$PM_REMOVE)
if @error then
;MsgBox(0,"Error","PeekMessage")
return 0
endif
\$stMsg = 0
; MsgBox(0,"PeekMessage","PeekMessage = " & \$res[0])
return \$res[0]
endfunc```

Valery

The point of world view

## Create an account

Register a new account