想在DLL中管理一个全局动态链表,以便于多个应用程序可以访问,请高手指点下实现方法,

解决方案 »

  1.   

    在动态库中将链表首部的指针定义成一个静态共享变量
    #pragma data_seg(".shlist")
    static void *pListHead = NULL;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.shlist,RWS")
    然后动态库中所有链表操作都基于该链表首部来完成
    另外在动态库中通过创建全局的指定名称的信号量或互斥量来在多个应用程序的操作中实现同步
      

  2.   

    楼上的正解。。
    不能用CRITICAL_SECITON来同步。
    因为是跨进程。。
      

  3.   

    To QrMs,谢谢指点
    我试了一下,没有达到在两个进程间共享的要求 不知道是不是写得有问题,请帮助分析一下.h文件
    ---------------------------------------------------
    #define DLLExport  _declspec(dllexport)
    class  CItem{
    public:
       int MsgID;
       int iValue;
       CItem *pNext;
    } ;
    class DLLExport CMsgDB{
    public:
    CMsgDB();
    virtual ~CMsgDB();
    public:
    void AddItem(int iMsgID,int iValue);
        const CItem* GetHead();
    const CItem* GetRoot();
    BOOL  UpData(int iMsgID,int iValue);
    int GetValue(int iMsgID);
    CItem* FindItem(int iMsgID);     
       
    };
    ---------------------------------------------------
    .CPP文件
    ---------------------------------------------------
    #include "stdafx.h"
    #include "MsgDB.h"#pragma data_seg(".shlist")
    static CItem *pListHead = NULL;
    static CItem *pListRoot = NULL;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.shlist,RWS")
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
     )
    {
        return TRUE;
    }
    CMsgDB::CMsgDB()
    {
    }
    CMsgDB::~CMsgDB()
    {}
    void CMsgDB::AddItem(int iMsgID,int iValue)
    {
    CItem *tmpItem=new CItem();
    tmpItem->iValue=iValue;
    tmpItem->MsgID=iMsgID;
    tmpItem->pNext=NULL;
    if(pListHead==NULL){
       pListHead=tmpItem;
           pListRoot=pListHead;
    }else{
        ((CItem *)pListRoot)->pNext=tmpItem;
            pListRoot=tmpItem; 
    }}
    const CItem* CMsgDB::GetHead()
    {
     return (CItem*)pListHead;
    }const CItem* CMsgDB::GetRoot()
    {
     return (CItem*)pListRoot;
    }
    CItem* CMsgDB::FindItem(int iMsgID)
    {
        CItem *tmpItem;
    if(pListHead==NULL){return FALSE;}
    tmpItem=pListHead;
    while(1){
       if(tmpItem->MsgID==iMsgID) break;
       if(tmpItem==pListRoot){tmpItem=NULL; break;}
       tmpItem=tmpItem->pNext;
    }
     
    return tmpItem;
    }BOOL  CMsgDB::UpData(int iMsgID,int iValue)
    {

    CItem *tmpItem=FindItem(iMsgID);
    if(tmpItem==NULL) return FALSE;
    tmpItem->iValue=iValue;
    return TRUE;
    }
    int  CMsgDB::GetValue(int iMsgID)
    {

    CItem *tmpItem=FindItem(iMsgID);
    if(tmpItem==NULL) return -1;
    return tmpItem->iValue ;
     
    }