我有一个C++程序,当windows启动时,它可以与远程服务器同步文件。需要打开公钥文件的函数在fopen()处失败。当我自己(从内部资源管理器)启动程序时,一切都很好。但是,当我向注册表添加启动键时,函数将失败。
我通过调试器跟踪代码,在调用CreateFileA()之前一切都很好。CreateFileA返回FILE_NOT_FOUND。
我删除了对fopen()的调用,并将其替换为对CreateFileA()的直接调用。然后,我将SECURITY_ATTRIBUTES更改为NULL,之后对CreateFileA()的调用开始工作。
问题是,我用于加密的第三方库需要一个文件*对象,而不仅仅是从文件中读取的数据。我该如何解决我的问题?
下面是我目前使用的代码:
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来重置运行时?
发布于 2012-04-20 16:37:50
你的rPath.erase电话似乎没什么意义
rPath.erase( rPath.begin() + ret, rPath.begin() + rPath.size() - ret );那该怎么办?
这里使用的是(iterator, iterator)版本的erase。我敢说,您正在尝试删除字符串的尾部部分,从位置ret开始。在这种情况下,我希望它看起来像
rPath.erase( rPath.begin() + ret, rPath.end() );如果您想使用(position, length)版本的erase,那么它应该如下所示
rPath.erase( ret, rPath.size() - ret );但你的具体用法看起来就像这两种奇怪的混合体。你想按那个电话做什么?
根据启动程序的方式,GetModuleFileNameA可能返回不同的字符串,这就是为什么在某些情况下您的代码看起来“工作”的原因。
https://stackoverflow.com/questions/10249928
复制相似问题