cfile读取文件失败,getlasterror返回5,权限不足,请问怎么解决。

shabi3301 2012-08-01 01:05:05
源代码如下

bool ParseDefPath()
{
bool bDefPath = false;
ZeroMemory(m_strDefPath,sizeof(m_strDefPath));
//m_strDefPath = TEXT("");
TCHAR szFilePath[256] = {0};
GetModuleFileName(theApp.m_hInstance,szFilePath,sizeof(szFilePath)/sizeof(TCHAR));
CString strRunPath;
strRunPath.Format(TEXT("%s"),szFilePath);
int nSep = strRunPath.ReverseFind(TEXT('\\'));
CString strRunIni;
strRunIni = strRunPath.Left(nSep) + TEXT("\\defpath.txt");
OutputDebugString(strRunIni);
if (!PathFileExists(strRunIni))
{
OutputDebugString(TEXT("can't find ini"));
return bDefPath;
}
OutputDebugString(TEXT("1....\n"));
CFile pFile;
bDefPath = pFile.Open(strRunIni,CFile::modeReadWrite);
OutputDebugString(TEXT("2....\n"));
if (!bDefPath)
{
CString strFail;
strFail.Format(TEXT("open ini failed,path = %s ,failed value = %d"),strRunIni,GetLastError());
OutputDebugString(strFail);
return bDefPath;
}
OutputDebugString(TEXT("3....\n"));
char strMsg[200] = {0};
pFile.Read(strMsg,200);
pFile.Close();
OutputDebugString(TEXT("4....\n"));
if (sscanf(strMsg,"defaultpath = %s",m_strDefPath))
{
CString strFail;
strFail.Format(TEXT("parse ini succeed,path = %s ,m_strDefPath = %s"),strRunIni,m_strDefPath);
OutputDebugString(strFail);
return true;
}
OutputDebugString(TEXT("5....\n"));
CString strFail;
strFail.Format(TEXT("parse ini failed,path = %s ,failed value = %d"),strRunIni,GetLastError());
OutputDebugString(strFail);
return false;
}

...全文
1072 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wnx_u 2013-09-22
  • 打赏
  • 举报
回复
本来WEB就没有访问文件的权限,创建进程的时候默认是以父进程的权限创建的,所以子进程也不行;你可以先AdjustTokenPrivileges提权限后再创建进程,但是不一定能提成功;也可以用active控件,但是需要颁发证书那些,而且active控件大部分浏览器都不支持的.
shabi3301 2012-08-03
  • 打赏
  • 举报
回复
怪我,没有说清楚,这个问题肯定是权限问题,问题是该怎么改。
我是这样的,当直接调用这个exe(控制台)的时候,它权限没有问题,能直接打开。
当使用webservice方法时,我的webservice是调用的COM的接口,然后COM的接口中对应的createprocess它,这时open就会失败,getlasterror返回错误代码5,权限不足。
我的createprocess是这么写的:

CString strRunCmd;
.... //此处略去为strRunCmd赋路径,路径没问题。
STARTUPINFOA si;
ZeroMemory(&si, sizeof(STARTUPINFOA));
si.cb = sizeof(STARTUPINFOA);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi;
bSucceed = CreateProcessA(NULL,W2A(strRunCmd.GetBuffer()),NULL,NULL,0,0,NULL,NULL,
&si,&pi);

运行能运行,唯独读取txt失败,求如何修改createprocess获得权限?
zgl7903 2012-08-03
  • 打赏
  • 举报
回复
捕捉文件异常

#include "Shlwapi.h"
#pragma comment(lib, "Shlwapi.lib")

{
TCHAR szFilePath[MAX_PATH+1] = {0};
GetModuleFileName(NULL, szFilePath, MAX_PATH);
PathRemoveFileSpec(szFilePath);
_tcscat_s(szFilePath, TEXT("\\defpath.txt"));

try
{
char strMsg[200] = {0};
CFile pFile(szFilePath,CFile::modeReadWrite);
pFile.Read(strMsg,200);
pFile.Close();

if (sscanf(strMsg,"defaultpath = %s",m_strDefPath))
{
CString strFail;
strFail.Format(TEXT("parse ini succeed,path = %s ,m_strDefPath = %s"),szFilePath,m_strDefPath);
OutputDebugString(strFail);
return true;
}
}
catch (CFileException *e)
{
e->ReportError();
e->Delete();
}

return false;
}
zwfgdlc 2012-08-03
  • 打赏
  • 举报
回复
CFileException ex;
bDefPath = pFile.Open(strRunIni,CFile::modeReadWrite,&ex);

看是不是权限问题
zwfgdlc 2012-08-03
  • 打赏
  • 举报
回复
CFileException ex;
bDefPath = pFile.Open(strRunIni,CFile::modeReadWrite);

看是不是权限问题
shabi3301 2012-08-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

程序提升UAC,用Administrator权限
[/Quote]
我就自己新建了个txt文件啊,里面就一行东西
defpath = E:\Path,而且文件本身并没有设置权限啊,我也是adminstrator进入的,而且是xp。在开发机上没问题,部署到其他机器才有这个问题的(也是XP,adminstrator用户,并且对txt没设置任何权限,奇怪的是,我自己写和读xml文件没问题。)。xp环境也没UAC一说啊
shabi3301 2012-08-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

最简单的当然是手动先修改文件的权限属性
[/Quote]
我就自己新建了个txt文件啊,里面就一行东西
defpath = E:\Path,而且文件本身并没有设置权限啊,我也是adminstrator进入的,而且是xp。在开发机上没问题,部署到其他机器才有这个问题的(也是XP,adminstrator用户,并且对txt没设置任何权限,奇怪的是,我自己写和读xml文件没问题。)。
oyljerry 2012-08-01
  • 打赏
  • 举报
回复
程序提升UAC,用Administrator权限
Gloveing 2012-08-01
  • 打赏
  • 举报
回复
最简单的当然是手动先修改文件的权限属性

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