I have a C++ program that syncs files with a remote server when windows xp starts. A function that needs to open a public key file fails at fopen(). When I start the program myself (from within explorer) everything works fine. But when I add a startup key to the registry the function fails.
I traced the code through a debugger and everything’s fine until the call to CreateFileA(). CreateFileA returns FILE_NOT_FOUND.
I removed the call to fopen() and replaced it with a call to CreateFileA() directly. Then I changed SECURITY_ATTRIBUTES to NULL after which the call to CreateFileA() works.
The problem is that the 3rd party library I’m using for my encryption needs a FILE* object instead of just the data read from the file. How can I solve my problem?
Here’s the code I’m currently using:
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 );
}
EDIT:
I found a hackery solution to the problem: if I free the runtime library and then reload it the problem goes away. However this isn’t a very elegant solution. Is it perhaps possible to reset the runtime withouth removing and reloading the dll?
Your
rPath.erasecall doesn’t seem to make much senseWhat is that supposed to do?
Here you are using the
(iterator, iterator)version oferasehere. I beleve you are trying to erase the tail portion of the string beginning from the positionret. In that case I would expect it to look asIf you wanted to use the
(position, length)version oferase, then it would look asBut your specific usage looks like a weird hybrid of the two. What are you trying to do by that call?
The
GetModuleFileNameAprobably returns different strings, depending on how you start your program, which is why your code might appear to “work” in some cases.