CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

哪个API可以取得本程序所在目录的绝对路径

楼主Runn(椰子)2003-07-02 13:42:13 在 VC/MFC / 基础类 提问

如题 问题点数:30、回复次数:5Top

1 楼zfive5(醉马不肖)回复于 2003-07-02 13:44:07 得分 10

GetModuleFileName  
  The   GetModuleFileName   function   retrieves   the   fully   qualified   path   for   the   specified   module.    
   
  To   specify   the   process   that   contains   the   module,   use   the   GetModuleFileNameEx   function.    
   
  DWORD   GetModuleFileName(  
      HMODULE   hModule,         //   handle   to   module  
      LPTSTR   lpFilename,     //   path   buffer  
      DWORD   nSize                   //   size   of   buffer  
  );  
  Parameters  
  hModule    
  [in]   Handle   to   the   module   whose   path   is   being   requested.   If   this   parameter   is   NULL,   GetModuleFileName   retrieves   the   path   for   the   current   module.    
  lpFilename    
  [out]   Pointer   to   a   buffer   that   receives   the   fully-qualified   path   for   the   module.   If   the   length   of   the   string   exceeds   the   size   specified   by   the   nSize   parameter,   the   string   is   truncated.    
  Windows   NT/2000/XP:   The   path   can   have   the   prefix   "\\?\",   depending   on   how   the   module   was   loaded.   For   more   information,   see   File   Name   Conventions.    
   
  nSize    
  [in]   Specifies   the   length   of   the   lpFilename   buffer,   in   TCHARs.    
  Return   Values  
  If   the   function   succeeds,   the   return   value   is   the   length   of   the   string   copied   to   the   buffer,   in   TCHARs.  
   
  If   the   function   fails,   the   return   value   is   zero.   To   get   extended   error   information,   call   GetLastError.    
   
  Remarks  
  If   a   DLL   is   loaded   in   two   processes,   its   file   name   in   one   process   may   differ   in   case   from   its   file   name   in   the   other   process.    
   
  For   the   ANSI   version   of   the   function,   the   number   of   TCHARs   is   the   number   of   bytes;   for   the   Unicode   version,   it   is   the   number   of   characters.    
   
  Windows   95/98/Me:   The   GetModuleFilename   function   retrieves   long   file   names   when   an   application's   version   number   is   greater   than   or   equal   to   4.00   and   the   long   file   name   is   available.   Otherwise,   it   returns   only   8.3   format   file   names.  
   
  Windows   95/98/Me:   GetModuleFileNameW   is   supported   by   the   Microsoft   Layer   for   Unicode.   To   use   this,   you   must   add   certain   files   to   your   application,   as   outlined   in   Microsoft   Layer   for   Unicode   on   Windows   95/98/Me   Systems.  
   
  Requirements    
      Windows   NT/2000/XP:   Included   in   Windows   NT   3.1   and   later.  
      Windows   95/98/Me:   Included   in   Windows   95   and   later.  
      Header:   Declared   in   Winbase.h;   include   Windows.h.  
      Library:   Use   Kernel32.lib.  
      Unicode:   Implemented   as   Unicode   and   ANSI   versions   on   Windows   NT/2000/XP.   Also   supported   by   Microsoft   Layer   for   Unicode.  
   
  Top

2 楼zfive5(醉马不肖)回复于 2003-07-02 13:44:58 得分 0

