From WikiChip
Difference between revisions of "mirc/sendmessage"
< mirc

(Examples)
(Examples)
Line 99: Line 99:
 
#define WM_MCOMMAND WM_USER + 200
 
#define WM_MCOMMAND WM_USER + 200
 
#define WM_MEVALUATE WM_USER + 201
 
#define WM_MEVALUATE WM_USER + 201
 
+
int __stdcall test(HWND mWnd, HWND aWnd, CHAR *data, char *parms, BOOL show, BOOL nopause)
+
__declspec(dllexport) int __stdcall test(HWND mWnd, HWND aWnd, CHAR *data, char *parms, BOOL show, BOOL nopause)
 
{
 
{
DWORD test = NULL;
+
    HANDLE file = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,4096, "mIRC");
        HANDLE file = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,4096, "mIRC");
 
 
LPSTR str = (LPSTR) MapViewOfFile(file, FILE_MAP_ALL_ACCESS, 0, 0, 0);
 
LPSTR str = (LPSTR) MapViewOfFile(file, FILE_MAP_ALL_ACCESS, 0, 0, 0);
 +
//after MapViewOfFile(), 'str' is where is where you write to but also is the result of each call with WM_MEVALUATE:
 +
//send //echo -s Hello world to mIRC
 
wsprintf(str,"//echo -s Hello world");
 
wsprintf(str,"//echo -s Hello world");
 
SendMessage(mWnd, WM_MCOMMAND, 1 , 0);
 
SendMessage(mWnd, WM_MCOMMAND, 1 , 0);
 +
//Ask mIRC to evaluate and send back the result
 +
wsprintf(str,"m $+ $upper(irc)");
 +
SendMessage(mWnd, WM_MEVALUATE, 0, 0);
 +
//Copy the result of "m $+ $upper(irc)" into data and we return 3 indicating $dll returns what 'data' contains
 +
lstrcpy(data,str);
 
CloseHandle(file);
 
CloseHandle(file);
return 1;
+
return 3;
 
}</syntaxhighlight>
 
}</syntaxhighlight>
 
[[Category:MIRC]]
 
[[Category:MIRC]]
 
[[Category:Interprocess communication]]
 
[[Category:Interprocess communication]]

Revision as of 16:16, 21 July 2014

Template:mIRC Guide The SendMessage() function can be used to communicate with mIRC from your external process (e.g. another program or a dll). mIRC provides a number of private Window Messages to evaluate or execute commands and identifiers.

Initializing communication

The external programs that send these messages must create a mapped file with the CreateFileMapping() function:

CreateFileMapping(
  _In_      HANDLE hFile,
  _In_opt_  LPSECURITY_ATTRIBUTES lpAttributes,
  _In_      DWORD flProtect,
  _In_      DWORD dwMaximumSizeHigh,
  _In_      DWORD dwMaximumSizeLow,
  _In_opt_  LPCTSTR lpName
);
  • Use INVALID_HANDLE_VALUE for the hFile parameter, which basically allows the sytem to handle the file for us.
  • Use NULL for lpAttributes, for a default security.
  • Use PAGE_READWRITE for flProtect, to be able to read and write from/to the file.
  • Use 0 for dwMaximumSizeHigh and 4096 for dwMaximumSizeLow to indicate a file of that size

Note: The mapped file must be at least 4096 bytes.

  • lpName is the name of the file, in previous version of mIRC, this parameter had to be "mIRC", you certainly can see the limitation with this, so it was extended, you can now specify a name of the form "mIRCN" where N is a number. You can also still use mIRC of course.

Note: To prevent simultaneous access to the mapped file, your code must check whether the mapped file exists or not before using it. If it exists, you should assume that it is in use by another program, and should try again later.

Communicating

After creating the mapped file, you need to write to that file the line mIRC will receive, see the examples to get an idea about how to write to that file.

Performing Commands

The following call to SendMessage() makes mIRC perform the commands that you specify:

