Shoutbox

Help with API - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Help with API (/showthread.php?tid=74696)

Help with API by pedro_cesar on 05-26-2007 at 06:13 AM

May someone please explain (or point me in the right direction) how MsgPLus interacts with the WinApi Ex.: what functions are available and how are they used?


RE: Help with API by vikke on 05-26-2007 at 06:48 AM

You can call almost any DLL export function with the MsgPlus! scripting.
The calling method has to be stdcall, so the Windows 32 API works fine. The function does also have to return int (if you're going to read the return value).

For example: You can call the SetWindowsHookEx function, but you won't be able to anything with it because javascript cannot handle the callbacks functions needed.

To use Win32 APIs, simply use the Interop object. The Interop object has a method/function which is called Call.
To use call in order to call any API function do like this:

code:
Interop.Call("user32.dll", "SetWindowsTextW", 0, "Hello");
The first parameter is the DLL the function is in, the second is the export name, rest are optional parameters used by the function.

Note: Always use the unicode version of Win32 APIs instead of the ANSI version. So put a W after a function to use the unicode function (only on functions whichs contains one string in its parameters, all expect GetProcAddress).

Edit: After using the DLL, you might want to free it, then simply do:
code:
Interop.FreeDll("dllname.dll");

RE: Help with API by Matti on 05-26-2007 at 06:52 AM

In theory, you can use any functions provided in the Win32 API. However, there are some disadvantages Plus! Live has because its scripting engine isn't yet perfect. For example, you can't register a notification for a control since there's no Control object which could have a RegisterMessageNotification function, such function only exists for the PlusWnd object. Except from that, any function should work (unless I forgot something?).

Some examples:

  • ShellExecute - Used to start a program. In this example the program "MyProg.exe" in the script folder with the parameter /JustDoIt, which should get recognized by the program.
    code:
    var Result = Interop.Call(
       /* Library to call */ "shell32",
       /* Function to call */ "ShellExecuteW",
       /* hWnd */ 0,
       /* lpOperation */, "open",
       /* lpFile */, MsgPlus.ScriptFilesPath + "\\MyProg.exe",
       /* lpParameters */ "/JustDoIt",
       /* lpDirectory */ 0,
       /* nShowCmd */, 0
    );
    if(Result <= 32) //If Result is less than or equal to 32, an error has been returned.
       Debug.Trace("ShellExecute returned an error! Result: "+Result);
    else
       Debug.Trace("ShellExecute succeeded");
  • SendMessage - Used to send a window message to a window or window control. In this example, the window caption will be changed by the WM_SETTEXT message.
    code:
    //Window
    var PlusWnd = MsgPlus.CreateWnd("Windows.xml", "WndTest");
    //String for title
    var sTitle = "My New Window";
    //DataBloc for title
    var lpTitle = Interop.Allocate((sTitle.length+1)*2);
    lpTitle.WriteString(sTitle, 0);
    //Call SendMessage with WM_SETTEXT
    var Result = Interop.Call(
       /* Library to call */ "user32",
       /* Function to call */ "SendMessageW",
       /* hWnd */ PlusWnd.Handle,
       /* WM_SETTEXT */ 0xC,
       /* wParam */ 0,
       /* lParam */ lpTitle.DataPtr
    );
    if(Result != 1) Debug.Trace("SendMessage returned an error! Result: "+Result);
  • PlusWnd.RegisterMessageNotification - Used to register a notification to a window and react on it. In this example we'll dump the new size of the window to the debug window when the user is resizing it.
    code:
    //Window
    var PlusWnd = MsgPlus.CreateWnd("Windows.xml", "WndTest");
    //Register message notification
    PlusWnd.RegisterMessageNotification(/* WM_SIZE */ 0x5, true);
    ...
    function OnWndTestEvent_MessageNotification(PlusWnd, Message, wParam, lParam) {
       if(Message == /* WM_SIZE */ 0x5) {
          var Width = lParam  & 0xFFFF; //Width is stored in low-order word of lParam
          var Height = lParam >> 16; //Height is stored in high-order word of lParam
          Debug.Trace("WndTest size: "+Width+"x"+Height);
       }
       return -1; //Let the window do its stuff with this message too
    }
Oh noees! I got beaten by vikke! :(
quote:
Originally posted by vikke
Note: Always use the unicode version of Win32 APIs instead of the ANSI version. So put a W after a function to use the unicode function (only on functions whichs contains one string in its parameters, all expect GetProcAddress).
True, I wanted to mention that but I forgot.
RE: RE: Help with API by vikke on 05-26-2007 at 06:54 AM

quote:
Originally posted by Mattike
Oh noees! I got beaten by vikke! :(

No you did not, you probably explains much better than me. ;)
RE: Help with API by mickael9 on 05-26-2007 at 09:23 AM

quote:
Originally posted by Mattike
var lpTitle = Interop.Call((sTitle.length+1)*2);
var lpTitle = Interop.Allocate((sTitle.length+1)*2);

:P
RE: Help with API by Matti on 05-26-2007 at 10:21 AM

quote:
Originally posted by mickael9
quote:
Originally posted by Mattike
var lpTitle = Interop.Call((sTitle.length+1)*2);
var lpTitle = Interop.Allocate((sTitle.length+1)*2);

:P
True. Fixed. :P
RE: Help with API by mickael9 on 05-26-2007 at 05:30 PM

Also note that you don't need to use a datablock to pass a simple unicode string, you can pass it directly.


RE: Help with API by pedro_cesar on 05-27-2007 at 01:03 AM

Many Thanks to all, this's been really useful, I'm trying to improve a script I created and submitted to the database (waiting for response though). I'm gonna go and practice now some of this stuff, I have some questions already, but I'll organize'em and ask them later in a proper way.