• <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>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開心的工作
            簡(jiǎn)單、開放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            DLL共享變量

            Posted on 2010-02-17 21:42 S.l.e!ep.¢% 閱讀(657) 評(píng)論(0)  編輯 收藏 引用 所屬分類: VC

            // ?The?following?ifdef?block?is?the?standard?way?of?creating?macros?which?make?exporting?
            // ?from?a?DLL?simpler.?All?files?within?this?DLL?are?compiled?with?the?TESTDLL_EXPORTS
            // ?symbol?defined?on?the?command?line.?this?symbol?should?not?be?defined?on?any?project
            // ?that?uses?this?DLL.?This?way?any?other?project?whose?source?files?include?this?file?see?
            // ?TESTDLL_API?functions?as?being?imported?from?a?DLL,?wheras?this?DLL?sees?symbols
            // ?defined?with?this?macro?as?being?exported.
            #ifdef?TESTDLL_EXPORTS
            #define ?TESTDLL_API?__declspec(dllexport)
            #else
            #define ?TESTDLL_API?__declspec(dllimport)
            #endif

            #pragma?data_seg?(
            " .shared " )
            int ?g_TestValue? = ? 0 ;
            #pragma?data_seg?()

            #pragma?comment(linker,?
            " /SECTION:.shared,RWS " )?


            TESTDLL_API?
            int ?getTestValue( void );
            TESTDLL_API?
            void ?setTestValue( int ?nValue);



            // ?testDLL.cpp?:?Defines?the?entry?point?for?the?DLL?application.
            //

            #include?
            " stdafx.h "
            #include?
            " testDLL.h "

            BOOL?APIENTRY?DllMain(?HANDLE?hModule,?
            ???????????????????????DWORD??ul_reason_for_call,?
            ???????????????????????LPVOID?lpReserved
            ?????????????????????)
            {
            ????
            switch ?(ul_reason_for_call)
            ????{
            ????????
            case ?DLL_PROCESS_ATTACH:
            ????????
            case ?DLL_THREAD_ATTACH:
            ????????
            case ?DLL_THREAD_DETACH:
            ????????
            case ?DLL_PROCESS_DETACH:
            ????????????
            break ;
            ????}
            ????
            return ?TRUE;
            }

            // ?This?is?an?example?of?an?exported?function.
            TESTDLL_API? int ?getTestValue( void )
            {
            ????
            return ?g_TestValue;
            }

            TESTDLL_API?
            void ?setTestValue( int ?nValue)
            {
            ????g_TestValue?
            = ?nValue;
            }


            EXPORTS
            ????getTestValue?@
            1
            ????setTestValue?@
            2


            接下來(lái),實(shí)踐證明一下
            BOOL?CTestCallDLLDlg::OnInitDialog()
            {
            ????CDialog::OnInitDialog();

            ????????
            //?
            ????
            ????
            //?TODO:?Add?extra?initialization?here

            ????m_hModule?
            =?::LoadLibrary("C:\\Documents?and?Settings\\Administrator\\桌面\\testDLL.dll");

            ????
            ????
            return?TRUE;??//?return?TRUE??unless?you?set?the?focus?to?a?control
            }

            void?CTestCallDLLDlg::OnButton1()?
            {
            ????typedef?
            int?(*getTestValueType)(void);
            ????getTestValueType?pfngetTestValue?
            =?(getTestValueType)::GetProcAddress(m_hModule,?"getTestValue");

            ????
            int?i?=?pfngetTestValue();

            ????CString?str;
            ????str.Format(
            "The?Value?now?is?%d",?i);
            ????AfxMessageBox(str);
            }

            void?CTestCallDLLDlg::OnButton2()?
            {
            ????typedef?
            int?(*getTestValueType)(void);
            ????getTestValueType?pfngetTestValue?
            =?(getTestValueType)::GetProcAddress(m_hModule,?"getTestValue");

            ????typedef?
            void?(*setTestValueType)(int?nValue);
            ????setTestValueType?pfnsetTestValue?
            =?(setTestValueType)::GetProcAddress(m_hModule,?"setTestValue");

            ????
            int?i?=?pfngetTestValue()?+?1;
            ????pfnsetTestValue(i);
            ????i?
            =?pfngetTestValue();

            ????CString?str;
            ????str.Format(
            "The?Value?now?is?%d",?i);
            ????AfxMessageBox(str);????
            }

            奇跡般地,兩個(gè)EXE拷到不同的地方,運(yùn)行了兩個(gè)實(shí)例,它們能夠“數(shù)據(jù)共享”

            加上下面兩個(gè)功能:
            void?CTestCallDLLDlg::OnButton3()?
            {
            ????::FreeLibrary(m_hModule);
            ????m_hModule?
            =?NULL;
            }

            void?CTestCallDLLDlg::OnButton4()?
            {
            ????
            //?TODO:?Add?your?control?notification?handler?code?here
            ????m_hModule?=?::LoadLibrary("C:\\Documents?and?Settings\\Administrator\\桌面\\testDLL.dll");
            }

            就會(huì)發(fā)現(xiàn),如果當(dāng)所有EXE的實(shí)例都把 DLL 卸裝再 Load 時(shí),DLL的數(shù)據(jù)會(huì)清0.......

            所以,為了“共享數(shù)據(jù)”,DLL必須至少有一個(gè)“引用(被某個(gè)EXE引用)”
            亚洲Av无码国产情品久久| 久久久久国产精品嫩草影院| 久久精品亚洲AV久久久无码| 久久精品国产精品亚洲艾草网美妙| 久久精品国产亚洲AV电影 | 一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 久久婷婷五月综合色奶水99啪| 国产精品久久久久久福利漫画 | 精品国产乱码久久久久久郑州公司| 久久一区二区三区99| 久久国产香蕉视频| 亚洲精品乱码久久久久久不卡| 狠狠色丁香婷婷久久综合五月| 色播久久人人爽人人爽人人片aV | 国产亚洲婷婷香蕉久久精品| 久久久久久综合一区中文字幕| 久久综合综合久久狠狠狠97色88| 99精品伊人久久久大香线蕉| 久久久WWW成人| 久久综合久久美利坚合众国| 奇米影视7777久久精品| 久久精品国产一区二区三区日韩| 99久久无码一区人妻| 日产久久强奸免费的看| 浪潮AV色综合久久天堂| 久久国产精品久久国产精品| 色欲综合久久躁天天躁| 久久99国产乱子伦精品免费| 精品久久久久久久久久中文字幕| 久久久久亚洲AV片无码下载蜜桃 | 久久免费精品一区二区| 日本精品久久久久久久久免费| 亚洲AV无码一区东京热久久| 国内精品久久久久久中文字幕 | 久久无码中文字幕东京热| 久久国产免费观看精品3| 久久久久亚洲精品中文字幕| 久久精品黄AA片一区二区三区| 久久午夜福利电影| 久久综合综合久久狠狠狠97色88| 一级女性全黄久久生活片免费 |