From WikiChip
Editing mirc/sendmessage

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
{{mirc title|SendMessage()}}
+
You can use SendMessage to communicate with mIRC from your external application, in your dll for example.
The {{Win32|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 =
  
== Initializing communication ==
 
 
The external programs that send these messages must create a mapped file with the [http://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=vs.85%29.aspx CreateFileMapping()] function:
 
The external programs that send these messages must create a mapped file with the [http://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=vs.85%29.aspx CreateFileMapping()] function:
  
CreateFileMapping(
+
  CreateFileMapping(
  _In_      HANDLE hFile,
+
    _In_      HANDLE hFile,
  _In_opt_  LPSECURITY_ATTRIBUTES lpAttributes,
+
    _In_opt_  LPSECURITY_ATTRIBUTES lpAttributes,
  _In_      DWORD flProtect,
+
    _In_      DWORD flProtect,
  _In_      DWORD dwMaximumSizeHigh,
+
    _In_      DWORD dwMaximumSizeHigh,
  _In_      DWORD dwMaximumSizeLow,
+
    _In_      DWORD dwMaximumSizeLow,
  _In_opt_  LPCTSTR lpName
+
    _In_opt_  LPCTSTR lpName
);
+
  );
  
 
* Use INVALID_HANDLE_VALUE for the hFile parameter, which basically allows the sytem to handle the file for us.
 
* Use INVALID_HANDLE_VALUE for the hFile parameter, which basically allows the sytem to handle the file for us.
Line 25: Line 25:
 
'''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.
 
'''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 ==
+
= 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 ==
 
 
=== Performing Commands ===
 
  
 
The following call to SendMessage() makes mIRC perform the commands that you specify:
 
The following call to SendMessage() makes mIRC perform the commands that you specify:
  
'''Note''': the 'command' is placed into the editbox and enter is pressed is the exact behavior, this can be used to send IRC messages by not prefixing the command with slashes, but you must prefix it with at least one slash then, if you want to execute a command.
+
SendMessage(mHwnd, WM_MCOMMAND, cMethod, cIndex)
  
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  
* '''mHwnd''' - The handle of the main mIRC window, or the handle of a Channel, Query, etc. window.
+
* '''cMethod''' - how mIRC should process the message, where:
* '''WM_MCOMMAND''' - Which should be defined as WM_USER + 200  
+
** 1 = as if typed in editbox (default)
* '''cMethod''' - How mIRC should process the message, where:
+
** 2 = as if typed in editbox, send as plain text
** 1 = As if typed in editbox (default).
+
** 4 = use flood protection if turned on, can be or'd with 1 or 2
** 2 = As if typed in editbox, send as plain text
+
** 8 = use unicode text
** 4 = Use flood protection if turned on, can be or'd with 1 or 2, and 8
 
** 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, and 4.
 
  
 
* '''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".
 
* '''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".
Line 49: Line 45:
 
This call returns 1 on success, 0 if it fails.
 
This call returns 1 on success, 0 if it fails.
  
=== Evaluating Identifiers and Variables ===
+
== Evaluating Identifiers and Variables ==
  
 
The following call to SendMessage() makes mIRC evaluate the contents of any line that you specify:
 
The following call to SendMessage() makes mIRC evaluate the contents of any line that you specify:
  
SendMessage(mHwnd, WM_MEVALUATE, cMethod, cIndex)
+
SendMessage(mHwnd, WM_MEVALUATE, cMethod, cIndex)
  
* '''mHwnd''' - The handle of the main mIRC window, or the handle of a Channel, Query, etc. window.
+
* '''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
+
* '''WM_MEVALUATE''' - should be defined as WM_USER + 201
* '''cMethod''' - How mIRC should process the message, where:
+
* '''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.
+
** 8 = use unicode text
  
 
* '''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".
 
* '''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".
Line 64: Line 60:
 
This call returns 1 on success, 0 if it fails.
 
This call returns 1 on success, 0 if it fails.
  
== Remote Event Context ==
+
= Remote Event Context =
  
If during a remote event, such as on TEXT, your script calls a DLL which then uses {{Win32|SendMessage()}} to execute a command or evaluate an identifier, you can tell {{Win32|SendMessage()}} to execute in the context of that remote event.
+
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 {{mIRC|$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:
 
During a remote event, a {{mIRC|$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)
+
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.
 
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 ==
+
= Extended Version Information =
  
 
If '''cMethod''' is set to -1, you can set '''cIndex''' to:
 
If '''cMethod''' is set to -1, you can set '''cIndex''' to:
Line 80: Line 76:
 
* '''-2''' - to receive the cMethod options that are supported.
 
* '''-2''' - to receive the cMethod options that are supported.
  
== Extended Error Information ==
+
= 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:
 
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:
Line 89: Line 85:
 
** '''8''' - Bad eventid.
 
** '''8''' - Bad eventid.
 
** '''16''' - Bad server.
 
** '''16''' - Bad server.
** '''32''' - Bad script - means that the script does not exist
+
** '''32''' - Bad code/script.
 
** '''64''' - Disabled (if disabled in lock dialog).
 
** '''64''' - Disabled (if disabled in lock dialog).
  
1 alone means that a script error occured, 16 and 32 can only happen if you use $eventid in SendMessage() in the context of a remote event but the script from which it was called no longer exist.
+
[[Category:MIRC]]
 
+
[[Category:Interprocess communication]]
= Examples =
 
<source lang="c">
 
#include <stdio.h>
 
#include <windows.h>
 
#define WM_MCOMMAND WM_USER + 200
 
#define WM_MEVALUATE WM_USER + 201
 
HANDLE file;
 
LPSTR str;
 
 
 
int main(int argc, char * argv[])
 
{
 
char* command = "//echo -a Hello world";
 
char* evaluation = "m $+ $upper(irc)";
 
file = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, L"mIRC");
 
if (file == NULL)
 
exit(0);
 
str = (LPSTR)MapViewOfFile(file, FILE_MAP_ALL_ACCESS, 0, 0, 0);
 
if (str == NULL)
 
exit(0);
 
HWND mhwnd = (HWND)atoi(argv[1]);
 
//send //echo -s Hello world to mIRC
 
strcpy_s(str, 4096, command);
 
SendMessage(mhwnd, WM_MCOMMAND, 1, 0);
 
//Ask mIRC to evaluate and send back the result
 
strcpy_s(str, 4096, evaluation);
 
SendMessage(mhwnd, WM_MEVALUATE, 0, 0);
 
printf("%s", str);
 
}
 
</source>
 
You must pass the handle of the main mIRC window or a valid channel, query etc window as the first parameter of the program in the command line for it to work.
 
[[Category:mIRC|sendmessage]]
 
[[Category:interprocess communication]]
 

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)