SmsDll.cpp
资源名称:Mesctrl.rar [点击查看]
上传用户:wang776455
上传日期:2022-06-12
资源大小:4721k
文件大小:3k
源码类别:
手机短信编程
开发平台:
Visual C++
- // SmsDll.cpp : Defines the initialization routines for the DLL.
- //
- #include "stdafx.h"
- #include "SmsDll.h"
- #include "SmsTraffic.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CSmsTraffic* m_pSmsTraffic;
- BOOL bIsOpenComm;
- //
- // Note!
- //
- // If this DLL is dynamically linked against the MFC
- // DLLs, any functions exported from this DLL which
- // call into MFC must have the AFX_MANAGE_STATE macro
- // added at the very beginning of the function.
- //
- // For example:
- //
- // extern "C" BOOL PASCAL EXPORT ExportedFunction()
- // {
- // AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // // normal function body here
- // }
- //
- // It is very important that this macro appear in each
- // function, prior to any calls into MFC. This means that
- // it must appear as the first statement within the
- // function, even before any object variable declarations
- // as their constructors may generate calls into the MFC
- // DLL.
- //
- // Please see MFC Technical Notes 33 and 58 for additional
- // details.
- //
- /////////////////////////////////////////////////////////////////////////////
- // CSmsDllApp
- BEGIN_MESSAGE_MAP(CSmsDllApp, CWinApp)
- //{{AFX_MSG_MAP(CSmsDllApp)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CSmsDllApp construction
- CSmsDllApp::CSmsDllApp()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- if(OpenComm("COM1", 115200) == FALSE)
- {
- CString strError;
- strError = "无法打开串口1! ";
- // MessageBox(strError, "警告", MB_OK);
- bIsOpenComm = FALSE;
- return;
- }
- else
- {
- m_pSmsTraffic = new CSmsTraffic;
- bIsOpenComm = TRUE;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CSmsDllApp object
- CSmsDllApp theApp;
- void SmsSend(CString centerNumber, CString mobileNo, CString message, char mode)
- {
- CString strSmsc;
- CString strNumber;
- CString strContent;
- strSmsc = centerNumber;
- strNumber = mobileNo;
- strContent = message;
- // 检查号码
- if(strNumber.GetLength() < 11)
- {
- // ShowMessage("请输入正确的号码!");
- return;
- }
- // 检查短消息内容是否空,或者超长
- CString strUnicode;
- WCHAR wchar[1024];
- int nCount = ::MultiByteToWideChar(CP_ACP, 0, strContent, -1, wchar, 1024);
- if(nCount <= 1)
- {
- // ShowMessage("请输入消息内容!");
- return;
- }
- else if(nCount > 70) // 我们决定全部用UCS2编码,最大70个字符(半角/全角)
- {
- // ShowMessage("消息内容太长,无法发送!");
- return;
- }
- SM_PARAM SmParam;
- ::memset(&SmParam, 0, sizeof(SM_PARAM));
- // 去掉号码前的"+"
- if(strSmsc[0] == '+') strSmsc = strSmsc.Mid(1, strSmsc.GetLength()-1);
- if(strNumber[0] == '+') strNumber = strNumber.Mid(1, strNumber.GetLength()-1);
- // 在号码前加"86"
- if(strSmsc.Mid(0,2) != "86") strSmsc = "86" + strSmsc;
- if(strNumber.Mid(0,2) != "86") strNumber = "86" + strNumber;
- // 填充短消息结构
- ::strcpy(SmParam.SCA, strSmsc);
- ::strcpy(SmParam.TPA, strNumber);
- ::strcpy(SmParam.TP_UD, strContent);
- SmParam.TP_PID = 0;
- //? SmParam.TP_DCS = GSM_7BIT;
- //? SmParam.TP_DCS = GSM_8BIT;
- SmParam.TP_DCS = GSM_UCS2;
- SmParam.mode = mode;
- // 发送短消息
- m_pSmsTraffic->PutSendMessage(&SmParam);
- }