#ISM_BASE = #WM_APP + $B000
#ISB_SEND = #ISM_BASE + 1
Procedure.i ImagineSharedMemoryProc(hWnd, uMsg, wParam, lParam)
Protected WinProc.i = GetProp_(hWnd, "ImagineSharedMemory_WinProc")
Protected *L = GetProp_(hWnd, "ImagineSharedMemory_Lua")
Protected Reference = 0
Protected Result = 0
Select uMsg
Case #ISB_SEND
Reference = GetProp_(hWnd, "ImagineSharedMemory_FuncRef")
If(Reference And *L)
Protected *lpTemp = AllocateMemory(lParam)
If(*lpTemp)
ReadWindowProcessMemory(*hWnd, wParam, *lpTemp, lParam)
luaX_restoreref(*L, Reference)
If(lua_isfunction(*L, 1))
lua_pushinteger(*L, *lpTemp)
lua_pushinteger(*L, lParam)
lua_call(*L, 2, 0)
EndIf
lua_remove(*L, 1)
FreeMemory(*lpTemp)
EndIf
EndIf
Case #WM_DESTROY
SetWindowLong_(hWnd, #GWL_WNDPROC, WinProc)
Protected *lpszString = GetProp_(hWnd, "ImagineSharedMemory_Name")
If(*lpszString)
FreeMemory(*lpszString)
EndIf
Reference = GetProp_(hWnd, "ImagineSharedMemory_FuncRef")
If(Reference And *L)
luaX_removeref(*L, Reference)
EndIf
; remove the props from the host object
RemoveProp_(hWnd, "ImagineSharedMemory_Name")
RemoveProp_(hWnd, "ImagineSharedMemory_NameL")
RemoveProp_(hWnd, "ImagineSharedMemory_FuncRef")
RemoveProp_(hWnd, "ImagineSharedMemory_Init")
RemoveProp_(hWnd, "ImagineSharedMemory_WinProc")
RemoveProp_(hWnd, "ImagineSharedMemory_Lua")
EndSelect
ProcedureReturn CallWindowProc_(WinProc, hWnd, uMsg, wParam, lParam)
EndProcedure
ProcedureC SharedMemory_Init(*L)
; clear any error code
IRLUA_PLUGIN_ResetLastError(*L)
; check the number of arguments
IRLUA_PLUGIN_CheckNumArgs(*L, 2)
Protected WindowID.s = IRLUA_PLUGIN_CheckString(*L, 1)
lua_remove(*L, 1)
Protected IsFunc.i = IRLUA_PLUGIN_CheckFunction(*L, 1)
Protected hWnd.i = IRLUA_PLUGIN_GetWindowHandle(*L)
If IsWindow_(hWnd)
Protected Initialized.i = GetProp_(hWnd, "ImagineSharedMemory_Init")
If(Initialized)
lua_pushboolean(*L, 0)
Else
Protected Ref.i = luaX_storeref(*L)
Protected *lpszString = AllocateMemory(Len(WindowID) + 1)
Protected dwLength.l = Len(WindowID)
PokeS(*lpszString, WindowID)
SetProp_(hWnd, "ImagineSharedMemory_Name", *lpszString)
SetProp_(hWnd, "ImagineSharedMemory_NameL", dwLength)
SetProp_(hWnd, "ImagineSharedMemory_FuncRef", Ref)
SetProp_(hWnd, "ImagineSharedMemory_Init", 1)
SetProp_(hWnd, "ImagineSharedMemory_Lua", *L)
Protected OldProc.i = SetWindowLong_(hWnd, #GWL_WNDPROC, @ImagineSharedMemoryProc())
SetProp_(hWnd, "ImagineSharedMemory_WinProc", OldProc)
lua_pushboolean(*L, 1)
EndIf
EndIf
ProcedureReturn 1
EndProcedure
ProcedureC SharedMemory_SendBuffer(*L)
; clear any error code
IRLUA_PLUGIN_ResetLastError(*L)
; check the number of arguments
IRLUA_PLUGIN_CheckNumArgs(*L, 3)
Protected WindowID.s = IRLUA_PLUGIN_CheckString(*L, 1)
Protected *lptrBuffer = IRLUA_PLUGIN_CheckNumber(*L, 2)
Protected dwSize.l = IRLUA_PLUGIN_CheckNumber(*L, 3)
Protected Dim hWnds.l(0)
Protected c = findISMWindows(WindowID, hWnds())
If(c)
For i = 0 To c - 1
SendMessage_(hWnds(i), #ISB_SEND, *lptrBuffer, dwSize)
Next
lua_pushboolean(*L, 1)
Else
lua_pushboolean(*L, 0)
EndIf
ProcedureReturn 1
EndProcedure