GetModuleFileName  
  The   GetModuleFileName   function   retrieves   the   fully   qualified   path   for   the   specified   module.    
   
  To   specify   the   process   that   contains   the   module,   use   the   GetModuleFileNameEx   function.    
   
  DWORD   GetModuleFileName(  
      HMODULE   hModule,         //   handle   to   module  
      LPTSTR   lpFilename,     //   path   buffer  
      DWORD   nSize                   //   size   of   buffer  
  );  
  Parameters  
  hModule    
  [in]   Handle   to   the   module   whose   path   is   being   requested.   If   this   parameter   is   NULL,   GetModuleFileName   retrieves   the   path   for   the   current   module.    
  lpFilename    
  [out]   Pointer   to   a   buffer   that   receives   the   fully-qualified   path   for   the   module.   If   the   length   of   the   string   exceeds   the   size   specified   by   the   nSize   parameter,   the   string   is   truncated.    
  Windows   NT/2000/XP:   The   path   can   have   the   prefix   "\\?\",   depending   on   how   the   module   was   loaded.   For   more   information,   see   File   Name   Conventions.    
   
  nSize    
  [in]   Specifies   the   length   of   the   lpFilename   buffer,   in   TCHARs.    
  Return   Values  
  If   the   function   succeeds,   the   return   value   is   the   length   of   the   string   copied   to   the   buffer,   in   TCHARs.  
   
  If   the   function   fails,   the   return   value   is   zero.   To   get   extended   error   information,   call   GetLastError.    
   
  Remarks  
  If   a   DLL   is   loaded   in   two   processes,   its   file   name   in   one   process   may   differ   in   case   from   its   file   name   in   the   other   process.    
   
  For   the   ANSI   version   of   the   function,   the   number   of   TCHARs   is   the   number   of   bytes;   for   the   Unicode   version,   it   is   the   number   of   characters.    
   
  Windows   95/98/Me:   The   GetModuleFilename   function   retrieves   long   file   names   when   an   application's   version   number   is   greater   than   or   equal   to   4.00   and   the   long   file   name   is   available.   Otherwise,   it   returns   only   8.3   format   file   names.  
   
  Windows   95/98/Me:   GetModuleFileNameW   is   supported   by   the   Microsoft   Layer   for   Unicode.   To   use   this,   you   must   add   certain   files   to   your   application,   as   outlined   in   Microsoft   Layer   for   Unicode   on   Windows   95/98/Me   Systems.  
   
  Requirements    
      Windows   NT/2000/XP:   Included   in   Windows   NT   3.1   and   later.  
      Windows   95/98/Me:   Included   in   Windows   95   and   later.  
      Header:   Declared   in   Winbase.h;   include   Windows.h.  
      Library:   Use   Kernel32.lib.  
      Unicode:   Implemented   as   Unicode   and   ANSI   versions   on   Windows   NT/2000/XP.   Also   supported   by   Microsoft   Layer   for   Unicode.  
   
  Top

3 楼newkey007(无限天空 www.xDrv.com)回复于 2003-07-02 13:45:58 得分 8

TCHAR   szItemValue[MAX_PATH];  
  //   得到程序全路径名  
  GetModuleFileName(   NULL,   szItemValue,   MAX_PATH   );  
  AppPath.Format("%s",szItemValue);Top

4 楼zfive5(醉马不肖)回复于 2003-07-02 13:46:00 得分 0

hModule    
  [in]   Handle   to   the   module   whose   path   is   being   requested.   If   this   parameter   is   NULL,   GetModuleFileName   retrieves   the   path   for   the   current   module.Top

5 楼FAICHEN(CC)回复于 2003-07-02 13:56:25 得分 2

agreeTop

6 楼xubobbs(波波酷)回复于 2003-07-02 13:57:09 得分 10

我给你一个函数吧,你放在你的APP文件类里面  
  CString   CXXXXXXApp::GetCurDir()  
  {  
    TCHAR   sDrive[_MAX_DRIVE];  
    TCHAR   sDir[_MAX_DIR];  
    TCHAR   sFilename[_MAX_FNAME],Filename[_MAX_FNAME];  
    TCHAR   sExt[_MAX_EXT];  
    GetModuleFileName(AfxGetInstanceHandle(),   Filename,   _MAX_PATH);  
    _tsplitpath(Filename,   sDrive,   sDir,   sFilename,   sExt);  
    CString   homeDir(CString(sDrive)   +   CString(sDir));  
    int   nLen   =   homeDir.GetLength();  
    if(homeDir.GetAt(nLen-1)   !=   _T('\\'))  
      homeDir   +=   _T('\\');  
    return   homeDir;  
  }Top

相关问题

  • 如何获得root目录(站点的根目录)的绝对路径?
  • 如何得到虚拟目录的根目录的绝对路径?
  • 如何获得站点中某个目录的绝对路径和相对路径?
  • 如何用代码获取iis中某虚拟目录的绝对路径
  • 如何获取程序所在目录下的文件绝对路径
  • 请问:如何从文件或目录相对于其父目录的pidl,求其绝对路径?
  • 知道当前文件的绝对目录,和数据库文件的绝对目录。可否算出相对路径
  • 如何表示当前目录的上一级目录,要求不用绝对路径,也不改变当前目录?
  • 紧急求救!怎么来获得系统映射的虚拟目录的绝对路径??等着急用呀……
  • 请问如何把一个绝对路径中各个目录名分离出来?

关键词

  • getmodulefilename
  • lpfilename
  • module
  • nsize
  • specifies
  • fully
  • hmodule
  • qualified
  • retrieves
  • path

得分解答快速导航

  • 帖主:Runn
  • zfive5
  • newkey007
  • FAICHEN
  • xubobbs

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo