Here's the code:
AnsiString path = "BrowserBot.exe";
ShellExecute(0, TEXT("open"), path.c_str(), TEXT("-parametr"), 0, SW_SHOW);
Writes an error that the AnsiString identifier is not defined. I don't know what the problem is.
All connected libraries:
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <fstream>
#include <sstream>
AnsiString is a string class that is specific to the C++Builder compiler. If you are using that compiler, make sure you are compiling your project with C++Builder's VCL (Visual Component Library) or FMX (FireMonkey) framework enabled, and that you have a corresponding #include <vcl.h> or #include <fmx.h> statement in your C++ code.
Otherwise, if you are using any other compiler, you should use the standard C++ std::string class instead (which can also be used in C++Builder), eg:
#include <string>
std::string path = "BrowserBot.exe";
ShellExecuteA(0, "open", path.c_str(), "-parametr", 0, SW_SHOW);
Related
I'm trying to print the text "Ääkkösiä ruutuun." to console with c++. I have windows 7 and am using Code::Blocks editor. Searching on the subject I found that maybe these sort of lines would help
_setmode(_fileno(stdout), _O_U16TEXT);
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
But when I try to compile it, I get the error: _fileno was not declared in this scope.
I have all these includes:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdexcept>
#include <cmath>
#include <sstream>
#include <fstream>
#include <codecvt>
#include <locale>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <cstdio>
#include <ostream>
what am I missing?
Also, one other thing I tried was locale, but then locale::empty wasn't found! Why doesn't my c++ have anything in it?
EDIT
Here is a picture of what my program is doing now.
It prints out just the first letter (Ä). What happens to the rest?
Ok, it seems that setmode sets it so that only one letter gets printed. (Even trying to print normal texts with multiple commands, just results in a single letter.) Without it the scandinavian letters don't print correctly, thought. They look like this:
The answer you found is for Visual Studio, not Code::Blocks.
While the C standard specifies what should in in <stdio.h>, it only specifies a minimum. Implementors may add their own functions, and should do so using an _ (underscore prefix). This is why you should NOT use that prefix. You don't know what you'll break. Microsoft clearly signaled their non-standard extensions using the correct prefix.
The answer is tagged C++, but C++ inherits the contents of <stdio.h> from C.
The line
setlocale(LC_CTYPE, ".OCP");
works!
A complete example:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
wstring readFile(const char* filename) {
wifstream wif(filename);
locale myLoc("");
//locale utf8_locale(locale(), new gel::stdx::utf8cvt<true>);
wif.imbue(myLoc);
basic_stringstream<wchar_t> wss;
wss << wif.rdbuf();
return wss.str();
}
int main() {
setlocale(LC_CTYPE, ".OCP");
wstring contents = readFile("test.txt");
wcout<<L"Does anything get printed out at all???"<<endl;
//wcout <<contents<<endl;
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
wcout<<L"Näkyykö äkköset?"<<endl;
return 0;
}
The text read from file (utf-8) still doesn't print correctly, though.
It should be
Hei!
Täällä on kaksi riviä.
ä's go awry there.
Output:
How can I execute an EXE file in the appdata folder without knowing the username of the path c:\users\username ?
#include <stdio.h> // C library to perform Input/Output operations
#include <tchar.h>
#include <stddef.h> // C Standard definitions
#include <iostream> // Input/Output
#include <fstream>
#include <cstdlib>
#include <windows.h>
int main()
{
ShellExecute(NULL, "Open", "C:\\Users\\%USERNAME%\\AppData\\Roaming\\Microsoft\\mcv.exe", NULL, NULL, SW_HIDE);
}
You can use the Windows environment variable %APPDATA%, which resolves to "C:\Users\UserName\AppData\Roaming" on Windows 10.
More info on Windows variables here :
https://www.microsoft.com/en-us/wdsi/help/folder-variables
Intro
First of all, I would like to say that I have read through the previous answers for this type of question, including this excellently written one.
However, I do not understand enough about C++ to be able to use the more "advanced" fixes.
I have ensured that the right type of console has been selected (Console (/SUBSYSTEM:CONSOLE) for those interested), and have the required imports with the possible exception of an IDL mentioned somewhere (that falls into the lack of understanding category).
If this is a duplicate, I would be more than happy to use the post I duplicated, but I have not been able to find anything that can help someone of my skill level.
Technical Information
IDE: Visual Studio
Platform: Windows
Code
headers.h
#pragma once
#include <stdio.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <Shobjidl.h>
#include <time.h>
#include <stdlib.h>
#include <tchar.h>
main.cpp
#include "headers.h"
using namespace std;
int main() {
string x = "C://Users/student/Desktop/i-should-buy-a-boat.jpg";
x.c_str();
wstring tempx = std::wstring(x.begin(), x.end());
LPCWSTR sw = tempx.c_str();
HRESULT SetWallpaper(
LPCWSTR monitorID,
LPCWSTR wallpaper
);
SetWallpaper(NULL, sw);
}
SetWallpaper() is not a standalone function exported by the Win32 API. It is a method of the IDesktopWallpaper interface (see here).
So you need to use code that is more like this instead:
#include "headers.h"
int main()
{
std::wstring x = L"C:\\Users\\student\\Desktop\\i-should-buy-a-boat.jpg";
CoInitialize(NULL);
IDesktopWallpaper *p;
if (SUCCEEDED(CoCreateInstance(__uuidof(DesktopWallpaper), 0, CLSCTX_LOCAL_SERVER, __uuidof(IDesktopWallpaper), (void**)&p)))
{
p->SetWallpaper(NULL, x.c_str());
p->Release();
}
CoUninitialize();
return 0;
}
I've included the TlHelp32.h header properly though I keep getting the message:
"Error: identifier "CreateToolhelp32Snapshot" is undefined"
when attempting to use CreateToolhelp32Snapshot. When I used the "peek definition" feature in VS I found that there are errors within this header where in certain areas it says:
"Error expected a ';'"
Any ideas how to fix this?
#include "stdafx.h"
#include <TlHelp32.h>
#include <Windows.h>
#include <iostream>
using namespace std;
class Functions{
public:
void playerHealthPrinter(){
HANDLE hProcess;
DWORD dwPID, dwProtection, dwCaveAddress;
BOOL bPOn, bIOn, bProt;
HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
Never include Win32 headers before <windows.h>. The following should work, instead:
#include <Windows.h>
#include <TlHelp32.h> // <-- include *after* windows.h
If it still doesn't work then please post an MCVE including the relevant headers and version of VS.
I'm using the following ultra-super-mega simple code to list all the files in a direcory (Windows 8.1, Visual Studio Express 2013, C++):
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <limits>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <bitset>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
using namespace std;
void get_file_list(string DATA_DIR)
{
HANDLE hFind;
WIN32_FIND_DATA data;
hFind = FindFirstFile(LPCWSTR(DATA_DIR.c_str()), &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
printf("%s\n", data.cFileName);
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
}
int main(int argc, char** argv)
{
string DATA_DIR = "D:\\drobpox\\Dropbox\\BinaryDescriptors\\LFW\\DATA\\*.*";
//string DATA_DIR = "c:\\Users\\GilLevi\\Downloads\\GraphsSURF\\GraphsSURF\\bark\\*.jpg";
string OUT_DIR = "D:\\drobpox\\Dropbox\\BinaryDescriptors\\LFW\\LATCH_TXT_FILES\\LATCH8";
get_file_list(DATA_DIR);
}
However, I "hFind" always equals "INVALID_HANDLE_VALUE". I double checked the path and tried various different paths.
Might the reason be that I'm running a 64bit application and using WIN32_FIND_DATA ?
Thanks in advance,
Gil
Converting a string to a widestring requires you to allocate memory and use string conversion functions.
If you don't want to change the function, the easiest solution is probably to use the non-unicode version of FindFirstFile, by Adding a A to the functionname and struct;
WIN32_FIND_DATAA data;
hFind = FindFirstFileA(DATA_DIR.c_str(), &data);
Since you're using LPCWSTR, you should be using std::wstring, not std::string in your program.
Also, there is no conversion magic when you cast to an LPCWSTR. It is just a dumb 'C' cast that basically does nothing except shut the compiler up.