SendMessage(mHwnd, WM_MCOMMAND, cMethod, cIndex)
  • mHwnd - The handle of the main mIRC window, or the handle of a Channel, Query, etc. window.
  • WM_MCOMMAND - Which should be defined as WM_USER + 200
  • cMethod - How mIRC should process the message, where:
    • 1 = As if typed in editbox (default)
    • 2 = As if typed in editbox, send as plain text
    • 4 = Use flood protection if turned on, can be or'd with 1 or 2
    • 8 = Use unicode text. For backward compatibility reason, mIRC takes the data in the mapped file as ANSI by default, if you are willing to use unicode, you must use this. This can be or'd with 1 or 2.
  • cIndex - If you created a mapped filename of the form "mIRCN", this is where you specify the N parameter to use, if cIndex is 0, the filename must be "mIRC".

This call returns 1 on success, 0 if it fails.

Evaluating Identifiers and Variables

The following call to SendMessage() makes mIRC evaluate the contents of any line that you specify:

SendMessage(mHwnd, WM_MEVALUATE, cMethod, cIndex)
  • mHwnd - The handle of the main mIRC window, or the handle of a Channel, Query, etc. window.
  • WM_MEVALUATE - Should be defined as WM_USER + 201
  • cMethod - How mIRC should process the message, where:
    • 8 = Use unicode text. For backward compatibility reason, mIRC takes the data in the mapped file as ANSI by default, if you are willing to use unicode, you must use this.
  • cIndex - If you created a mapped filename of the form "mIRCN", this is where you specify the N parameter to use, if cIndex is 0, the filename must be "mIRC".

This call returns 1 on success, 0 if it fails.

Remote Event Context

If during a remote event, such as on TEXT, your script calls a DLL which then uses SendMessage() to execute a command or evaluate an identifier, you can tell SendMessage() to execute in the context of that remote event.

During a remote event, a $eventid identifier is set to a unique value to identify the event. This can be passed to a DLL which can then pass it back to mIRC using:

SendMessage(mHwnd, WM_MCOMMAND, MAKEWPARAM(cMethod, cEventId), cIndex)

This will cause the command/evaluation to execute in the context of the remote event identified by cEventId. If cEventId is 0, this indicates a non-remote event.

Extended Version Information

If cMethod is set to -1, you can set cIndex to:

  • -1 - to receive the mIRC version number.
  • -2 - to receive the cMethod options that are supported.

Extended Error Information

If cMethod is or'd with the value 16, this will make SendMessage() return more useful error values instead of just 0 for failure and 1 for success. The return values are:

  • 0 - Success.
  • 1 - Failure, You can OR that value to get more specific errors:
    • 2 - Bad mapped filename.
    • 4 - Bad mapped file size.
    • 8 - Bad eventid.
    • 16 - Bad server.
    • 32 - Bad code/script.
    • 64 - Disabled (if disabled in lock dialog).

Examples

This creates a function "test" for a dll which will execute a //echo command in mIRC, we do not use Unicode.

Note: Error checking is not provided.

#include <cstdio>
#include <windows.h>
#define WM_MCOMMAND WM_USER + 200
#define WM_MEVALUATE WM_USER + 201
 
 __declspec(dllexport) int __stdcall test(HWND mWnd, HWND aWnd, CHAR *data, char *parms, BOOL show, BOOL nopause)
{
    HANDLE file = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,4096, "mIRC");
	LPSTR str = (LPSTR) MapViewOfFile(file, FILE_MAP_ALL_ACCESS, 0, 0, 0);
	//after MapViewOfFile(), 'str' is where is where you write to but also is the result of each call with WM_MEVALUATE:
	//send //echo -s Hello world to mIRC
	wsprintf(str,"//echo -s Hello world");
	SendMessage(mWnd, WM_MCOMMAND, 1 , 0);
	//Ask mIRC to evaluate and send back the result
	wsprintf(str,"m $+ $upper(irc)");
	SendMessage(mWnd, WM_MEVALUATE, 0, 0);
	//Copy the result of "m $+ $upper(irc)" into data and we return 3 indicating $dll returns what 'data' contains
	lstrcpy(data,str);
	CloseHandle(file);
	return 3;
}