本帖最后由 believe_me 于 2012-02-03 11:39:07 编辑

解决方案 »

  1.   

    // tell compiler
    #pragma data_seg ("Shared")
    long preins=0;// has to been initialized
    #pragma data_seg ()
    // tell link
    #pragma comment (linker," -section:Shared,rws") 
      

  2.   

    我这个写法应该是没错的啊#pragma data_seg("SHARED")
    //路径
    CHAR g_szAppPath[MAX_PATH] = {0};
    #pragma data_seg()
    #pragma comment(linker, "/section:SHARED,RWS")
      

  3.   

    动态库中调用FilterApp要在SetAppPath后。
    或者:
    char g_szAppPath[MAX_PATH] = "some path";
    试试。
      

  4.   

    上面这段代码是在同一个DLL代码里?
      

  5.   


    是的 在同一个DLL里。
      

  6.   

    补充一下,我是在程序1里面动态调用接口SetAppPath设置g_szAppPath的,在程序2里面调用动态库的FilterApp时g_szAppPath依然无效的。
      

  7.   

    在程序1里面动态调用接口SetAppPath设置g_szAppPath,在程序1里面再调用动态库的FilterApp试试对不对。
      

  8.   

    修正一下,我是在程序1里面动态调用接口SetAppPath设置g_szAppPath的,在动态库DLL1里面调用该动态库的FilterApp接口时g_szAppPath依然无效的。
      

  9.   

    把g_szAppPath给extern了
    程序1和动态库1都直接打印g_szAppPath看一下
      

  10.   

    在动态库中添加了一个接口,用来返回g_szAppPath:VOID WSPAPI GetAppPath(char * szAppPath){
    memcpy(szAppPath, g_szAppPath, MAX_PATH);
            //此处打印日志返回依然为空
    DoLog("Get app path - %s", g_szAppPath);
    }
    在程序1设置完g_szAppPath后立刻调用GetAppPath来取g_szAppPath的值:typedef
    void (*LPFN_GETAPPPATH)(char * szPath);//...void GetFilterAppPath(const char * szLspPath)
    {
    HMODULE hMod = NULL;
    LPFN_GETAPPPATH fnGetAppPath = NULL; // Load teh library
    hMod = LoadLibraryA( szLspPath );
    if ( NULL == hMod )
    {
    fprintf( stderr, "fnGetAppPath: LoadLibraryA failed: %d\n", GetLastError() );
    goto cleanup;
    } // Get a pointer to the LSPs GetLspGuid function
    fnGetAppPath = (LPFN_GETAPPPATH) GetProcAddress( hMod, "GetAppPath");
    if ( NULL == fnGetAppPath )
    {
    fprintf( stderr, "RetrieveLspGuid: GetProcAddress failed: %d\n", GetLastError() );
    goto cleanup;
    }
    char szAppPath[MAX_PATH];
    memset(szAppPath, 0, MAX_PATH);
    fnGetAppPath(szAppPath);
    //此处打印出来也为空
    TRACE1("====== GET APP PATH - %s======\n", szAppPath);
    cleanup: if ( NULL != hMod )
    FreeLibrary( hMod );
    }已经hold不住了,是不是我的共享数据段用法有问题?
      

  11.   

    动态库中调用FilterApp要在SetAppPath后。
      

  12.   

    是的啊,设置和取值的接口中都卸载了。
    设置方法如下面://设置要过滤的应用程序路径
    void SetFilterAppPath(const char * szLspPath, const char * szAppPath)
    {
    HMODULE hMod = NULL;
    LPFN_SETAPPPATH fnSetAppPath = NULL; // Load teh library
    hMod = LoadLibraryA( szLspPath );
    if ( NULL == hMod )
    {
    fprintf( stderr, "fnSetAppPath: LoadLibraryA failed: %d\n", GetLastError() );
    goto cleanup;
    } // Get a pointer to the LSPs GetLspGuid function
    fnSetAppPath = (LPFN_SETAPPPATH) GetProcAddress( hMod, "SetAppPath");
    if ( NULL == fnSetAppPath )
    {
    fprintf( stderr, "RetrieveLspGuid: GetProcAddress failed: %d\n", GetLastError() );
    goto cleanup;
    } fnSetAppPath(szAppPath);
    TRACE1("====== SET APP PATH - %s======\n", szAppPath);
    cleanup: if ( NULL != hMod )
    FreeLibrary( hMod );
    }
      

  13.   

    你freelibrary后,共享的东西部在内存中了。
      

  14.   

    如果让设置的值有效,设置后,打开的DLL句柄就不能卸载掉了?
    我添加一个全局的句柄,在项目退出的时候再freelibrary试试。