• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            c++中使用com的方法

            簡單模擬:

            #include? " ClassDll.h "
            #include?
            " ClassDll_i.c "


            if ?(?FAILED(?CoInitialize(NULL)?))
            {
            return ;
            }


            IClass
            * ?pIClass;

            hr?
            = ?CoCreateInstance(CLSID_Class,
            NULL,
            CLSCTX_INPROC_SERVER,
            IID_IClass,
            (
            void ** )? & pIClass);

            if ?(?SUCCEEDED(hr)?)
            {
            // hr?=?pIClass->Method();

            if ?(?SUCCEEDED(hr)?)
            {
            }

            else
            {
            }


            pIClass
            -> Release();
            }


            CoUninitialize();


            具體實例:
            LOCAL COM Server:

            import? " oaidl.idl " ;

            // ?define?IStats?interface
            [ object ,?uuid(FE78387F - D150 - 4089 - 832C - BBF02402C872),
            ?oleautomation,?helpstring(
            " Get?the?status?information?about?this?car " )]
            interface ?IStats?:?IUnknown
            {
            ???HRESULT?DisplayStats();
            ???HRESULT?GetPetName([
            out ,retval]?BSTR * ?petName);
            }
            ;

            // ?define?the?IEngine?interface
            [ object ,?uuid(E27972D8 - 717F - 4516 - A82D - B688DC70170C),
            ?oleautomation,?helpstring(
            " Rev?your?car?and?slow?it?down " )]
            interface ?IEngine?:?IUnknown
            {
            ???HRESULT?SpeedUp();
            ???HRESULT?GetMaxSpeed([
            out ,retval]? int * ?maxSpeed);
            ???HRESULT?GetCurSpeed([
            out ,retval]? int * ?curSpeed);
            }
            ;

            // ?define?the?ICreateMyCar?interface
            [ object ,?uuid(5DD52389 - B1A4 - 4fe7 - B131 - 0F8EF73DD175),
            ?oleautomation,?helpstring(
            " This?lets?you?create?a?car?object " )]
            interface ?ICreateMyCar?:?IUnknown
            {
            ???HRESULT?SetPetName([
            in ]BSTR?petName);
            ???HRESULT?SetMaxSpeed([
            in ]? int ?maxSp);
            }
            ;

            // ?library?statement
            [uuid(957BF83F - EE5A - 42eb - 8CE5 - 6267011F0EF9),?version( 1.0 ),
            ?helpstring(
            " Car?server?with?typeLib " )]
            library?CarLocalServerLib
            {
            ???importlib(
            " stdole32.tlb " );
            ???[uuid(1D66CBA8
            - CCE2 - 4439 - 8596 - 82B47AA44E43)]
            ???coclass?MyCar
            ???
            {
            ??????[
            default ]? interface ?ICreateMyCar;
            ??????
            interface ?IStats;
            ??????
            interface ?IEngine;
            ???}
            ;
            }
            ;


            LOCAL COM Client:


            #include?"../CarLocalServer/CarLocalServerTypeInfo.h"????//?use?your?own?path?here
            #include?"../CarLocalServer/CarLocalServerTypeInfo_i.c"??//?use?your?own?path?here
            #include?"iostream.h"

            //?for?showing?possible?mistakes
            void?ShowErrorMessage(LPCTSTR,HRESULT);

            int?main()
            {
            ???
            //?initialize?the?COM?runtime
            ???cout?<<?"Initialize?the?COM?runtime";
            ???CoInitialize(NULL);
            ???cout?
            <<?"success."?<<?endl;

            ???
            //?declare?variables
            ???HRESULT?hr;
            ???IClassFactory
            *?pICF?=?NULL;
            ???ICreateMyCar
            *??pICreateMyCar?=?NULL;
            ???IEngine
            *???????pIEngine?=?NULL;
            ???IStats
            *????????pIStats?=?NULL;

            ???cout?
            <<?endl?<<?"Get?the?class?factory?interface?for?the?Car?class";
            ???hr?
            =?CoGetClassObject(CLSID_MyCar,CLSCTX_LOCAL_SERVER,?
            ?????????????????????????NULL,IID_IClassFactory,(
            void**)&pICF);
            ???
            if?(?FAILED(hr)?)
            ???
            {
            ??????ShowErrorMessage(
            "CoGetClassObject()",hr);
            ??????exit(
            1);
            ???}

            ???
            else?cout?<<?"success."?<<?endl;

            ???cout?
            <<?"Create?the?Car?object?and?get?back?the?ICreateMyCar?interface";
            ???hr?
            =?pICF->CreateInstance(NULL,IID_ICreateMyCar,(void**)&pICreateMyCar);
            ???
            if?(?FAILED(hr)?)
            ???
            {
            ??????ShowErrorMessage(
            "CoCreateInstance()",hr);
            ??????exit(
            1);
            ???}

            ???
            else?cout?<<?"success."?<<?endl;

            ???
            //?set?parameters?on?the?car
            ???cout?<<?endl?<<?"Set?different?parameters?on?the?car";
            ???pICreateMyCar
            ->SetMaxSpeed(30);
            ???BSTR?carName?
            =?SysAllocString(OLESTR("COMCar?!"));
            ???pICreateMyCar
            ->SetPetName(carName);
            ???SysFreeString(carName);
            ???cout?
            <<?"success."?<<?endl;

            ???cout?
            <<?endl?<<?"Query?the?IStats?interface";
            ???pICreateMyCar
            ->QueryInterface(IID_IStats,(void**)&pIStats);
            ???cout?
            <<?"success."?<<?endl;

            ???cout?
            <<?endl?<<?"Use?the?IStats?interface?to?
            ????????????????????display?the?status?of?the?car:"?<<?endl;
            ???pIStats->DisplayStats();

            ???cout?
            <<?endl?<<?"Query?the?IEngine?interface";
            ???pICreateMyCar
            ->QueryInterface(IID_IEngine,(void**)&pIEngine);
            ???cout?
            <<?"success."?<<?endl;

            ???cout?
            <<?endl?<<?"Start?to?use?the?engine"?<<?endl;
            ???
            int?curSp?=?0;
            ???
            int?maxSp?=?0;
            ???pIEngine
            ->GetMaxSpeed(&maxSp);
            ???
            do
            ???
            {
            ??????pIEngine
            ->SpeedUp();
            ??????pIEngine
            ->GetCurSpeed(&curSp);
            ??????cout?
            <<?"current?speed?is:?"?<<?curSp?<<?endl;
            ???}
            ?while?(curSp?<=?maxSp);

            ???
            if?(?pICF?)?????????pICF->Release();
            ???
            if?(?pICreateMyCar)?pICreateMyCar->Release();
            ???
            if?(?pIStats?)??????pIStats->Release();
            ???
            if?(?pIEngine?)?????pIEngine->Release();

            ???cout?
            <<?endl?<<?"Close?the?COM?runtime";
            ???CoUninitialize();
            ???cout?
            <<?"success."?<<?endl;
            ???
            return?0;
            }


            void?ShowErrorMessage(LPCTSTR?header,?HRESULT?hr)
            {
            ???
            void*?pMsg;

            ???FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER?
            |?
            ?????????????????FORMAT_MESSAGE_FROM_SYSTEM,NULL,hr,
            ?????????????????MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),?
            ?????????????????(LPTSTR)
            &pMsg,0,NULL);

            ???cout?
            <<?header?<<?":?Error("?<<?hex?<<?hr?<<?"):?"?
            ????????
            <<?(LPTSTR)pMsg?<<?endl;

            ???LocalFree(pMsg);
            }

            參考: http://www.codeproject.com/com/LocalCOMServerClient.asp

            posted on 2005-12-24 14:53 夢在天涯 閱讀(3166) 評論(0)  編輯 收藏 引用 所屬分類: CPlusPlusCOM/ATL

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1805095
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产精品成人无码久久久久久 | 国产激情久久久久影院| 婷婷久久香蕉五月综合加勒比| 欧美一区二区三区久久综合| 久久亚洲精品视频| 亚洲成av人片不卡无码久久| 91精品国产色综合久久| 久久国产福利免费| 久久久久久久久无码精品亚洲日韩| 国产精品成人99久久久久 | 亚洲日韩中文无码久久| 国内精品久久久久影院免费| 久久久这里有精品| 久久综合欧美成人| 国内精品久久久久影院日本| 亚洲国产日韩欧美综合久久| 久久久久综合网久久| 人妻精品久久久久中文字幕69| 色婷婷久久久SWAG精品| 国产成人精品久久亚洲高清不卡 | 亚洲乱码精品久久久久..| 久久久久亚洲精品中文字幕| 精品久久一区二区三区| 亚洲va久久久噜噜噜久久狠狠| 亚洲欧洲久久av| 欧美一级久久久久久久大片| 久久噜噜电影你懂的| 狠色狠色狠狠色综合久久| 久久国产色AV免费观看| 久久国产免费直播| 伊人久久大香线蕉精品| 伊人久久大香线蕉综合影院首页| 久久婷婷色综合一区二区| 久久久WWW成人免费精品| 久久福利青草精品资源站| 精品无码久久久久久尤物| 无码日韩人妻精品久久蜜桃| 亚洲va国产va天堂va久久| 久久久久久久久无码精品亚洲日韩| 亚洲伊人久久精品影院| 天天躁日日躁狠狠久久|