函数GetFullPathName的调用细节
来源:互联网 发布:超市用什么软件 编辑:程序博客网 时间:2024/05/19 00:38
函数GetFullPathName的细节
一开始查看了在线的msdn,看到这样的示例:
#include <windows.h>#include <tchar.h>#include <stdio.h> #define BUFSIZE 4096#define LONG_DIR_NAME TEXT("c:\\longdirectoryname") void _tmain(int argc, TCHAR *argv[]){ DWORD retval=0; BOOL success; TCHAR buffer[BUFSIZE]=TEXT(""); TCHAR buf[BUFSIZE]=TEXT(""); TCHAR** lppPart={NULL}; if( argc != 2 ) { _tprintf(TEXT("Usage: %s [file]\n"), argv[0]); return; } // Retrieve the full path name for a file. // The file does not need to exist. retval = GetFullPathName(argv[1], BUFSIZE, buffer, lppPart); if (retval == 0) { // Handle an error condition. printf ("GetFullPathName failed (%d)\n", GetLastError()); return; } else { _tprintf(TEXT("The full path name is: %s\n"), buffer); if (lppPart != NULL && *lppPart != 0) { _tprintf(TEXT("The final component in the path name is: %s\n"), *lppPart); } }
在vs2010下执行结果不正确,lppPart总是为0。后来在网上搜索一番,发现是调用函数时传递的最后一个参数不对。
函数定义如下:
DWORD WINAPI GetFullPathName( _In_ LPCTSTR lpFileName, _In_ DWORD nBufferLength, _Out_ LPTSTR lpBuffer, _Out_ LPTSTR *lpFilePart);
参数说明:
lpFileName [in]
文件名。
该参数既可以是一个短文件名,也可以是一个长文件名,还可以是共享名或卷名。
nBufferLength [in]
接收路径的缓冲区的长度。
lpBuffer [out]
这是一个输出参数,指向路径缓冲区的指针。
lpFilePart [out]
输出参数,指向路径缓冲区中文件名部分的指针。该参数可以是NULL。如果lpBuffer指向的缓冲区内存放的是一个目录而非文件,lpFilePart为0。
问题就出在最后一个参数lpFilePart的定义上。微软定义为:
TCHAR** lppPart={NULL};但实际改为TCHAR* lppPart={NULL};即可,当然,后面的代码也要作出相应的变动,具体见下面修改后的代码。
#include<iostream>#include <windows.h>#include <tchar.h>#include <stdio.h>using namespace std;#define BUFSIZE 4096#define LONG_DIR_NAME TEXT("c:\\longdirectoryname") void _tmain(int argc, TCHAR *argv[]){ DWORD retval=0; BOOL success; TCHAR buffer[BUFSIZE]=TEXT(""); TCHAR buf[BUFSIZE]=TEXT(""); TCHAR* lppPart={NULL}; // Retrieve the full path name for a file. // The file does not need to exist. retval = GetFullPathName(L"C:\\Users\\yeosn\\Desktop\\b.dwg", BUFSIZE, buffer, &lppPart); if (retval == 0) { // Handle an error condition. printf ("GetFullPathName failed (%d)\n", GetLastError()); return; } else { _tprintf(TEXT("The full path name is: %s\n"), buffer); if (lppPart != NULL && *lppPart != 0) { //_tprintf(TEXT("The final component in the path name is: %s\n"), *lppPart);wcout<<lppPart<<endl; }}
总结:
这个问题主要出在二级指针的运用上。TCHAR** lppPart={NULL};的写法虽然定义了一个二级指针,也进行了初始化,但并没有对一级指针初始化,所以在后面的代码中调用*lppPart会失败。
0 0
- 函数GetFullPathName的调用细节
- GetFullPathName函数使用
- 关于函数调用的细节
- GetFullPathName
- 类成员函数调用的细节
- 日常工作问题总结(三十一)windows获取路径的几个函数GetCurrentDirectory,GetModuleFileName,GetFullPathName
- CSAPP--第三章中函数调用与返回的细节
- Unity3D RPC(远程过程调用)细节__让你调用一个远程计算机的函数
- Unity3D RPC(远程过程调用)细节__让你调用一个远程计算机的函数
- 输入函数的细节
- 构造函数的细节
- 构造函数的细节
- <细节>C++函数形参表调用顺序
- 【JavaScript】(4)普通函数、动态函数定义格式,调用方式,注意的详细细节
- 多态中继承调用的细节
- Jquery中的一些细节(一)--------------->Jquery加载调用多个函数(方法)需要注意的细节
- 从汇编角度看Linux C函数的调用约定和参数传递的细节
- 使用函数的小细节
- 设置Servlet
- Android 4.1 Audio系统变化说明
- 2014-3-4 destroy,init
- servlet基础
- Linux学习笔记九:FileSystem
- 函数GetFullPathName的调用细节
- TQ2440搭建Qt4.5需要修改的地方
- hdu(2048)全错位排列
- php输出控制
- JNI 如何使用dlopen,dlsym调用第三方自定义的 .so库(包括JNI->JNI)
- 第二次作业
- 指针函数与函数指针的区别
- C++的文件操作
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法! .