| / | Articles |
Articles
Features
Using Skype™ to Send Messages from an Expert Advisor
To post a new article, please log in or register
|
Using Skype™ to Send Messages from an Expert Advisor [ ru ]IntroductionSkype is a telecommunications program that, along with normal chats, allows people
to phone via Internet. One of the most important advantages of Skype as compared
to other programs of the kind is a gateway to real mobile network operators. Respectively,
one can call a real mobile phone, send SMSes, and so on. There is also a Skype
version for mobile phones. One can save money on SMSes since sending regular messages
within the program is absolutely free. Basically, a mobile phone must work under
an operating system. Well, it is possible now to be fully mobile if necessary.
And it is this opportunity that many people use nowadays. Who and Why May Need This?Trader cannot always sit in front of trading terminal and watch how the trading is performed, neither he/she must do this. However, it would be great to be able to get signals sometimes that would inform the trader about Expert Advisor's actions or about the market state at certain moments. Why not to get this information in his or her mobile phone? What Kind of Information Would It Be Useful to Get?Information that can be got using messages in a mobile phone can be divided into two groups:
Let's consider examples of current data:
Useful information:
This division is, of course, of a relative nature, and the list of possible events extends further than this article. Every trader will decide for him or herself what content the message should have. The main thing is to realize that this function is very helpful.
How Does This Work in Skype?
How to Do This from an Expert Advisor?I found two ways how to do this, and both need using of DLLs:
How to Realize the Second WayLet us start with DLL. The main part of all works will be preparing DLL for interaction
with the Expert Advisor. First, we write a library that would work when called
from several Expert Advisors. Unfortunately, it will be insufficient just to write
a function and call it. We are using ActiveX, so it is desirable to create a special
separated thread for it and do all work in it. The standard paralleling tool for
Mutex functions will not help. There will be crashes that cannot be detected. So
we will realize the sequence of calls through the custom messaging system. DLL Source Code#include "stdafx.h" #pragma once // Allow use of features specific to Windows XP or later. #ifndef WINVER // Change this to the appropriate value to target other versions of Windows. #define WINVER 0x0501 #endif // Exclude rarely-used stuff from Windows headers #define WIN32_LEAN_AND_MEAN // Include Skype4COM.dll, preliminarily downloaded // from the Skype developers website – http://developers.skype.com/ #import "Skype4COM.dll" rename("CreateEvent","CreatePluginEvent"), rename("SendMessage","SendChatMessage") #define MT4_EXPFUNC __declspec(dllexport) // Declare message codes for our functions. #define WM_PROC_SENDSKYPESMS WM_USER + 01 #define WM_PROC_SENDSKYPEMESSAGE WM_USER + 02 // Variables for the thread to be used for // sending messages HANDLE hUserThread; DWORD ThreadId; // Structures to store parameters of functions // SendSkypeSMS struct fcpSendSkypeSMS { int ExitCode; char * UserNum; char * Message; }; // SendSkypeMessage struct fcpSendSkypeMessage { int ExitCode; char * UserName; char * Message; }; //+------------------------------------------------------------------+ //| Thread function | //+------------------------------------------------------------------+ DWORD WINAPI ThreadProc(LPVOID lpParameter) { MSG msg; HANDLE hEvent; while(true) { if(PostThreadMessage(GetCurrentThreadId(), WM_USER, 0, 0)) break; }; // Initialize COM CoInitialize(NULL); while(GetMessage(&msg, 0, 0, 0)) { if(msg.message == WM_QUIT) { break; } // Message processor WM_PROC_SENDSKYPESMS else if(msg.message == WM_PROC_SENDSKYPESMS) { fcpSendSkypeSMS* fcp = (fcpSendSkypeSMS*)msg.wParam; hEvent = (HANDLE)msg.lParam; try { // Initialize Skype SKYPE4COMLib::ISkypePtr pSkype(__uuidof(SKYPE4COMLib::Skype)); // Connect to Skype. 6 is the protocol version HRESULT hr=pSkype->Attach(6,VARIANT_TRUE); // If everything is ok, start sending the message if(!FAILED(hr)) { try { fcp->ExitCode = 1; // Try to send an SMS pSkype->SendSms(fcp->UserNum,fcp->Message,""); } catch(...) { fcp->ExitCode=-1; } } // Deinitialize Skype pSkype = NULL; } catch(...) { //Error is processed here } // Set the event SetEvent(hEvent); } // Message processor WM_PROC_SENDSKYPEMESSAGE else if(msg.message == WM_PROC_SENDSKYPEMESSAGE) { fcpSendSkypeMessage* fcp = (fcpSendSkypeMessage*)msg.wParam; hEvent = (HANDLE)msg.lParam; try { // Initialize Skype SKYPE4COMLib::ISkypePtr pSkype(__uuidof (SKYPE4COMLib::Skype)); // Connect to Skype. 6 is the protocol version HRESULT hr=pSkype->Attach(6,VARIANT_TRUE); // If everything is ok, start sending the message if(!FAILED(hr)) { try { fcp->ExitCode = 1; // Try to send the message pSkype->SendChatMessage(fcp->UserName, fcp->Message); } catch(...) { fcp->ExitCode=-1; MessageBeep(0); } } // Deinitialize Skype pSkype = NULL; } catch(...) { //Error is processed here } // Set the event SetEvent(hEvent); } }; // Deinitialize COM CoUninitialize(); return 0; } //DLL Initialization //+------------------------------------------------------------------+ //| DLL entry | //+------------------------------------------------------------------+ BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { if(ul_reason_for_call == DLL_PROCESS_ATTACH) { // Create a thread and attach the processor procedure address to it hUserThread = CreateThread(NULL, NULL, ThreadProc, NULL, 0, &ThreadId); if(!hUserThread) { // Error processing if the thread is not created }; } else if(ul_reason_for_call == DLL_PROCESS_DETACH) { // Delete the thread when exiting the library CloseHandle(hUserThread); } return(TRUE); } MT4_EXPFUNC bool __stdcall SendSkypeSMS(int &ExCode,char* sUserNum, char* sMessage) { //Declare the structure of function parameters fcpSendSkypeSMS* fcp; //Declare an event HANDLE hEvent; //The result of function operation by default is false bool Result = false; // Allocate a password for the structure and initialize it fcp = new fcpSendSkypeSMS(); memset(fcp, 0, sizeof(fcpSendSkypeSMS)); // Fill out the structure //By default, the code of the function working end is an error. fcp->ExitCode = -1; fcp->UserNum = sUserNum; fcp->Message = sMessage; // Create an event hEvent = CreateEvent(NULL,FALSE,FALSE, NULL); // Call event WM_PROC_SENDSKYPESMS, pass the data structure address // to processing procedure PostThreadMessage(ThreadId, WM_PROC_SENDSKYPESMS, (WPARAM)fcp, (LPARAM)hEvent); if(WAIT_OBJECT_0 == WaitForSingleObject(hEvent,INFINITE)) { Result = true; } else { // If there was an error at message processing, the function will // return false return(Result); }; // Assign the function processing code to the variable ExCode = fcp->ExitCode; if(ExCode == -1) Result = false; // Free memory and variables and exit delete fcp; CloseHandle(hEvent); return(Result); } MT4_EXPFUNC bool __stdcall SendSkypeMessage(int &ExCode,char* sUserName, char* sMessage) { //Declare the structure of function parameters fcpSendSkypeMessage* fcp; //Declare an event HANDLE hEvent; //The result of function operation by default is false bool Result = false; // Allocate memory for the structure and initialize it fcp = new fcpSendSkypeMessage(); memset(fcp, 0, sizeof(fcpSendSkypeMessage)); // Fill out the structure //By default, the code of the function working end is an error. fcp->ExitCode = -1; fcp->UserName = sUserName; fcp->Message = sMessage; // Create an event hEvent = CreateEvent(NULL, FALSE,FALSE, NULL); // Call the event WM_PROC_SENDSKYPESMS, pass the data structure address // to processing procedure PostThreadMessage(ThreadId, WM_PROC_SENDSKYPEMESSAGE, (WPARAM)fcp, (LPARAM)hEvent); if(WAIT_OBJECT_0 == WaitForSingleObject(hEvent, INFINITE)) { Result = true; } else { // If there was an error at message processing, the function will // return false return(Result); }; // Assign the function processing code to the variable ExCode = fcp->ExitCode; if(ExCode == -1) Result = false; // Free memory and variables and exit delete fcp; CloseHandle(hEvent); return(Result); } File DEFLIBRARY SkypeLib EXPORTS SendSkypeSMS SendSkypeMessage Expert Advisor to Be Tested//+------------------------------------------------------------------+ //| SkypeTestExpert.mq4 | //| Copyright © 2007, Alexey Koshevoy. | //+------------------------------------------------------------------+ // Import functions #import "SkypeLib.dll" bool SendSkypeSMS(int &ExCode[], string Num,string Message); bool SendSkypeMessage(int &ExCode[], string User, string Message); #import //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int init() { int ExCode[1]; Alert("Send message..."); Alert(SendSkypeMessage(ExCode, "skype.account.name", "Skype message test")); if(ExCode[0] == -1) Alert("Error sending the message"); else Alert("Message sent"); Alert("Send SMS..."); Alert(SendSkypeSMS(ExCode, "+1234567890", "Skype sms test")); if(ExCode[0] == -1) Alert("Error sending the SMS"); else Alert("SMS sent"); return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { return(0); } //+------------------------------------------------------------------+ The Ea is very simple, its main aim is to send an SMS and a regular message through the DLL we have coded. It acts within the initialization function, so it can also be tested at weekends.
Skype InstallationThe application can be downloaded at http://www.skype.com/. It is recommended to install the latest version of the program, since the preceding versions don't support COM interface, they only have an API. However, the API doesn't support sending SMSes. Well, Skype has already been installed. Now we have to download the COM library.
It can be found at https://developer.skype.com/, in Downloads. Check the amount on the account to be used for sending SMSes. If
there is not enough money, you can add some through Internet. It will not allow
to send SMSes without money on the account, but regular messages can be sent without
any problems. ![]() For the terminal to access to Skype API, it must be registered. You can check whether it is permitted to work with the API using the menu of Tools->Options->Privacy->Manage other programs access to Skype. It must look approximately like this: ![]() The terminal will be registered at the first attempt to use the library. It cannot be done manually. So, when the library is being installed for the first time, it is necessary to wait until a message is sent in order to have confirmed the permission to use Skype API. Skype will show the following dialog box: ![]() After confirmation, the system will start working in the automated mode. Installation of SkypeLibДля In order to install the library named SkypeLib.dll, it is necessary to copy it to the experts/libraries
folder in the terminal directory. The library named Skype4COM.dll must also be copied to this folder. Now we
have to set up the terminal to be able to work with the DLL. For this, check field "Allow DLL imports"
in the "Safety" section when attaching an EA as shown below: ![]() Now we can use the library. Some Important DetailsHaving some experience in testing and implementation, I noticed some "subtleties". For example, you
have to consider that, if you have enough money on your account and send an SMS to a non-existing number, no
error will be reported, the function will work successfully, whereas the message status will be set for “sending…”.
This is why it is important to set up function inputs very clearly. It is also very important the you use Skype
version 3. 0 (or later versions). It must also be noted that some additional libraries may be needed for SkypeLib. dll to operate. The problem is especially acute after the first service pack for Visual Studio 2005 has been released. The best way to solve this problem is to create a setup file. All necessary libraries will be included in it automatically. File Skype4COM. dll can be included in it, too. Files Attached to the Article
Highs and LowsThe following disadvantages can be observed when using Skype SMSes:
This method has the following advantages:
ConclusionsWe have learned how to send SMSes and regular messages via Skype. In such a way, we have got an interface, which
is perhaps not the most convenient, but surely indispensable, to inform us about current events in the terminal.
What comes next? Well, for example, Skype allows both sending and RECEIVING messages… Translated from Russian by MetaQuotes Software Corp.
Original article: http://articles.mql4.com/ru/305
Warning:
All rights to these materials are reserved by MetaQuotes Software Corp.
Copying or reprinting of these materials in whole or in part is prohibited.
Hello, Can I just launch the macrofile from EA without using dll? I just want a blank sms or a phone call without additional info. How can that be done?
2008.08.12 20:38 menem
Hi Alexey Koshevoy I found this skype DLL is quite useful, but I have problem to send message/ SMS, everytime the EA calls "SendSkypeMessage(int &ExCode[], string Num,string Message) ", the process will stuck there forever. I had copied the SkypeLib.dll and Skype4COM.dll to the Expert/libraries/ folder, what other files I need ? Also, the Manage API Access Control Message Box never pop up and I can't registered the process as well. Is there anything to deal with OS anf Skype Version, I m using Windows Vista and Skype 3.8.0.144. Last, do you have a DLL which allows EA to read Skype message . Thanks Tom
2008.08.05 21:16 TT_FX1
good tutorial. one correction: // Include Skype4COM.dll, preliminarily downloaded URL is http://developer.skype.com/ not http://developers.skype.com/
2008.01.23 05:54 alpha430
4 comments
|