xoru

Untitled

Aug 18th, 2012
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ISM_BASE = #WM_APP + $B000
  2. #ISB_SEND = #ISM_BASE + 1
  3. Procedure.i ImagineSharedMemoryProc(hWnd, uMsg, wParam, lParam)
  4.   Protected WinProc.i = GetProp_(hWnd, "ImagineSharedMemory_WinProc")
  5.   Protected *L        = GetProp_(hWnd, "ImagineSharedMemory_Lua")
  6.   Protected Reference = 0
  7.   Protected Result    = 0
  8.  
  9.   Select uMsg
  10.     Case #ISB_SEND
  11.       Reference = GetProp_(hWnd, "ImagineSharedMemory_FuncRef")
  12.       If(Reference And *L)
  13.         Protected *lpTemp = AllocateMemory(lParam)
  14.         If(*lpTemp)
  15.           ReadWindowProcessMemory(*hWnd, wParam, *lpTemp, lParam)
  16.           luaX_restoreref(*L, Reference)
  17.           If(lua_isfunction(*L, 1))
  18.             lua_pushinteger(*L, *lpTemp)
  19.             lua_pushinteger(*L, lParam)
  20.             lua_call(*L, 2, 0)
  21.           EndIf
  22.           lua_remove(*L, 1)
  23.          
  24.           FreeMemory(*lpTemp)
  25.         EndIf
  26.       EndIf
  27.      
  28.     Case #WM_DESTROY
  29.       SetWindowLong_(hWnd, #GWL_WNDPROC, WinProc)
  30.      
  31.       Protected *lpszString = GetProp_(hWnd, "ImagineSharedMemory_Name")
  32.       If(*lpszString)
  33.         FreeMemory(*lpszString)
  34.       EndIf
  35.      
  36.       Reference = GetProp_(hWnd, "ImagineSharedMemory_FuncRef")
  37.       If(Reference And *L)
  38.         luaX_removeref(*L, Reference)
  39.       EndIf
  40.      
  41.       ; remove the props from the host object      
  42.       RemoveProp_(hWnd, "ImagineSharedMemory_Name")
  43.       RemoveProp_(hWnd, "ImagineSharedMemory_NameL")
  44.       RemoveProp_(hWnd, "ImagineSharedMemory_FuncRef")
  45.       RemoveProp_(hWnd, "ImagineSharedMemory_Init")
  46.       RemoveProp_(hWnd, "ImagineSharedMemory_WinProc")
  47.       RemoveProp_(hWnd, "ImagineSharedMemory_Lua")
  48.   EndSelect
  49.  
  50.   ProcedureReturn CallWindowProc_(WinProc, hWnd, uMsg, wParam, lParam)
  51. EndProcedure
  52.  
  53. ProcedureC SharedMemory_Init(*L)
  54.   ; clear any error code
  55.   IRLUA_PLUGIN_ResetLastError(*L)
  56.  
  57.   ; check the number of arguments
  58.   IRLUA_PLUGIN_CheckNumArgs(*L, 2)
  59.  
  60.   Protected WindowID.s  = IRLUA_PLUGIN_CheckString(*L, 1)
  61.   lua_remove(*L, 1)
  62.   Protected IsFunc.i    = IRLUA_PLUGIN_CheckFunction(*L, 1)
  63.   Protected hWnd.i      = IRLUA_PLUGIN_GetWindowHandle(*L)
  64.  
  65.   If IsWindow_(hWnd)
  66.    
  67.     Protected Initialized.i = GetProp_(hWnd, "ImagineSharedMemory_Init")
  68.     If(Initialized)
  69.       lua_pushboolean(*L, 0)
  70.     Else
  71.       Protected Ref.i       = luaX_storeref(*L)
  72.       Protected *lpszString = AllocateMemory(Len(WindowID) + 1)
  73.       Protected dwLength.l  = Len(WindowID)
  74.      
  75.       PokeS(*lpszString, WindowID)
  76.      
  77.       SetProp_(hWnd, "ImagineSharedMemory_Name",      *lpszString)
  78.       SetProp_(hWnd, "ImagineSharedMemory_NameL",     dwLength)
  79.       SetProp_(hWnd, "ImagineSharedMemory_FuncRef",   Ref)
  80.       SetProp_(hWnd, "ImagineSharedMemory_Init",      1)
  81.       SetProp_(hWnd, "ImagineSharedMemory_Lua",       *L)
  82.      
  83.       Protected OldProc.i   = SetWindowLong_(hWnd, #GWL_WNDPROC, @ImagineSharedMemoryProc())
  84.       SetProp_(hWnd, "ImagineSharedMemory_WinProc", OldProc)
  85.      
  86.       lua_pushboolean(*L, 1)
  87.     EndIf
  88.   EndIf
  89.  
  90.   ProcedureReturn 1
  91. EndProcedure
  92.  
  93. ProcedureC SharedMemory_SendBuffer(*L)
  94.   ; clear any error code
  95.   IRLUA_PLUGIN_ResetLastError(*L)
  96.  
  97.   ; check the number of arguments
  98.   IRLUA_PLUGIN_CheckNumArgs(*L, 3)
  99.  
  100.   Protected WindowID.s  = IRLUA_PLUGIN_CheckString(*L, 1)
  101.   Protected *lptrBuffer = IRLUA_PLUGIN_CheckNumber(*L, 2)
  102.   Protected dwSize.l    = IRLUA_PLUGIN_CheckNumber(*L, 3)
  103.   Protected Dim hWnds.l(0)
  104.  
  105.   Protected c = findISMWindows(WindowID, hWnds())
  106.   If(c)
  107.     For i = 0 To c - 1
  108.       SendMessage_(hWnds(i), #ISB_SEND, *lptrBuffer, dwSize)
  109.     Next
  110.    
  111.     lua_pushboolean(*L, 1)
  112.   Else
  113.     lua_pushboolean(*L, 0)
  114.   EndIf
  115.  
  116.   ProcedureReturn 1
  117. EndProcedure
Advertisement