fopen() failing

fopen() failing

本文关键字:failing fopen      更新时间:2023-10-16

我有一个C++程序,可以在Windows xp启动时将文件与远程服务器同步。需要打开公钥文件的函数在 fopen() 失败。当我自己启动程序(从资源管理器中)时,一切正常。但是当我将启动密钥添加到注册表时,该功能失败。

我通过调试器跟踪代码,一切都很好,直到调用 CreateFileA()。创建文件A 返回FILE_NOT_FOUND。

我删除了对 fopen() 的调用,并将其替换为直接对 CreateFileA() 的调用。然后我SECURITY_ATTRIBUTES更改为 NULL,之后对 CreateFileA() 的调用起作用。

问题是我用于加密的第三方库需要一个 FILE* 对象,而不仅仅是从文件中读取的数据。如何解决我的问题?

这是我当前使用的代码:

if( !GetModuleFileNameA(NULL, Path, MAX_PATH) ){
    delete [] buf;
    delete [] Path;
    return strerror( errno );
}
rPath = Path;
delete [] Path;
ret = rPath.find_last_of( '' );
if( ret == string::npos ){
    delete [] buf;
    return strerror( errno );
}
ret++;
rPath.erase( rPath.begin() + ret, rPath.begin() + rPath.size() - ret );
rPath += "rsa_pub.txt";
if( ( f = fopen( rPath.c_str(), "rb" ) ) == NULL ){  // fails when started from registry
    delete [] buf;
    return strerror( errno );
}

编辑:

我找到了这个问题的黑客解决方案:如果我释放运行时库然后重新加载它,问题就会消失。但是,这不是一个非常优雅的解决方案。是否可以在不删除和重新加载 dll 的情况下重置运行时?

您的rPath.erase呼叫似乎没有多大意义

rPath.erase( rPath.begin() + ret, rPath.begin() + rPath.size() - ret );

这是应该做什么?

在这里,您使用的是erase(iterator, iterator)版本。我恳求您正在尝试从位置ret开始擦除字符串的尾部。在这种情况下,我希望它看起来像

rPath.erase( rPath.begin() + ret, rPath.end() );

如果你想使用erase(position, length)版本,那么它看起来像

rPath.erase( ret, rPath.size() - ret );

但是您的特定用法看起来像是两者的奇怪混合体。你想通过这个电话做什么?

GetModuleFileNameA可能会返回不同的字符串,具体取决于您启动程序的方式,这就是为什么您的代码在某些情况下可能看起来"有效"的原因。