國內主流簡訊貓c++介面描述和簡訊解析程式碼
阿新 • • 發佈:2019-01-09
最近開發簡訊貓,用了sms.dll,網上一查,好像大家都是用這個。是“上海迅賽資訊科技有限公司”開發的,還在裡面留了這個字串。
因為網上有c#程式碼,沒有c++程式碼,又沒有文件,摸索了很久,才找到了最終的引數描述:
HINSTANCE hDllInst = LoadLibrary("sms.DLL"); if(hDllInst) { typedef unsigned int (WINAPI *Sms_ConnectionF)(const char*,int,int,LPCTSTR*,LPCTSTR*); typedef unsigned int (WINAPI *Sms_ReceiveF)(const char*,LPCTSTR *); typedef unsigned int (WINAPI *Sms_AutoFlagF)(); typedef unsigned int (WINAPI *Sms_NewFlagF)(); typedef unsigned int (WINAPI *Sms_DeleteF)(const char*); typedef int (WINAPI *Sms_SendF)(const char*,const char*); Sms_ConnectionF Sms_Connection = NULL; Sms_SendF Sms_Send = NULL; Sms_ReceiveF Sms_Receive = NULL; Sms_NewFlagF Sms_NewFlag = NULL; Sms_AutoFlagF Sms_AutoFlag = NULL; Sms_DeleteF Sms_Delete = NULL; Sms_Connection = (Sms_ConnectionF)GetProcAddress(hDllInst,"Sms_Connection"); Sms_Send = (Sms_SendF)GetProcAddress(hDllInst,"Sms_Send"); Sms_Receive = (Sms_ReceiveF)GetProcAddress(hDllInst,"Sms_Receive"); Sms_NewFlag = (Sms_NewFlagF)GetProcAddress(hDllInst,"Sms_NewFlag"); Sms_AutoFlag = (Sms_AutoFlagF)GetProcAddress(hDllInst,"Sms_AutoFlag"); Sms_Delete = (Sms_DeleteF)GetProcAddress(hDllInst,"Sms_Delete");
……
關於簡訊接收出來的簡訊的格式是:|1#04#8613970234430#123#11-07-12 10:00:17
分別是id type 手機號 內容 時間
我寫了一個函式分析簡訊,放到vector裡面。這裡需要考慮的是使用者輸入了#或|而不會影響整個解析崩潰。
程式碼如下:
struct Sms{ char id[50]; char type[50]; char mobile[50]; char content[200]; char time[50]; bool parseOk; }; void parseSms(const char* contentAll,vector<Sms> & sms) { if (contentAll==0) return; int i=0;//遍歷contentall用, int status=0;//0 沒開始or 找到時間,1 找到| , 2 找到型別 3 找到手機 ,4找到內容 //|1#04#8613970234430#123#11-07-12 10:00:17 //1 簡訊索引號 //04 簡訊型別 //8613970234430 手機號碼 //123 回覆內容 //時間 Sms sms_; while(1) { switch(status) { case 0: while (contentAll[i]!='\0'&&contentAll[i]!='|') i++; if(contentAll[i]=='|') { status=1; char b[50]={0}; int j=0; i++; while (contentAll[i]!='\0'&&contentAll[i]!='#') { if(j>10||contentAll[i]<'0' ||contentAll[i]>'9')//id分析出錯,回到status 0 { status=0; break; } b[j++]=contentAll[i++]; } if(contentAll[i]=='\0') return; memset(sms_.id,0,50); strcpy(sms_.id ,b); } else return; break; case 1: if(contentAll[i]=='#') { status=2; char b[50]={0}; int j=0; i++; while (contentAll[i]!='\0'&&contentAll[i]!='#') { if(j>10)//type分析出錯,回到status 0 { status=0; break; } b[j++]=contentAll[i++]; } if(contentAll[i]=='\0') return; memset(sms_.type,0,50); strcpy(sms_.type ,b); if(status==0)//出錯不能丟棄,放在vector,出去後需要對id呼叫,刪除sim卡中簡訊 { sms_.parseOk=false; sms.push_back(sms_); } } break; case 2: if(contentAll[i]=='#') { status=3; char b[50]={0}; int j=0; i++; while (contentAll[i]!='\0'&&contentAll[i]!='#') { if(j>30)//mobile分析出錯,回到status 0 { status=0; break; } b[j++]=contentAll[i++]; } if(contentAll[i]=='\0') return; memset(sms_.mobile,0,50); strcpy(sms_.mobile ,b); if(status==0) { sms_.parseOk=false; sms.push_back(sms_); } } break; case 3://找內容,要考慮使用者輸入了| 和 # if(contentAll[i]=='#') { i++; //找到下一個|或結尾 int endForThisMsg=0; int i_=i; bool foundJinghao=false;//發現警號,必須發現#再發現|,只發現|,跳過 while (contentAll[i_]!='\0'&&(contentAll[i_]!='|'||!foundJinghao)) if(contentAll[i_++]=='#') foundJinghao=true; endForThisMsg = i_; //找到本節最後一個# i_=endForThisMsg; while(contentAll[i_--]!='#') ; endForThisMsg = i_; if(endForThisMsg - i +1>=0&&endForThisMsg - i +1<200)//緩衝區200,分析出超過200 的簡訊認為出錯,拋棄,進入下一條 { memset(sms_.content,0,200); memcpy(sms_.content ,contentAll+i,endForThisMsg - i +1); status=4; i=endForThisMsg+1; } else { sms_.parseOk=false; sms.push_back(sms_); status=0; } } break; case 4: if(contentAll[i]=='#') { status=0; char b[50]={0}; int j=0; i++; bool breakF=false; while (contentAll[i]!='\0'&&contentAll[i]!='|') { if(j>=49)//時間分析出錯,回到status 0 { status=0; breakF=true; break; } b[j++]=contentAll[i++]; } if(breakF) { sms_.parseOk=false; sms.push_back(sms_); break; } if(j<14)//時間沒找到,可能是使用者有輸入#| , 回到status 0 { sms_.parseOk=false; sms.push_back(sms_); break; } memset(sms_.time,0,50); strcpy(sms_.time,b); //插入vector返回值 sms_.parseOk=true; sms.push_back(sms_); } break; } } }