Socket to Air Windows COM元件使用範例 for VC6
(Dialog based)
- 系統需求:Windows
- 程式語言:Visual C++ 6.0
- 應用程式形式:Dialog based
- 元件註冊與安裝:(使用前必須先註冊)
1. 將S2ACOM.dll複製到Windows系統目錄中,如C:\WinNT\System32\。
2. 於Windows開始->執行的視窗中,輸入指令 regsvr32
C:\WinNT\System32\S2ACOM.dll
註冊此元件
3. 使用VC開發,在專案中需另外加入(S2ACOM_i.c與S2ACOM.h)。
- 注意事項:
若發送成功,系統會回傳訊息代碼(MessageID),若沒取得訊息代碼,則不算發送成功
- 範例開發步驟:
1. 使用VC6建立專案:專案類型選 MFC AppWizard(exe) -> Project name 取名為VC6_Dialog ->
application type選 Dialog based -> 然後按Finish即可。
2. 在resource view中加入Edit Box,項目如下圖所示。

3. 使用ClassWizard建立對應的變數,項目如下圖所示。

m_account : 帳號 , m_passwd : 密碼 , m_msisdn : 手機號碼 , m_message
: 簡訊內容
4. 回到resource view,在VC6_Dialog視窗中Double click傳送鈕,建立OnOK()函式。
5. 將S2ACOM_i.c與S2ACOM.h複製到VC6_Dialog
Project目錄下。
6. 在VC6_DialogDlg.cpp加入下列程式碼:
// VC6_DialogDlg.cpp : implementation file
......
//----------code added for S2ACOM by chming ----
#include "S2ACOM_i.c"
// this is a server file
#include "S2ACOM.h" // this is a server file
#include "Comdef.h"
// for usage of BSTR
#include "Atlbase.h"
// for usage of CComBSTR
......
void CVC6_DialogDlg::OnOK()
{
// TODO: Add extra validation here
CString ServerIP="203.66.172.131"; //Socket to air
server ip
CString ServerPort="8000"; //Socket to air server port
CString Ret_Buffer=_T("");
int ret=-99;
//init value
//Get the below two constants from
the S2ACOM_i.c
const IID IID_IS2Air = {0x4F91871D,0x9B96,0x45C2,{0xB6,0x70,0x2D,0x5D,0x9C,0xE5,0xFF,0x44}};
const CLSID CLSID_S2Air = {0x31C80217,0x22B5,0x4BA9,{0x8A,0x3C,0x0B,0x4F,0xC2,0x4A,0x44,0x1F}};
UpdateData(TRUE); //Enable
DoDataExchange(CDataExchange* pDX)
//CString to BSTR
BSTR bServerIP = ServerIP.AllocSysString();
BSTR bServerPort = ServerPort.AllocSysString();
BSTR bm_account = m_account.AllocSysString();
BSTR bm_passwd = m_passwd.AllocSysString();
BSTR bm_msisdn = m_msisdn.AllocSysString();
BSTR bm_message = m_message.AllocSysString();
HRESULT hr = CoInitialize(NULL); //
Initialize COM library
CComBSTR mresult;
if (SUCCEEDED(hr)){
//To check if CoInitialize success or not
IS2Air *pIS2Air = NULL;
hr = CoCreateInstance(CLSID_S2Air,
NULL,
CLSCTX_INPROC_SERVER, IID_IS2Air,
(void**)&pIS2Air);
if (SUCCEEDED(hr)){
//建立與Socket to air的連線
pIS2Air->StartCon(bServerIP,bServerPort,bm_account,bm_passwd,&ret);
if(ret==0){ //建立連線成功
//開始傳送文字簡訊,如要傳送多筆簡訊,只需在此多次呼叫SendMsg()即可
pIS2Air->SendMsg(bm_msisdn,bm_message,&ret,&mresult);
CString Ret_Result=mresult; //BSTR to CString
Ret_Buffer.Format("傳送完成,ret_code=%d ret_description=",ret);
Ret_Buffer+= Ret_Result;
AfxMessageBox(Ret_Buffer);
}else{
Ret_Buffer.Format("建立連線失敗,ret_code=%d",ret);
AfxMessageBox(Ret_Buffer);
}
pIS2Air->EndCon();
//結束連線
pIS2Air->Release();
}else{
AfxMessageBox("CreateInstance Fail");
}
CoUninitialize();
}else{
AfxMessageBox("Failed to initialize
S2ACOM library");
}
//release BSTR
::SysFreeString(bServerIP);
::SysFreeString(bServerPort);
::SysFreeString(bm_account);
::SysFreeString(bm_passwd);
::SysFreeString(bm_msisdn);
::SysFreeString(bm_message);
//CDialog::OnOK();
}
7. 然後build此project即可。