I'm trying to open a file with a c++ program using ShellExecuteA in Windows10. (I'm also using VisualStudio2019 in case that's relevant.)
ShellExecute itself is working (I can use "explore" and "find" as it is intended), however it seems to be unable to find the file even though it exists in the directory. I have tried both an absolute as well as a relative path and neither work.
My code is this:
#include <iostream>
#include <windows.h>
#include <shellapi.h>
#include <string>
#include <limits.h>
using namespace std;
string getCurrentDir() {
char buff[MAX_PATH];
GetModuleFileName(NULL, buff, MAX_PATH);
string::size_type position = string(buff).find_last_of("\\/");
return string(buff).substr(0, position);
}
int main()
{
cout << "path: " << getCurrentDir() << endl;
int ret1 = (int)ShellExecuteA(NULL, "open", "C:\\Users\\Sasha\\source\\repos\\shellopen\\Debug\\MyTextFile.txt", NULL, NULL, SW_SHOWNORMAL);
cout << ret1 << endl;
int ret2 = (int)ShellExecuteA(NULL, "open", "MyTextFile.txt", NULL, NULL, SW_SHOWNORMAL);
cout << ret2 << endl;
return 0;
}
The result is
path: C:\Users\Sasha\source\repos\shellopen\Debug
2
2
"2" apparently means that the file couldn't be found, however "MyTextFile.txt" definitely exists in the directory and there is no spelling mistake.
I've tried googling the problem but it seems to be uncommon enough that I haven't found anything that works for me. I'd be very grateful for any help.
Ok, it turned out it was a spelling mistake, causing my file to be named "MyTextFile.txt.txt"
Thank you to everyone who tried to help me find the answer.
Related
I'm currently new to C++ and I've been watching a tutorial series https://www.youtube.com/watch?v=_bYFu9mBnr4, but I'm having a big issue. My C++ code will not open a file no matter what I do, I've looked online and tried renaming it, the full path, everything I can think of. Here's my code,
#include <iostream>
#include <fstream>
#include <cstring>
#include <cerrno>
#include <filesystem>
int main()
{
std::ofstream file;
file.open("hello.txt");
if (!file.is_open())
{
std::cerr << "Error: " << strerror(errno) << '\n';
std::cout << std::filesystem::current_path() << std::endl;
}
file << "hello!";
file.close();
return 0;
}
Sorry about this question, it may have been a dumb issue. Turns out IT WAS my antivirus. Avast kept blocking it, it was just looking out for me. I decided to change my antivirus afterwards and it now works fine!
I want to get a value from a Proccess, here it's a simple test with "Calculator".
First, I get the address with CheatEngine. Secondly I put it in ReadProcessMemory.
But ReadProcessMemory return 0, I think I miss something, I've found something with BaseAddress, but I still have bad results. Google is out of results for me, so I ask you!
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int value;
DWORD pid;
HWND hwnd = FindWindow(NULL,"Calculatrice");
if(!hwnd)
{
cout << "Window not found!";
}
else
{
GetWindowThreadProcessId(hwnd,&pid);
HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
if(!phandle)
{
cout <<"Could not get handle!";
}
else
{
cout << ReadProcessMemory(phandle,(LPVOID)0xC71657E900,&value,sizeof(value),0) << endl;
cout << value;
getch();
return 0;
}
}
}
Resolved ! When the address is too big, because it's a 64bit address, the program change to a 32bit address, that's why it don't work.
So, in Visual Studio, set to x64.
Is there a way to create files on a computer using C++ and the location of the C++ program? I want to be able to use this program on multiple computers yet each computer has its own different directory. For example if I want to create test.txt file located on the user directory is there a way to put the path of the file corresponding to the program file's location because "user" differs on the name of the user.
as long as you are on windows you can use the API GetUserName then use SetCurrentDirectory API to change working directory then create your file there using CreateFile or fstream:
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ULONG len = 50;
LPSTR lpBuffer = (LPSTR)GlobalAlloc(GPTR, len);
GetUserName(lpBuffer, &len);
cout << lpBuffer << endl;
// don't forget to clean when you're done:
string str = "C:\\Users\\";
str += lpBuffer;
str += "\\test.txt";
cout << str << endl;
ofstream out(str.c_str());
if(out.fail())
perror("Opening failed");
out << "Hello " << lpBuffer;
out.close();
GlobalFree(lpBuffer);
GlobalFree(lpBuffer);
return 0;
}
this program gets the current username and creates file inside its folder and writes in it "Hello" 'yourusername'
Newb here. I have spent the last 4 hours trying to solve this problem.
ifstream is suddenly not opening files.
ofstream has no problems writing to the file.
The file exists, it's contents are, ThisIsText, and it is in the reference directory, which I confirmed with system("dir & pause")
I tried Code::Blocks and Dev C++, but I think they're using the same compiler(GNU GCC Compiler).
I tried using the full filename path with double backslashes.
I see people mentioning permissions, but I don't know how to tinker with that.
I'm on Windows 10.
Edit: I just found a new compiler(Embarcadero 10.1 AKA Borland) and the code works with it. I still want to know what the problem is with GNU GCC
The following code skips to the else statement:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string message;
ifstream inputFile;
inputFile.open("File.txt");
if (inputFile.good())
{
inputFile >> message;
cout << message;
system("dir & pause");
}
else
{
cout << "failed to open input file\n";
system("dir & pause");
} return 0;
}
If it helps, I found the following code online and it outputs, "Error code = 2"
from winerror.h, that is: ERROR_FILE_NOT_FOUND
#include <windows.h>
#include <iostream>
int main()
{
HANDLE hFile = CreateFile(
"one.txt", // Windows does not case about case
GENERIC_READ,
0, // no sharing
NULL, // default security
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL ); // no file template
if(INVALID_HANDLE_VALUE == hFile)
{
DWORD errCode = GetLastError(); // see winerror.h for meanings
std::cout << "File wouldn't open :-(" << std::endl;
std::cout << "Error code = " << errCode << std::endl;
}
else
{
std::cout << "File opened OK :-)" << std::endl;
CloseHandle(hFile);
}
return 0;
}
PS. I know using namespace std; is bad practice
I've got a pretty basic console program here, to determine if a folder or file exists or not using stat:
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main() {
char path[] = "myfolder/";
struct stat status;
if(stat(path,&status)==0) { cout << "Folder found." << endl; }
else { cout << "Can't find folder." << endl; } //Doesn't exist
cin.get();
return 0;
}
I have also tried the access version:
#include <iostream>
#include <io.h>
using namespace std;
int main() {
char path[] = "myfolder/";
if(access(path,0)==0) { cout << "Folder found." << endl; }
else { cout << "Can't find folder." << endl; } //Doesn't exist
cin.get();
return 0;
}
Neither of them find my folder (which is right there in the same directory as the program). These worked on my last compiler (the default one with DevCpp). I switched to CodeBlocks and am compiling with Gnu GCC now, if that helps. I'm sure it's a quick fix - can someone help out?
(Obviously I'm a noob at this so if you need any other information I've left out please let me know).
UPDATE
The problem was with the base directory. The updated, working program is as follows:
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main() {
cout << "Current directory: " << system("cd") << endl;
char path[] = "./bin/Release/myfolder";
struct stat status;
if(stat(path,&status)==0) { cout << "Directory found." << endl; }
else { cout << "Can't find directory." << endl; } //Doesn't exist
cin.get();
return 0;
}
ANOTHER UPDATE
Turns out that a trailing backslash on the path is big trouble.
Right before your stat call, insert the code:
system("pwd"); // for UNIXy systems
system("cd"); // for Windowsy systems
(or equivalent) to check your current directory. I think you'll find it's not what you think.
Alternatively, run the executable from the command line where you know what directory you're in. IDEs will frequently run your executable from a directory you may not expect.
Or, use the full path name so that it doesn't matter which directory you're in.
For what it's worth, your first code segment works perfectly (gcc under Ubuntu 10):
pax$ ls my*
ls: cannot access my*: No such file or directory
pax$ ./qq
Cannot find folder.
pax$ mkdir myfolder
pax$ ll -d my*
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder/
pax$ ./qq
Folder found.
Are you sure that the current directory of your running program is what you expect it to be? Try changing path to an absolute pathname to see if that helps.
Check your PWD when you running your program. This problem is not caused by compiler. You DevCpp may set a working directory for your program automatically.
You can find out why stat() failed (which is a C function, not C++, by the way), by checking errno:
#include <cerrno>
...
if (stat(path,&status) != 0)
{
std::cout << "stat() failed:" << std::strerror(errno) << endl;
}