Win mobile uses mapirule to receive text messages

Source: Internet
Author: User

I. Before using the mapi rule client, we need to register it as a COM Object and add its Class Identifier to the following registry key: hkey_classes_root/CLSID /. To make the inbox aware of the existence of the mapi rule client, we must also write its Class Identifier to the following registry key: HKEY_LOCAL_MACHINE/software/Microsoft/inbox/svc/SMS/rules.

 

// Register as a COM Object

LR = regcreatekeyex (hkey_classes_root, text ("// CLSID // {3ab4c10e-673c-494c-98a2-cc2e91a48115 }"),
0, null, 0, 0, null,
& Hkey, & dwdisposition );

LR = regcreatekeyex (hkey, text ("inprocserver32 "),
0, null, 0, 0, null,
& Hsubkey, & dwdisposition );

Lstrcpy (wszvalue, text ("// windows // mapirule. dll "));
LR = regsetvalueex (hsubkey, null, 0, REG_SZ, (lpbyte) wszvalue, (lstrlen (wszvalue) + 1) * sizeof (tchar ));

// Register with Inbox:

LR = regcreatekeyex (HKEY_LOCAL_MACHINE, text ("// software // Microsoft // inbox // SVC // SMS // Rules "),
0, null, 0, 0, null,
& Hkey, & dwdisposition );

LR = regsetvalueex (hkey, text ("{3ab4c10e-673c-494c-98a2-cc2e91a48115}"), 0, REG_DWORD,
(Lpbyte) & dwdisposition, sizeof (DWORD ));

II. After successful registration, the system will automatically call the processmessage function to receive text messages. When the received text message content is "Zzz", it will be placed into the memory ing file (there are other methods to pass to the application, such as messages), and then delete the text message.

 

Hresult cmailruleclient: processmessage (imsgstore * pmsgstore, ulong cbmsg, lpentryid lpmsg,
Ulong cbdestfolder, lpentryid lpdestfolder, ulong * puleventtype, mrchandled * phandled)
{
Hresult hR = s_ OK;
Sizedsproptagarray (1, sptasubject) = {1, pr_subject };
Sizedsproptagarray (1, sptaemail) = {1, pr_sender_email_address };
Ulong cvalues = 0;
Spropvalue * pspvsubject = NULL;
Spropvalue * pspvemail = NULL;
IMessage * PMSG = NULL;
Hresult hrret = s_ OK;

// Get the message from the entry ID
HR = pmsgstore-> openentry (cbmsg, lpmsg, null, 0, null, (lpunknown *) & PMSG );
If (failed (HR ))
{

Retailmsg (true, (text ("unable to get the message! /R/N ")));
Goto exit;
}

// For SMS, the subject is also the message body
HR = PMSG-> getprops (sproptagarray *) & sptasubject, mapi_unicode, & cvalues, & pspvsubject );
If (failed (HR ))
{

Retailmsg (true, (text ("unable to get the message body! /R/N ")));
Goto exit;
}
// Get the sender's address or phone number
HR = PMSG-> getprops (sproptagarray *) & sptaemail, mapi_unicode, & cvalues, & pspvemail );

If (failed (HR ))
{

Retailmsg (true, (text ("couldn't get the sender's address! /R/N ")));

Goto exit;
}
 
// Here we filter the message on some predetermined string. for sample purposes
// Here we use "Zzz". What happens when the filter condition (s) are met is up
// You. You can send wm_copydata messages to other app windows for light IPC, send
// An SMS message in response, or whatever you need to do.

If (wcsstr (pspvsubject-> value. lpszw, l "Zzz ")! = NULL)
{
If (g_hsmsavailableevent! = NULL ){
// We have received an SMS message that needs to be sent to our client.
// Since we run in the process space of inbox, we are using a memory mapped
// File to pass the message and phone number to our client, that typically
// Runs in another process space (therefor we can not simply copy strings ).
// We protect the memory mapped file with a mutex to make sure that we are
// Not writing new SMS data while the reading client is still processing
// A previous SMS message.
Waitforsingleobject (g_hmutex, infinite );
Lstrcpy (g_psmsbuffer-> g_szphonenr, pspvemail-> value. lpszw );
Lstrcpy (g_psmsbuffer-> g_szmessagebody, pspvsubject-> value. lpszw );
Releasemutex (g_hmutex );
Setevent (g_hsmsavailableevent );
}

// Delete the message and mark it as handled so it won't show up in inbox
HR = deletemessage (pmsgstore, PMSG, cbmsg, lpmsg, cbdestfolder, lpdestfolder, puleventtype, phandled );
}
Else
{
// A 'normal' message, pass it on
* Phandled = mrc_not_handled;
}

// Clean up
Exit:
If (pspvemail)
{
Mapifreebuffer (pspvemail );
}
If (pspvsubject)
{
Mapifreebuffer (pspvsubject );
}
If (PMSG)
{
PMSG-> release ();
}

Return hr;
}

3. The application listens to the memory ing file (call terminatesmsmessagepassing) void terminatesmsmessagepassing (void)
{
// Make sure to have one last empty string available to copy to the client.
Memset (g_pclientsmsbuffer, 0, sizeof (sms_buffer ));

Setevent (g_hclientevent); // optionally allow the calling application to return from getdata.
Closehandle (g_hclientevent );
Closehandle (g_hclientmutex );

If (g_pclientsmsbuffer ){
Unmapviewoffile (g_pclientsmsbuffer );
G_pclientsmsbuffer = NULL;
}
If (g_hclientmmobj ){
Closehandle (g_hclientmmobj );
G_hclientmmobj = NULL;
}
}

4. The application obtains the text message content and the text message number (call smsmessageavailable)

Bool smsmessageavailable (wchar_t * lpdestination, wchar_t * lpphonenr)
{
Waitforsingleobject (g_hclientevent, infinite );

If (g_pclientsmsbuffer! = NULL ){
Waitforsingleobject (g_hclientmutex, infinite );
Lstrcpy (lpphonenr, g_pclientsmsbuffer-> g_szphonenr );
Lstrcpy (lpdestination, g_pclientsmsbuffer-> g_szmessagebody );
Releasemutex (g_hclientmutex );
} Else {
* Lpphonenr = '/0 ';
* Lpdestination = '/0 ';
}
Return * lpphonenr! = '/0 ';
}

5. Cancel mailruleclient object

Stdapi dllunregisterserver ()
{
Hkey = NULL;
Hresult hR = e_fail;
Lresult LR;
DWORD dwdisposition;

// Delete registry keys
Regdeletekey (hkey_classes_root, text ("// CLSID // {3ab4c10e-673c-494c-98a2-cc2e91a48115 }"));

LR = regcreatekeyex (HKEY_LOCAL_MACHINE, text ("// software // Microsoft // inbox // SVC // SMS // Rules "),
0, null, 0, 0, null,
& Hkey, & dwdisposition );
If (LR! = Error_success)
{
Goto exit;
}

LR = regdeletevalue (hkey, text ("{3ab4c10e-673c-494c-98a2-cc2e91a48115 }"));

HR = s_ OK;

Exit:
If (hkey)
{
Regclosekey (hkey );
}

Return hr;
}

Sat. The Calling process in c ++ is as follows:

Hinstance hinstlibrary = loadlibrary (_ T ("// windows // mapirule. dll "));
If (hinstlibrary = NULL ){
Freelibrary (hinstlibrary );
Return 0;
}

// Register

If (dllregisterserver () = 0)
{
MessageBox ("cocould not initialize the imailruleclient DLL ");
Freelibrary (hinstlibrary );
Return 0;
}

Capturesmessages ();
While (true ){
Smsmessageavailable (lpdestination, lpphonenr );
If (* lpdestination! = '/0 '){
Outputdebugstring (lpdestination );
Break;
}
}

Terminatesmsmessagepassing ();

Dllunregisterserver ();
Freelibrary (hinstlibrary );

7. Call the // import/export file in C #

# Region P/invoke helpers
[Dllimport ("mapirule. dll")]
Public static extern int dllregisterserver ();
[Dllimport ("mapirule. dll")]
Public static extern void dllunregisterserver ();
[Dllimport ("coredll. dll")]
Public static extern intptr loadlibrary (string libname );
[Dllimport ("coredll. dll")]
Public static extern bool freelibrary (intptr hlibmodule );
# Endregion

Public class unmanagedapi
{
[Dllimport ("mapirule. dll")]
Public static extern int smsmessageavailable (stringbuilder message, stringbuilder phonenumber );
[Dllimport ("mapirule. dll")]
Public static extern void capturesmsmessages ();
[Dllimport ("mapirule. dll")]
Public static extern void terminatesmsmessagepassing ();
}
# Endregion

// Register

Hlibmodule = loadlibrary ("mapirule. dll ");
If (dllregisterserver ()! = 0)
{
MessageBox. Show ("cocould not initialize the imailruleclient DLL ");
}

// Listen and get the message

Unmanagedapi. capturesmsmessages ();

While (! Bdone)
{
Unmanagedapi. smsmessageavailable (sbsms, sbphonenr );

If (sbsms. length! = 0)
{
Parentform. receivedsmessage = sbphonenr. tostring () + "-" + sbsms. tostring ();
Parentform. Invoke (New eventhandler (parentform. updatelistbox ));
}
}

// Cancel

Dllunregisterserver ();

Freelibrary (hlibmodule );

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/procedurecode/archive/2007/10/23/1839708.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.