DLL共享段数据设置成功,读取失败。 本帖最后由 believe_me 于 2012-02-03 11:39:07 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // tell compiler#pragma data_seg ("Shared")long preins=0;// has to been initialized#pragma data_seg ()// tell link#pragma comment (linker," -section:Shared,rws") 我这个写法应该是没错的啊#pragma data_seg("SHARED")//路径CHAR g_szAppPath[MAX_PATH] = {0};#pragma data_seg()#pragma comment(linker, "/section:SHARED,RWS") 动态库中调用FilterApp要在SetAppPath后。或者:char g_szAppPath[MAX_PATH] = "some path";试试。 上面这段代码是在同一个DLL代码里? 是的 在同一个DLL里。 补充一下,我是在程序1里面动态调用接口SetAppPath设置g_szAppPath的,在程序2里面调用动态库的FilterApp时g_szAppPath依然无效的。 在程序1里面动态调用接口SetAppPath设置g_szAppPath,在程序1里面再调用动态库的FilterApp试试对不对。 修正一下,我是在程序1里面动态调用接口SetAppPath设置g_szAppPath的,在动态库DLL1里面调用该动态库的FilterApp接口时g_szAppPath依然无效的。 把g_szAppPath给extern了程序1和动态库1都直接打印g_szAppPath看一下 在动态库中添加了一个接口,用来返回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的值:typedefvoid (*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不住了,是不是我的共享数据段用法有问题? 动态库中调用FilterApp要在SetAppPath后。 是的啊,设置和取值的接口中都卸载了。设置方法如下面://设置要过滤的应用程序路径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 );} 你freelibrary后,共享的东西部在内存中了。 如果让设置的值有效,设置后,打开的DLL句柄就不能卸载掉了?我添加一个全局的句柄,在项目退出的时候再freelibrary试试。 求类似于QQ宠物 ,瑞星小狮子怎么做的。 ████████新手的关于ODBC连接数据库的疑问████████████████ 简单局域网通信程序 单文档应用程序,如何在文档doc内获取视图view 在VC中如何调试多线程的程序? 新学VC,不知道这些代码是什么意思~ 请教高手台球问题:关于台球运动轨迹和角度!! 出售〈MFC WINDOWS程序设计〉 全新 地点长沙! 如何用MFC编写一个代理服务器 POP3问题:为什么我不能连续得到下一封邮件的信息? spin 旋转按钮控件,调节增加或减小的幅度 关于如何动态调整ActiveX控件的大小问题,内有详细问题描述
#pragma data_seg ("Shared")
long preins=0;// has to been initialized
#pragma data_seg ()
// tell link
#pragma comment (linker," -section:Shared,rws")
//路径
CHAR g_szAppPath[MAX_PATH] = {0};
#pragma data_seg()
#pragma comment(linker, "/section:SHARED,RWS")
或者:
char g_szAppPath[MAX_PATH] = "some path";
试试。
是的 在同一个DLL里。
程序1和动态库1都直接打印g_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不住了,是不是我的共享数据段用法有问题?
设置方法如下面://设置要过滤的应用程序路径
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 );
}
我添加一个全局的句柄,在项目退出的时候再freelibrary试试。