none
用SmsSendMessage 发送短信失败的原因? 怎么实现群发短信呢? RRS feed

  • 问题

  • 请问: 用SmsSendMessage 发送短信有时失败的原因? 返回值是E_FAIL,但GetLastError()返回0,应该成功了啊? 更奇怪的是我设断点调试都是成功的,直接运行有时就失败?

    另: 请问怎么实现群发短信呢?
    2009年6月11日 14:08

答案

  • 来来来,代码说话。

    C/C++ code
    // 短信发送的例子
    #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;
    }

     


    至于短信群发,简单的你可以写一个循环嘛,不断地调用SmsSendMessage就完事了,如果要搞复杂点的,那就要加上信号量,做个队列,把联系人插入到这个队列里,不断地异步调用SmsSendMessage,每收到一个发送成功的网络回执继续去队列里拿下一个联系人号码继续调用SmsSendMessage,直到队列为空为止。

    2009年6月11日 14:30