Socket to Air COM元件使用範例 for VC6
(Console Application)
- 系統需求:Windows
- 程式語言:Visual C++ 6.0
- 應用程式形式:Console Application
- 元件註冊與安裝:(使用前必須先註冊)
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建立專案:專案類型選 Win32 Console Application -> Project name 取名為VC6_Console ->
An empty project -> 然後按Finish即可。
2. 在Source Files中建立新檔vc6_console.cpp。
3. 將S2ACOM_i.c與S2ACOM.h複製到VC6_Console
Project目錄下,並分別加入Source Files與Header Files中。
4. 在vc6_console.cpp加入下列程式碼:
#include <iostream.h>
//----------code added for S2ACOM by chming ----
#include "S2ACOM.h" // this is
a server file
#include <Comdef.h>
// for usage of BSTR
#include <Atlbase.h>
// for usage of CComBSTR
void main(int argc, char *argv[])
{
// TODO: Add extra validation here
char *ServerIP="203.66.172.131";
//Socket to air server ip
char *ServerPort="8000";
//Socket to air server port
char Account[10];
//帳號
char Passwd[10];
//密碼
char Msisdn[12];
//手機號碼
char Message[160];
//簡訊內容
int ret=-99;
//init value
if(argc<5){
printf("\nUsage : vc6_console <Login> <Pw> <TelNum> <Msg> ");
printf("\n <ex> : vc6_console test test1 0932xxxxxx
message");
exit(0);
}
//取得參數值
strcpy(Account, argv[1]);
strcpy(Passwd, argv[2]);
strcpy(Msisdn, argv[3]);
strcpy(Message, argv[4]);
//將char* 轉成BSTR
BSTR bServerIP = _com_util::ConvertStringToBSTR(ServerIP);
BSTR bServerPort = _com_util::ConvertStringToBSTR(ServerPort);
BSTR bAccount = _com_util::ConvertStringToBSTR(Account);
BSTR bPasswd = _com_util::ConvertStringToBSTR(Passwd);
BSTR bMsisdn = _com_util::ConvertStringToBSTR(Msisdn);
BSTR bMessage = _com_util::ConvertStringToBSTR(Message);
//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}};
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,bAccount,bPasswd,&ret);
if(ret==0){
//建立連線成功
//開始傳送文字簡訊,如要傳送多筆簡訊,只需在此多次呼叫SendMsg()即可
pIS2Air->SendMsg(bMsisdn,bMessage,&ret,&mresult);
_bstr_t res =
mresult; //CComBSTR to BSTR
char *ret_description = res; //BSTR to char*
cout <<
"傳送完成,ret_code=" << ret << " ret_description=" << ret_description << endl;
}else{
cout <<
"建立連線失敗,ret_code=" << ret << endl;
}
pIS2Air->EndCon();
//結束連線
pIS2Air->Release();
}else{
cout << "CreateInstance Fail!" <<
endl;
}
CoUninitialize();
}else{
cout << "Failed to initialize S2ACOM library" << endl;
}
//release BSTR
::SysFreeString(bServerIP);
::SysFreeString(bServerPort);
::SysFreeString(bAccount);
::SysFreeString(bPasswd);
::SysFreeString(bMsisdn);
::SysFreeString(bMessage);
}
7. 然後build此project即可。