• <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.¢%

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

            DLL共享變量

            Posted on 2010-02-17 21:42 S.l.e!ep.¢% 閱讀(654) 評論(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


            接下來,實踐證明一下
            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);????
            }

            奇跡般地,兩個EXE拷到不同的地方,運行了兩個實例,它們能夠“數據共享”

            加上下面兩個功能:
            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");
            }

            就會發現,如果當所有EXE的實例都把 DLL 卸裝再 Load 時,DLL的數據會清0.......

            所以,為了“共享數據”,DLL必須至少有一個“引用(被某個EXE引用)”
            久久99精品久久久久久hb无码 | 久久久噜噜噜久久中文福利| 天天综合久久一二三区| 久久国产乱子伦精品免费强| 久久精品九九亚洲精品天堂| 亚洲欧美日韩中文久久| 国内精品伊人久久久久av一坑 | 久久久久久精品免费看SSS| 中文字幕精品久久久久人妻| 久久久精品人妻一区二区三区蜜桃 | 日日狠狠久久偷偷色综合96蜜桃| 办公室久久精品| 久久久国产亚洲精品| 国产午夜久久影院| 久久99国产亚洲高清观看首页| 国产精品久久久久国产A级| 天天躁日日躁狠狠久久| 亚洲色大成网站www久久九| 亚洲欧美精品伊人久久| 久久精品国产2020| 亚洲AV无码久久寂寞少妇| 久久天堂AV综合合色蜜桃网 | 无码人妻久久一区二区三区蜜桃| 久久精品国产亚洲AV无码娇色| 亚洲精品国产美女久久久| 久久ZYZ资源站无码中文动漫| 成人久久精品一区二区三区| 久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 久久久久亚洲国产| 久久人人爽人人爽人人片AV不| 国产综合久久久久| 久久精品中文字幕有码| 精品午夜久久福利大片| 国产亚洲美女精品久久久| 国产精品九九九久久九九| 久久国产精品久久国产精品| 久久无码人妻精品一区二区三区| 777久久精品一区二区三区无码 | 国产精品美女久久久久av爽| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 亚洲日本久久久午夜精品|