| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 740 人关注过本帖
标题:GetModuleFileName 获得运行文件路径的问题
只看楼主 加入收藏
smart3503
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-1-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
GetModuleFileName 获得运行文件路径的问题
#include "Shlwapi.h"
#include <Windows.h>  
#include <iostream>   
#include <string>
using namespace std;   
   
string GetProgramDir()  
{   
    TCHAR exeFullPath[MAX_PATH]; // Full path
    string strPath = "";
    GetModuleFileName(NULL,exeFullPath,MAX_PATH);
    strPath=(string)exeFullPath;    // Get full path of the file,该行运行显示错误,代码见最后
    int pos = strPath.find_last_of('\\', strPath.length());
    return strPath.substr(0, pos);  // Return the directory without the file name
}   
 
int main ()
{
    string str = "";
    str = GetProgramDir();
    cout << str << endl;
    return 0;
}

运行后显示 error C2440: 'type cast' : cannot convert from 'TCHAR [260]' to 'std::string'。不知如何更改,请各路高手支招,多谢!
搜索更多相关主题的帖子: return without include namespace pos 
2013-01-29 13:37
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:20 
估计你的是用宽字符吧,改成下面的试试。

string GetProgramDir()  
 {   
     char exeFullPath[MAX_PATH]; // Full path
     string strPath = "";
     GetModuleFileNameA(NULL,exeFullPath,MAX_PATH);
     strPath=(string)exeFullPath;   
     int pos = strPath.find_last_of('\\', strPath.length());
     return strPath.substr(0, pos);  // Return the directory without the file name
 }   

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-01-29 13:51
smart3503
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-1-19
收藏
得分:0 
多谢版主指教,可以了,学的不精,以后多来向版主学习
2013-01-29 14:27
快速回复:GetModuleFileName 获得运行文件路径的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020374 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved