- 论坛徽章:
- 59
|
本帖最后由 folklore 于 2013-05-09 15:28 编辑
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <vector>
- #include <sstream>
- #pragma comment(lib,"User32.lib")
- #pragma message("Compiling command(dll): cl.exe /EHsc /Ot /Ox /W4 /LD /DDLL "__FILE__" /link /subsystem:console")
- #pragma message("Compiling command(exe): cl.exe /EHsc /Ot /Ox /W4 "__FILE__" /link /subsystem:console")
- // -- Html Helper function
- static std::wstring HtmlEncode(const std::wstring& html){
- std::wostringstream result;
- for (std::wstring::const_iterator itCh = html.begin(); itCh !=html.end(); itCh++){
- switch(*itCh){
- case L'<':
- result <<L"<";
- break;
- case L'>':
- result <<L">";
- break;
- case L'&':
- result <<L"&";
- break;
- case L'\"':
- result <<L""";
- break;
- default:
- result <<static_cast<wchar_t>(*itCh);
- break;
- }
- }
- return result.str();
- }
- //static std::wstring HtmlDecode(const std::wstring& html)
- //{
- // std::wostringstream result;
- // size_t iParse =0;
- // while(iParse <html.length()){
- // if(html.at(iParse) !=L'&'){
- // result <<html.at(iParse++);
- // }else{// enter slash staus
- // std::wostringstream enctext;
- // while(html.length() >iParse &&html.at(iParse) !=L';'){
- // enctext <<html.at(iParse++);
- // }
- // if(_wcsicmp(enctext.str().c_str(),L"<") ==0){
- // result <<L'<';
- // }else if(_wcsicmp(enctext.str().c_str(),L">") ==0){
- // result <<L'>';
- // }else if(_wcsicmp(enctext.str().c_str(),L"&") ==0){
- // result <<L'&';
- // }else if(_wcsicmp(enctext.str().c_str(),L""") ==0){
- // result <<L'\"';
- // }
- // if(html.length() >iParse) iParse++; // Skip semicolon
- // }
- // }
- // return result.str();
- //}// end function: HtmlDecode
- // -- end: Html helper functions
- struct IOS{
- std::wstring Input;
- std::vector<HWND> Output;
- };
- /// <summary>
- /// Walk through all windows&subwindows that shown in current desktop, and figure out the window which be identified with special UUID prop
- /// </summary>
- /// <see>
- /// See msdn to get more info aboud this function (Relative API "EnumWindows"/"EnumChildWindows")
- /// </see>
- static BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd,_In_ LPARAM lParam){
- struct IOS *pIos =(struct IOS *)lParam;
- HANDLE hData =::GetPropW(hwnd,pIos->Input.c_str());
- if(hData !=NULL){
- pIos->Output.push_back(hwnd);
- EnumChildWindows(hwnd,EnumWindowsProc,lParam);
- }
- return TRUE;
- }// end function: EnumWindowsProc
- // -- local helper function
- static std::wstring _Makeparams(const wchar_t *const pParams){
- std::wstring rsMsg;
- rsMsg.append(L"\t\t\t<Params>\n");
- const wchar_t *pParam =pParams;
- do{
- rsMsg.append(L"\t\t\t\t<Param>");
- rsMsg.append(HtmlEncode(pParam));
- rsMsg.append(L"</Param>\n");
- while(pParam[0] !=NULL){
- pParam++;
- }// end if
- pParam++;
- }while(pParam[0] !=NULL);
- rsMsg.append(L"\t\t\t</Params>\n");
- return rsMsg;
- }
- /// <summary>
- /// MakeMessage: Input the message and translate it to XML format
- /// </summary>
- // The result string looks like
- // <messages>
- // <message ext-code="3">
- // <Title code="35" Description="">
- // <Params>
- // <Param>p1 of content<Param>
- // <Params>
- // </Title>
- // <Content code="53" Description="">
- // <Params>
- // <Param>p1 of content</Param>
- // <Params>
- // </Content>
- // </message>
- // </messages>
- static std::wstring MakeMessage(const wchar_t *const id,const long cdTitle,const wchar_t *const pTitle,const long cdBody,const wchar_t *const pBody,const wchar_t *const paramsTitle=NULL,const wchar_t *const paramsContent=NULL,const long cdExtend=-1){
- (void) id;
- wchar_t intbuf[MAX_PATH]; // Global buffer
- // -- Xml header
- std::wstring rsMsg(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
- rsMsg.append(L"<Messages>\n");
- rsMsg.append(L"\t<Message ext-code=\"");
- _itow_s(cdExtend,intbuf,MAX_PATH,10);
- rsMsg.append(intbuf);
- rsMsg.append(L"\"");
- // -- UUID if specified
- if(id !=NULL){
- rsMsg.append(L" UUID=\"");rsMsg.append(HtmlEncode(id));rsMsg.append(L"\"");
- }//end if
- rsMsg.append(L">\n");
- // -- Title
- _itow_s(cdTitle,intbuf,MAX_PATH,10);
- rsMsg.append(L"\t\t<Title code=\"");rsMsg.append(intbuf);rsMsg.append(L"\"");
- if(pTitle !=NULL){
- rsMsg.append(L" Description=\"");rsMsg.append(HtmlEncode(pTitle));rsMsg.append(L"\"");
- }
- if(paramsTitle !=NULL){
- rsMsg.append(L">\n");
- rsMsg.append(_Makeparams(paramsTitle).c_str());
- rsMsg.append(L"\t\t</Title>\n");
- }else{
- rsMsg.append(L"/>\n");
- }
- // -- Content
- _itow_s(cdBody,intbuf,MAX_PATH,10);
- rsMsg.append(L"\t\t<Content code=\"");rsMsg.append(intbuf);rsMsg.append(L"\"");
- if(pBody !=NULL){
- rsMsg.append(L" Description=\"");rsMsg.append(HtmlEncode(pBody));rsMsg.append(L"\"");
- }
- if(paramsContent !=NULL){
- rsMsg.append(L">\n");
- rsMsg.append(_Makeparams(paramsContent).c_str());
- rsMsg.append(L"\t\t</Content>\n");
- }else{
- rsMsg.append(L"/>\n");
- }
- rsMsg.append(L"\t</Message>\n");
- rsMsg.append(L"</Messages>");
- return rsMsg;
- }// end function: MakeMessage
- /// <summary>
- /// Send message to UI (typically main window) program that registered. The COM component can be executed in remote host.
- /// This function try to send message to *local host* (and typically the local host show a message depend on the message-code).
- /// </summary>
- /// <param name="pUUIDDes">UUID of target window (via get/setprop)</param>
- /// <param name="id">The message id, can be null</param>
- /// <param name="cdTitle">Code of message title</param>
- /// <param name="pTitle">Message title, can be null</param>
- /// <param name="cdBody">Code of message body</param>
- /// <param name="pBody">Message content, can be null</param>
- /// <Return> The number of the target window </Return>
- extern "C" __declspec(dllexport) int SendUIMessage(const wchar_t *const pUUIDDes,wchar_t *const id,const long cdTitle,const wchar_t *const pTitle,const long cdBody,const wchar_t *const pBody,const wchar_t *const paramsTitle=NULL,const wchar_t *const paramsContent=NULL,const long cdExtend=0){
- int nSent =0;
- if(pUUIDDes !=NULL){
- struct IOS ios;
- ios.Input =pUUIDDes;
- ::EnumWindows(EnumWindowsProc,(LPARAM)&ios);
- for(std::vector<HWND>::iterator itHwnd =ios.Output.begin(); itHwnd !=ios.Output.end();itHwnd++){
- std::wstring msgstr =MakeMessage(id,cdTitle,pTitle,cdBody,pBody,paramsTitle,paramsContent,cdExtend);
- COPYDATASTRUCT msg ={
- NULL,(msgstr.size()+1)<<1,(LPVOID)msgstr.c_str()
- };
- ::SendMessage(*itHwnd,WM_COPYDATA,NULL,(LPARAM)&msg);
- //MessageBoxW(*itHwnd,L"OK",L"OK",MB_OK); // -- Try to show message at destinate window
- nSent++;
- }
- }// end if
- return nSent;
- }
- #if !defined(DLL)
- static const wchar_t *const UUIDBendCAM =L"{3B0C708E-8014-4f33-BFC8-4AE1E8F3901C}";
- static size_t dntwcslen(const wchar_t *const pSrc){ //double null terminal string
- size_t rsSize =0;
- if(pSrc !=NULL){
- do{
- while(pSrc[rsSize] !=NULL){rsSize++;}
- rsSize++;
- }while(pSrc[rsSize] !=NULL);
- }
- return rsSize;
- }
- int main(void){
- const wchar_t *const pParams =L"Param<01>\0Param02\0";
- std::wstring params;
- params.resize(dntwcslen(pParams));
- for(size_t iAssign =0;iAssign <params.size();iAssign++){ // the size is not include the last terminal tocken
- params.at(iAssign) =pParams[iAssign];
- //wprintf(L"%c",params.at(iAssign));
- }
- //wprintf(L"\n%d",dntwcslen(pParams));
- if(0 ==SendUIMessage(UUIDBendCAM,NULL,123,L"Title&b",456,L"Context: <happy new year.>",params.data(),params.data())){
- std::wstring msg=MakeMessage(NULL,123,L"Title&b",456,L"Context: <happy new year.>",params.data()/*,params.data()*/);
- wprintf(L"%s",msg.c_str());
- }
- return 0;
- }
- #endif //!defined(DLL)
复制代码 new version
|
|