NX二次开发-获取当前dll的项目路径、获取配置文件路径
- 获取当前dll的项目路径;
1 string NXCommon::GetUserDir()
2 {
3 std::string dll = GetHostName();
4 string dir, fname;
5 SplitFileName(dll, dir, fname);
6
7 string mdlName = GetModuleFullPath(fname);
8 string tailName = "application\" + fname;
9
10 string userDir = mdlName.substr(0, mdlName.size() - tailName.size());
11
12 return userDir;
13 }
1 string GetHostName()
2 {
3 HMODULE handle = NULL;
4
5 bool isOK = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
6 (char*)_ReturnAddress(), &handle);
7
8 if (isOK)
9 {
10 char name[1024] = { 0 };
11 GetModuleFileNameA(handle, name, 1024);
12
13 return std::string(name);
14 }
15 else
16 {
17 return "";
18 }
19 }
1 std::string GetModuleFullPath(std::string dllName)
2 {
3 HMODULE handle = GetModuleHandleA(dllName.c_str());
4
5 char lpFileName[_MAX_PATH + 1] = { '/0' };
6 GetModuleFileNameA(handle, lpFileName, _MAX_PATH);
7
8 return std::string(lpFileName);
9 }
- 获取配置文件路径。
1 string NXCommon::GetConfigDir()
2 {
3 return GetUserDir() + "config\";
4 }