发送短信

来源:互联网 发布:荣耀9比8好在哪里知乎 编辑:程序博客网 时间:2024/05/07 11:08

// 短信发送的例子
#include "StdAfx.h"
#include <sms.h>

#pragma comment (lib, "sms.lib")

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPreviousInstance,
                    LPWSTR pszCommandLine,
                    int nCommandShow)
{
        SMS_HANDLE smshHandle;
        SMS_ADDRESS smsaCenter;
        SMS_ADDRESS smsaDestination;
        TEXT_PROVIDER_SPECIFIC_DATA tpsd;
        SMS_MESSAGE_ID smsmidMessageID;

        TCHAR num[] = TEXT("+8613800138000");    // 短信目的地
        LPTSTR lpszDestination = num;

        TCHAR source[] = TEXT("+8613800755500");  // 短信中心
        LPTSTR lpszSMSCenter = source;

        TCHAR text[] = TEXT("黑心的CMCC~!");  // 中英文全角半角字符
        LPTSTR lpszMessage = text;

        HRESULT hRet = 0;


        // 打开 SMS Handle
        hRet = SmsOpen(SMS_MSGTYPE_TEXT, SMS_MODE_SEND, &smshHandle, NULL);

        if(FAILED(hRet))
        {
                return FALSE;
        }
        hRet = SmsGetSMSC(&smsaCenter);

        // 设置 SMS Center
        smsaCenter.smsatAddressType = SMSAT_INTERNATIONAL;
        _tcsncpy(smsaCenter.ptsAddress, lpszSMSCenter, SMS_MAX_ADDRESS_LENGTH);

        hRet = SmsSetSMSC(&smsaCenter);

        // 创建 destination address
        smsaDestination.smsatAddressType = SMSAT_INTERNATIONAL;
        _tcsncpy(smsaDestination.ptsAddress, lpszDestination, SMS_MAX_ADDRESS_LENGTH);

        // 设置 provider specific data
        memset(&tpsd, 0, sizeof(tpsd));
        tpsd.dwMessageOptions = PS_MESSAGE_OPTION_NONE;
        tpsd.psMessageClass = PS_MESSAGE_CLASS1;
        tpsd.psReplaceOption = PSRO_NONE;
        tpsd.dwHeaderDataSize = 0;

        // 发送短信
        hRet = SmsSendMessage(
                smshHandle,
                NULL,
                &smsaDestination,
                NULL,
                (PBYTE) lpszMessage,
                _tcslen(lpszMessage) * sizeof(TCHAR),
                (PBYTE) &tpsd,
                sizeof(TEXT_PROVIDER_SPECIFIC_DATA),
                SMSDE_OPTIMAL,
                SMS_OPTION_DELIVERY_NONE,
                &smsmidMessageID
                );

        if (smshHandle != NULL)
        {
                SmsClose(smshHandle);
                SmsClose(smshRecieveHandle);
                smshHandle = NULL;
        }

        return TRUE;
}

 

sdk路径:

//D:/Program Files/Windows CE Tools/wce500/Windows Mobile 5.0 Pocket PC SDK/Samples/CPP/Win32/Cellcore/Sms

 

 

SmsSendMessage

功能Use the SmsSendMessage function to create and send an Short Message Service (SMS) message.(创建和发送短信,但短信发送后并不保存到发件箱中)

原型

HRESULT SmsSendMessage (

const SMS_HANDLE smshHandle, // 调用SmsOpen时获得的短信句柄

const SMS_ADDRESS * const psmsaSMSCAddress, //指向短信中心号码的地址

const SMS_ADDRESS * const psmsaDestinationAddress, // 发送的目的地址

const SYSTEMTIME * const pstValidityPeriod, // 发送时间的有效期

const BYTE * const pbData, // 信息的内容部分

const DWORD dwDataSize,// 信息内容的长度

const BYTE * const pbProviderSpecificData, //运营商的附加数据

const DWORD dwProviderSpecificDataSize, // 附加数据的长度

const SMS_DATA_ENCODING smsdeDataEncoding, // 短信编码

const DWORD dwOptions, // 其他选项

SMS_MESSAGE_ID * psmsmidMessageID); // 用于得到系统回执的信息

(具体介绍可查看http://msdn.microsoft.com/en-us/library/aa455068.aspx

在实际应用中短信发送不出去,但是SmsSendMessage的返回值是S_OK值。在一些文章中有人这样认为是短信编码的问题造成的。

如果编码格式不对可能造成短信中心网关把短信给吞掉的情况,程序虽然调用成功,但是就是目标号码收不到短信。函数参数中的后三个参数可以不用或设默认值都可以。

起初我也是认为这个地方造成的,很是兴奋。短信的回复内容恰为字母,我误以为短信内容此时是7-BIT的短消息,短信网关把短信给吞掉了,造成目标号码收不到短信。在练习中却也阴差阳错的成功了。很高兴的把理由归到了这个地方。并这样总结:SmsSendMessage可以支持7-bitASCII码的短消息,也支持16-bitunicode的短消息。但内容为ASCII的时候,短信编码为 SMSDE_GSMSMSDE_OPTIMAL,当内容不全是ASCII的时候,短信编码为SMSDE_GSMSMSDE_OPTIMAL。所以回复内容改为汉字即可。

但是这样对么?起初我认为我的解释很合理.但是我却发现我的一个参数与原来的程序不一样.

是我在尝试中无意修改了一个参数,

tpsd.psMessageClass = PS_MESSAGE_CLASS1;

修改为了

tpsd.psMessageClass = PS_MESSAGE_CLASSUNSPECIFIED;

这是发送短信中的运营商的指定数据TEXT_PROVIDER_SPECIFIC_DATA,它的参数psMessageClass是指

Text Short Message Service (SMS) messages with the appropriate flag can replace previously received notifications with a similar flag and originating address.

它有以下五个值:

PS_MESSAGE_CLASS0: The message should be displayed immediately but not stored. The MS shall send an acknowledgement to the service center when the message has successfully reached the MS. (被接受后立即显示但不存储(称为闪信)。需要向SMSC发送确认信息。)

PS_MESSAGE_CLASS1The message should be stored and an acknowledgement should be sent to the Service Center when it is stored.(接收后被存储,一旦存储,需要向SMSC发送确认信息。)

PS_MESSAGE_CLASS2The message should be transferred to the SMS data field in the subscriber identity module (SIM) before an acknowledgement is sent to the Service Center.

PS_MESSAGE_CLASS3When the message has successfully reached the destination and can be stored, an acknowledgement is sent to the Service Center.

PS_MESSAGE_CLASSUNSPECIFIEDThe message Class is not set in the outgoing or incoming message. (对发出或收到的短信不进行设置)

 

分析以上五个值,前四个值有一个共同的特点,都需要向SMSC发送确认。而最后一个值没有设定。

这个值的改动,解决了我所遇到的问题。但究其原因,我有些想不通为什么?
    但是在实际应用中,出现了
tmail.exe的异常。不知道是这个值的变动带来的问题,还是实际模块中存在的问题。还需要继续研究一下。

http://www.cppblog.com/SpringSnow/archive/2009/03/10/76107.html

原创粉丝点击