在nx二次开发中经常会用到dll的全路径,上一级路径和名称,这里我对其进行封装,方便以后调用。
关键代码:
//头文件
#include <Windows.h>
#include <atlbase.h>//获取当前dll所在的文件夹(mode:0-全路径名,1-dll的上一级路径,2-dll名称)
static string GetDllPanthAndName(int mode = 0);string LiangFuns::UserUFBaseFun::GetDllPanthAndName( int mode )
{char szBuff[1024] = { 0 };HMODULE hModuleInstance = _AtlBaseModule.GetModuleInstance();GetModuleFileNameA(hModuleInstance, szBuff, 1024);string strName = szBuff;if (mode == 0){string strdllPanth = strName;return strdllPanth;}else if (mode == 1){string strDLLUPPath = strName.substr(0, strName.find_last_of("\\"));return strDLLUPPath;}else if (mode == 2){string strALLName = strName.substr(strName.find_last_of("\\") + 1, strName.find_last_of(".") - (strName.find_last_of("\\") + 1));return strALLName;}return 0;
}
结果如下所示: