Wchar_t to string Conversion - c++

I want to convert the strFileNotifyInfo[1].FileName(Wchar_t) to a string so that i can see the filepath. but i can't make it work.
Here is my code:
while(TRUE)
{
if( ReadDirectoryChangesW( hDir, (LPVOID)&strFileNotifyInfo, sizeof(strFileNotifyInfo), FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE || FILE_NOTIFY_CHANGE_CREATION, &dwBytesReturned, NULL, NULL) == 0)
{
cout << "Reading Directory Change" << endl;
}
else
{
cout << ("File Modified: ") << strFileNotifyInfo[1].FileName << endl;
cout << ("Loop: ") << nCounter++ << endl;
}
}

Use wcout when working with wide character data.
std::wcout << L"File Modified: " << strFileNotifyInfo[1].FileName << std::endl;

You should also keep in mind that FileName is not null-terminated.
WCHAR* filename_w = strFileNotifyInfo[1].FileName;
DWORD filename_len = strFileNotifyInfo[1].FileNameLength;
std::string filename(filename_w, filename_w + filename_len);
std::cout << "File Modified: " << filename << std::endl;

Related

list not displaying from txt file

void main() {
nodLista* LS=NULL;
FILE* F=fopen("asaceva.txt","r");
if(F!=NULL) {
char buffer[100]; int id;float pret;
fscanf(F,"d",&id);
while(!feof(F)) {
fscanf(F,"f",&pret);
fscanf(F,"s",buffer);
Produs* p= creareProdus(id,pret,buffer);
LS=inserareSfarsit(LS,*p);
fscanf(F,"%d",&id);
}
afisareLista(LS);
}
_getch();
}
afisareLista: displays the list
inserareSfarsit: inserts at the end
I don't understand why it doesn't get the data from the txt file. Can you explain why?
There are a couple of issues in your code:
main not returning integer.
not using fscanf consistently and correctly with placeholders.
you are not checking the return value of fscanf for failure.
NULL should be replaced with nullptr if you have C++11 support available.
The correct code should be like this:
int main() {
nodLista* LS=NULL;
FILE* F=fopen("asaceva.txt","r");
if(F!=NULL) {
char buffer[100]; int id;float pret;
if (!fscanf(F,"%d",&id))
cout << "Error happened: " << ferror(F) << ", error string: " << strerror(errno) << endl;
while(!feof(F)) {
if (!fscanf(F,"%f",&pret))
cout << "Error happened: " << ferror(F) << ", error string: " << strerror(errno) << endl;
if (!fscanf(F,"%s",buffer))
cout << "Error happened: " << ferror(F) << ", error string: " << strerror(errno) << endl;
Produs* p= creareProdus(id,pret,buffer);
LS=inserareSfarsit(LS,*p);
if (!fscanf(F,"%d",&id))
cout << "Error happened: " << ferror(F) << ", error string: " << strerror(errno) << endl;
}
afisareLista(LS);
}
_getch();
return 0;
}

Getting process ID's

My program below will concatenate the names of the processes into the names string. How can I change it to include the process ID's instead of names? What type should names be, how to initialise it and how to concatenate every proc32.th32ProcessID in it ?
PROCESSENTRY32 proc32;
TCHAR names[MAX_PATH]=L"";
if(hSnap == INVALID_HANDLE_VALUE)
{
cout<<"invalid handle value error!\n";
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32);
if(!Process32First(hSnap, &proc32))
{
cout<<"Tread32First() error!\n";
CloseHandle(hSnap);
return ;
}
do
{
//cout<<"Current process id: "<<GetCurrentProcessId()<<"\n";
wcout<<L"Process Name: "<<proc32.szExeFile<<"\n";
cout<<"Process ID: " <<proc32.th32ProcessID<<"\n";
cout<<"Thread Count: "<<proc32.cntThreads<<"\n"<<endl;
lstrcat(names, proc32.szExeFile);
lstrcat(names, L"\n");
}while(Process32Next(hSnap, &proc32));
Since you are using C++ anyway, you should make use of it. Use std::vector, std::wstring, etc:
PROCESSENTRY32W proc32;
vector<wstring> names;
vector<DWORD> ids;
if (hSnap == INVALID_HANDLE_VALUE)
{
cout << "invalid handle value error!" << endl;
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnap, &proc32))
{
cout << "Tread32First() error!" << endl;
CloseHandle(hSnap);
return ;
}
do
{
//cout << "Current process id: " << GetCurrentProcessId() << endl;
wcout << L"Process Name: " << proc32.szExeFile << endl;
cout << "Process ID: " << proc32.th32ProcessID << endl;
cout << "Thread Count: " << proc32.cntThreads << endl << endl;
names.push_back(proc32.szExeFile);
ids.push_back(proc32.th32ProcessID);
}
while (Process32Next(hSnap, &proc32));
// use names and ids as needed...
Or:
PROCESSENTRY32W proc32;
vector<PROCESSENTRY32W> procs;
if (hSnap == INVALID_HANDLE_VALUE)
{
cout << "invalid handle value error!" << endl;
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnap, &proc32))
{
cout << "Tread32First() error!" << endl;
CloseHandle(hSnap);
return ;
}
do
{
//cout << "Current process id: " << GetCurrentProcessId() << endl;
wcout << L"Process Name: " << proc32.szExeFile << endl;
cout << "Process ID: " << proc32.th32ProcessID << endl;
cout << "Thread Count: " << proc32.cntThreads << endl << endl;
procs.push_back(proc32);
}
while (Process32Next(hSnap, &proc32));
// use procs as needed...

SendMessage not getting text

Not sure why SendMessage isn't getting the text from the class I need. I have done this before but it was is VisualBasic and I wanted to port it over to c++. I have not tried this code on any other program. I was reading something about it possibly being unicode but I wasn't sure on how to implement that.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
using namespace std;
void FindmIRC()
{
cout << "[mIRC]" << endl;
cout << "- find mIRC window" << endl;
HWND hwndmIRC = FindWindow(L"mIRC", NULL);
if (NULL != hwndmIRC)
{
cout << " + found mIRC window" << endl;
cout << "- find MDIClient window" << endl;
HWND hwndMDIClient = FindWindowEx(hwndmIRC, NULL, L"MDIClient", NULL);
if (NULL != hwndMDIClient)
{
cout << " + found MDIClient window" << endl;
cout << "- find mIRC_Channel window" << endl;
HWND hwndmIRC_Channel = FindWindowEx(hwndMDIClient, NULL, L"mIRC_Channel", NULL);
if (NULL != hwndmIRC_Channel)
{
cout << " + found mIRC_Channel window" << endl;
cout << "- find Static window" << endl;
HWND hwndStatic = FindWindowEx(hwndmIRC_Channel, NULL, L"Static", NULL);
if (NULL != hwndStatic)
{
cout << " + found Static window" << endl;
cout << "- get text length" << endl;
int textLen = (int)SendMessage(hwndStatic, WM_GETTEXTLENGTH, 0, 0);
if (0 < textLen)
{
cout << "- getting text" << endl;
const int bufferSize = 1024;
char textBuffer[bufferSize] = "";
SendMessage(hwndStatic, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
cout << "[begin text]" << endl;
cout << textBuffer << endl;
cout << "[end text]" << endl;
}
else
{
cerr << "No text." << endl;
}
}
else
{
cerr << "Static not found." << endl;
}
}
else
{
cerr << "mIRC_Channel not found." << endl;
}
}
else
{
cerr << "MDIClient not found." << endl;
}
}
else
{
cerr << "mIRC not open." << endl;
}
}
int main()
{
FindmIRC();
return 0;
}
The highlighted class is what has the text:
The program find the class with no problem and does not report not finding it so I don't see a reason why it should not find it. Any help is great!
As you can see on your spy++ output, highlighted control does not contain any text. It should appear on the left of Static in "".

C++ Clearing wstring

My problem is that no method that used to work on string works on wstring.
So I'm asking how I could easily clear wstring for aesthetical purpose.
My code right now:
while (!foundRightOne)
{
wstring cTitle;
ForegroundWindow = GetForegroundWindow();
cout << "FRGW " << ForegroundWindow << endl;
int len = GetWindowTextLengthW(ForegroundWindow) + 1;
wchar_t * windowTitle = new wchar_t[len];
GetWindowTextW(ForegroundWindow, windowTitle, len);
title += windowTitle;
// OUTPUT
cTitle = L"Title: ";
cTitle += title;
wcout << cTitle << endl;
cTitle = ' ';
//OUTPUT
keyPress = getchar();
system("CLS");
if (keyPress == 'y' || keyPress == 'Y')
{
foundRightOne = true;
}
}
Basically it loops while I press y or Y, I press when I see the right cTitle and after ~20 cycles cTitle gets fully filled with text from last cycles.
std::wstring::clear should work, as both it and std::string are std::basic_strings. Check out std::basic_string documentation if you're somehow having trouble.
#include <iostream>
int main()
{
std::string regularString("regular string!");
std::wstring wideString(L"wide string!");
std::cout << regularString << std::endl << "size: " << regularString.size() << std::endl;
std::wcout << wideString << std::endl << "size: " << wideString.size() << std::endl;
regularString.clear();
wideString.clear();
std::cout << regularString << std::endl << "size: " << regularString.size() << std::endl;
std::wcout << wideString << std::endl << "size: " << wideString.size() << std::endl;
}
Output:
regular string!
size: 15
wide string!
size: 12
size: 0
size: 0
Here's an ideone link to that code.

fstream not writing to file

I'm having trouble with fstream not writing to my file when it is located in a specific place. In the following code when the commented line is uncommented the file is written to, however when it is commented the file is not written to at all. I've added a bunch of console output and it says both of the outputs around the "outputFile << newWord << endl;" are completed but the file is never actually written to.
void write_index( string newWord )
{
fstream outputFile( "H:\\newword.txt", ios::app );
int same = 0;
string currWord;
currWord.resize(5);
//outputFile << newWord << endl;
while( !outputFile.eof() )
{
getline( outputFile, currWord );
cout << "Checking if " << newWord << "is the same as " << currWord << endl;
if( newWord == currWord )
{
cout << "It is the same" << endl;
same = 1;
break;
}
}
if( same != 1 )
{
cout << "Writing " << newWord << "to file" << endl;
outputFile << newWord << endl;
cout << "Done writing" << endl;
}
outputFile.close();
}
I'm somewhat confident you're looking for something along these lines:
void write_index( const string& newWord )
{
fstream outputFile( "H:\\newword.txt", ios::in);
bool same = false;
if (outputFile)
{
string currWord;
while (getline(outputFile, currWord))
{
cout << "Checking if " << newWord << " is the same as " << currWord << endl;
same = (newWord == currWord);
if (same)
{
cout << "It is the same" << endl;
break;
}
}
}
if (!same)
{
outputFile.close();
outputFile.open("H:\\newword.txt", ios::app);
cout << "Writing " << newWord << "to file" << endl;
outputFile << newWord << endl;
cout << "Done writing" << endl;
}
outputFile.close();
}
There are better ways to do this, but that will likely be a decent place to start.