dll怎么能获取自身所在的目录?

3m2u 2006-10-31 12:17:43
在dll程序内部,
发现用以下的办法是可以的
::GetModuleFileName(GetModuleHandle( DLL_FILE_NAME ),m_name,MAX_PATH);

不过前提是知道DLL_FILE_NAME
有没有办法不通过dll文件的名字来取得其路径呢?
...全文
652 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
KeSummer 2006-11-01
  • 打赏
  • 举报
回复
我的方法~~只需要包含windows.h即可
// 一个通过内存地址取得模块句柄的帮助函数
HMODULE WINAPI ModuleFromAddress(PVOID pv)
{
MEMORY_BASIC_INFORMATION mbi;
if(::VirtualQuery(pv, &mbi, sizeof(mbi)) != 0)
{
return (HMODULE)mbi.AllocationBase;
}
else
{
return NULL;
}
}
//下面是获得DLL自身的路径,它通过DLL入口DllMain的地址去获得的.很经典的代码值得收藏
char dllpath[MAX_PATH];

::memset(dllpath,0,sizeof(dllpath));

::GetModuleFileName(ModuleFromAddress(DllMain),dllpath,MAX_PATH);
大选 2006-11-01
  • 打赏
  • 举报
回复
可以弄個ini文件啊.裡面寫好.
aa3000 2006-10-31
  • 打赏
  • 举报
回复
如果在那个DLL入口有个HMODULE,用这个来传进去就可以找到了
如果没有这个HMODULE那么使用下面这段代码先求出这个HMODULE:
Most DLL developers have faced the challenge of detecting a HMODULE/HINSTANCE handle within the module you're running in. It may be a difficult task if you wrote the DLL without a DLLMain() function or you are unaware of its name. For example:

Your DLL was built without ATL/MFC, so the DLLMain() function exists, but it's hidden from you code and you cannot access the hinstDLL parameter. You do not know the DLL's real file name because it could be renamed by everyone, so GetModuleHandle() is not for you.

This small code can help you solve this problem:

#if _MSC_VER >= 1300 // for VC 7.0
// from ATL 7.0 sources
#ifndef _delayimp_h
extern "C" IMAGE_DOS_HEADER __ImageBase;
#endif
#endif

GetCurrentModule()
{
#if _MSC_VER < 1300 // earlier than .NET compiler (VC 6.0)

// Here's a trick that will get you the handle of the module
// you're running in without any a-priori knowledge:
// http://www.dotnet247.com/247reference/msgs/13/65259.aspx

MEMORY_BASIC_INFORMATION mbi;
static int dummy;
VirtualQuery( &dummy, &mbi, sizeof(mbi) );
HMODULE
return reinterpret_cast<HMODULE>(mbi.AllocationBase);

#else // VC 7.0

// from ATL 7.0 sources

return reinterpret_cast<HMODULE>(&__ImageBase);
#endif
}
获得了HMODULE,然后求这个路径
GetModuleFileName的hModule参数在DLL中如何设置?

DWORD GetModuleFileName(
HMODULE hModule, // handle to module to find filename for
LPTSTR lpFilename, // pointer to buffer to receive module path
DWORD nSize // size of buffer, in characters
);

////////////////////////
GetModuleFileName(NULL,
strPath.
GetBufferSetLength(MAX_PATH+1),
MAX_PATH);
strPath是你自己定义的CString类型变量,用来存储执行程序路径,一般来说,取得路径后你还要在后边加上“\\”才行。
化外之民 2006-10-31
  • 打赏
  • 举报
回复
::GetModuleFileName(NULL,m_name,MAX_PATH);
zhangchuangxun 2006-10-31
  • 打赏
  • 举报
回复
上面这个方法是可以的,我也是这样用的
zhangchuangxun 2006-10-31
  • 打赏
  • 举报
回复
char szModuleName[MAX_PATH] = { 0 };
HMODULE h = AfxGetInstanceHandle( );
if( !h ) return;
GetModuleFileName(h, szModuleName, MAX_PATH);
//去掉后面的文件名称,提取路径
//方法一、
int len = strlen(szModuleName);
if( len == 0) return;
while (len>0)
{
if(szModuleName[len-1] == '\\')
{
szModuleName[len-1] = '\0';
break;
}
else
{
szModuleName[len-1] = '\0';
len--;
}
}

if( len <= 0 ) return;
//方法二、
string s = szModuleName;
int pos = s.find_last_of('\\', s.size());//找到字符串中最后一个'\\';
if( pos == s.size() ) //没找到
return;

s.resize(pos); //去掉字符'\\'和后面的可执行文件名称
nnhehe 2006-10-31
  • 打赏
  • 举报
回复
DWORD dwDirBufferSize = MAX_PATH;
char szDirBuffer[MAX_PATH];
GetCurrentDirectory(dwDirBufferSize,szDirBuffer);

这样行不?

15,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