C Tips:获得当前运行目录(Windows版)
来源:互联网 发布:下载盯盯软件 编辑:程序博客网 时间:2024/06/11 00:15
接口:
BOOL GetCurrentModuleFolderPath(_Out_ LPTSTR * lpFolderPath, _Out_ DWORD * pLength);
实现:
#include <windows.h>const TCHAR FolderDelimiter = _T('\\');const TCHAR StringDelimiter = _T('\0');BOOL GetCurrentModuleFolderPath(_Out_ LPTSTR * lpFolderPath, _Out_ DWORD * pLength){static TCHAR fullPath[MAX_PATH];PTSTR delimiter = NULL;size_t size = 0;ZeroMemory(fullPath, MAX_PATH * sizeof(TCHAR));GetModuleFileName(NULL, fullPath, MAX_PATH);if (fullPath[MAX_PATH - 1] != StringDelimiter){goto EXIT_FAIL;}delimiter = _tcsrchr(fullPath, FolderDelimiter);if (!delimiter){goto EXIT_FAIL;}*pLength = delimiter - fullPath;size = (*pLength + 1) * sizeof(TCHAR);*lpFolderPath = (LPTSTR)malloc(size);if (!*lpFolderPath){goto EXIT_FAIL;}ZeroMemory(*lpFolderPath, size);_tcsncpy_s(*lpFolderPath, *pLength + 1, fullPath, *pLength);(*lpFolderPath)[*pLength] = StringDelimiter;return TRUE;EXIT_FAIL:pLength = 0;return FALSE;}
测试:
int _tmain(int argc, _TCHAR* argv[]){LPTSTR path = NULL;DWORD length = 0;if (!GetCurrentModuleFolderPath(&path, &length)){_tprintf(_T("GetCurrentModuleFolderPath() Failed!\n"));}else{_tprintf(_T("Current folder: \n%s\nLength: %d\n"), path, length);}if (path){free(path);path = NULL;}return 0;}
结果:
0 0
- C Tips:获得当前运行目录(Windows版)
- 获得程序当前的运行目录
- VC++中如何获得一个当前进程的运行目录
- VC++中如何获得一个当前进程的运行目录
- C++中用 GetModuleFileName()函数 获得程序当前的运行目录
- GetCurrentDirectory获得当前目录
- 获得程序当前目录
- 获得程序当前目录
- windows mobile进程查看器开发(一)——获得当前运行进程的信息
- Windows Mobile下当前运行程序的目录
- Windows & Linux(Unix) 获取当前程序运行目录
- 获取当前运行目录
- 获得当前程序目录路径
- 获得当前目录的方法
- MFC 获得当前应用程序目录
- 批处理获得当前/自身目录
- 利用 GetModuleFileName 获得当前目录
- C获得当前时间
- [Leetcode]Word Break
- BZOJ 大视野 2761: [JLOI2011]不重复数字 解题报告
- A*算法. Knight Moves
- VIM基本知识与快捷操作(备忘)
- go开发环境搭建
- C Tips:获得当前运行目录(Windows版)
- 分布式session处理方案
- maven引入本地jar
- POJ 2575 && ZOJ 1879 Jolly Jumper(整数序列)
- 网站开发-php开发手机论坛(10)-帖子列表显示
- Mysql引擎
- ps aux指令詳解
- LPC1768 SPI 外设控制DA(PCM1796)调试记录
- 《Bootstrap廣告輪播》加入Touth用手指就可左右滑動切換廣告