Printing whole folder - c++

This is my first time ever asking something here, so I apologize if I do anything wrong.
I have a problem with a program I am trying to write.
What I want to do is have the user pick a folder and the program prints all the documents (mostly a mix of pdf and word files) without any more user interaction.
And to make matters worse, the system I am working in is severely restricted, so my testing possibilities are limited.
Basically, what I want is to do some sort of equivalent of right-click printing in code. Or copy a file to the printer queue.
If anyone could point me in the right direction I would be very grateful. Google isn't much help. I tried executing the print command, but it doesn't do what I want it to.
My current attempt:
void folderFrame::onOpen(wxCommandEvent &event)
{
int listLength;
wxArrayString fileList;
wxString pFolder, pCommand;
chooseFolder = new wxDirDialog(this, _("Please choose folder from which to print:"), "", wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST, wxDefaultPosition, wxDefaultSize,"");
if (chooseFolder->ShowModal() == wxID_OK) //User chooses folder
{
int pResult;
pFolder = chooseFolder->GetPath();
wxDir::GetAllFiles(pFolder, &fileList, "", wxDIR_FILES); //Get all files in folder
listLength = fileList.GetCount(); //Get number of files
for(int i = 0; i < listLength; i++) //For each file...
{
pCommand = _T("print /d:\\\\app065\\300408_P18 "); //Make command to execute
pCommand.append(fileList[i]);
theText->AppendText(fileList[i]); //The text ctrl is to check what commands I actually execute
theText->AppendText("\n"); //And to check if the system return errors
theText->AppendText(pCommand);
theText->AppendText("\n");
pResult = wxExecute(pCommand, wxEXEC_SYNC | wxEXEC_SHOW_CONSOLE | wxEXEC_NOEVENTS, NULL, NULL); //Print!!!!
theText->AppendText(wxString::Format(wxT("%i\n"), pResult));
}
}
}
print .... prints only one file.
copy only returns 0 (success error code, but nothing happens)
So, here I am stuck. Any help getting forward would be greatly appreciated.
/Mike

This actually isn't really hard, since your path hints at Windows. The second parameter of ShellExecute can be "print".

Related

CFile and CStdioFile Reading one byte at a time

Using C++ MFC with Visual Studio 2008, I am trying to using CFile or CStdioFile to read in the last line of a text document, store it, and then reprint it after the file has had text amended to it.
I have gotten that part working, the only problem is is that it is not dynamic, you have to manually created an offSet for however long the last line is. As such, I am trying to make a function that reads the last line until it finds a common element in all of the files this will be working with, and count how many bytes there were. This is what I have now for that:
int MeasureLastTag(CStdioFile* xmlFile)
{
TCHAR lastTag[1];
CString tagBracket = _T("");
xmlFile->Seek(0, CFile::end);
int count = 0;
while(tagBracket != _T("<")) //Go back two, read ahead one
{
xmlFile->Seek(-2, CFile::current);
xmlFile->Read(lastTag, 1);
tagBracket = lastTag;
count++;
}
return count;
}
However, this causes an infinite loop that I can't seem to shake. Any ideas on how to make it work?
Additional Information, this is a sample of the file.
<Station>
</Station>
I want it to read < /Station> until it gets to the <, counting along the way.
Changing TCHAR lastTag[1] to char lastTag[1] has solved the issue.

C++ SendMessage loop stop and end of line

Here is my dilemma. I have a program that uses SendMessage to get text from a chat program. Now when I did this in Visual Basic I just put the SendMessage in a loop and whenever the chat was updated so would my output.
This is not the case with C++. When I put the SendMessage in a loop it loops forever. Say the Chat is something like:
Apples
Bananas
Cheerios
Now lets say I run my program it finds the text and starts looping. Now in Visual Basic it would keep looping until it hit Cheerios and it would stop and wait until someone in the chat typed something else.
With C++ it would output:
Apples
Bananas
Cheerios
Apples
Bananas
Cheerios
...
And go on forever. Is there a way to stop this? I was thinking along the terms of EOF but that wouldn't help because as soon as it hit the last line it would go out of the loop and wouldn't pick up anything else that people typed in chat.
Here is the portion of the code that gets the text. Now not that I do have an loop that won't end until I set bLoop to True.
cout << " + found RichEdit20W window at: " << hwndRichEdit20W << endl << endl;
cout << "- get text " << endl;
bool bLoop = false;
int textLen = (int)SendMessage(hwndRichEdit20W, WM_GETTEXTLENGTH, 0, 0);
while (bLoop == false)
{
const int MAXSIZE = 32678;
wchar_t szBuf[MAXSIZE];
SendMessage(hwndRichEdit20W, WM_GETTEXT, (WPARAM)textLen, (LPARAM)szBuf);
wcout << szBuf;
}
Thanks for any help!
VB CODE UPDATE
This is how I did it in my VB program. This is for a game console and not a chat window but it should be the same concept right?
Note Do While PARENThwnd <> IntPtr.Zero The handle is never 0 so it will be an infinite loop.
Private Sub bwConsole_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwConsole.DoWork
Dim PARENThwnd As IntPtr
Dim CHILDhwnd As IntPtr
PARENThwnd = FindWindow(Nothing, "ET Console")
If PARENThwnd = IntPtr.Zero Then
txtConsole.Text = "ET Console is not availble."
Else
Do While PARENThwnd <> IntPtr.Zero
CHILDhwnd = GetDlgItem(PARENThwnd, 100)
Dim Handle As IntPtr = Marshal.AllocHGlobal(32767)
Dim NumText As Integer = CInt(SendMessage(CType(CHILDhwnd, IntPtr), WM_GETTEXT, CType(32767 \ Marshal.SystemDefaultCharSize, IntPtr), Handle))
Dim Text As String = Marshal.PtrToStringAuto(Handle)
txtConsole.Text = Text
Marshal.FreeHGlobal(Handle)
txtConsole.SelectionStart = txtConsole.TextLength
txtConsole.ScrollToCaret()
rtxtDefinition.Text = ""
Call GetDefinitions(txtConsole.Text)
Loop
End If
End Sub
Also will not be able to answer any questions until later tonight. Off to work.
You never update your bLoop variable, causing the loop to run infinitely. If you want to break your loop, implement a loop termination condition that changes the value of bLoop.
A condensed version of your code does the following:
bool bLoop = false;
while ( bLoop == false ) {
// This loop never terminates, unless bLoop is set to true
}
It is somewhat hard to understand, why you are looping to begin with. I suppose you hope the SendMessage call would somehow guess that you don't want it to run, until some condition is met. There is no such magic built into functions.
To respond to text changes you should choose a different solution altogether: An event-based model. The supported way to implement this is through UI Automation.
The first thing you need to understand is that the two versions of the code are doing very different things with the contents of the chat window. The VisualBasic version is taking that text and stuffing it into a text control. This has the effect of replacing the existing text with the new text from the chat window so you never see duplicates. The C++ version outputs the text to the output stream (console) each time it retrieves it. Unlike the text control used in the VB version the output stream appends the text rather than replaces it.
To get around this you need to keep tract of the previous text retrieved from the chat window and only output the difference. This may mean appending new text from the chat or outputting the entire string. The code below maintains a string buffer and manages appending or replacing the history of the chat. It also creates a new string containing only the differences from the last change allowing you to easily deal with updates.
class ChatHistory
{
std::wstring history;
public:
std::wstring update(const std::wstring& newText)
{
const std::wstring::size_type historySize = history.size();
if(newText.compare(0, historySize, history.c_str()) == 0)
{
history.append(newText.c_str() + historySize, newText.size() - historySize);
return history.c_str() + historySize;
}
else
{
history = newText;
}
return newText;
}
};
Below are the necessary changes to your code to use it. I haven't had a chance to test it with your exact set up but it should work.
ChatHistory history;
while (bLoop == false)
{
const int MAXSIZE = 32678;
wchar_t szBuf[MAXSIZE];
SendMessage(hwndRichEdit20W, WM_GETTEXT, (WPARAM)textLen, (LPARAM)szBuf);
std::wcout << history.update(szBuf);
}
In both VBA and C++ that structure will cause an infinite loop.
It sounds like you want an infinite loop, but don't want an infinite print out of the contents of the buffer.
As #IInspectable has pointed out (thanks!) sendmessage will copy the contents of the chat program into the buffer at every iteration of the loop.
So, either purge the chat window or let the program know what content is new and needs to be printed.
I'm on a linux box at the moment, so can't test this very easily. Please let me know if the following works...
#include <cwchar> // so we can use wcschr function
const int MAXSIZE = 32678; // moved outside of the loop
wchar_t szBuf[MAXSIZE]; // moved outside of the loop
szBuf[0] = L'\0'; // set sentinel (wide NULL!)
wchar_t* end_printed_message = szBuf;
//pointer to the end of the content that has been printed
while (bLoop == false)
{
SendMessage(hwndRichEdit20W, WM_GETTEXT, (WPARAM)textLen, (LPARAM)szBuf);
wchar_t* end_message_buffer = wcschr(szBuf, L'\0'); // find end of the string using wchar version of strchr()
if(end_printed_message != end_message_buffer){ // if the buffer contains something new
wcout << end_printed_message; // print out the new stuff (start at old end)
end_printed_message = end_message_buffer; // update the pointer to the end of the content
}
}
I don't know what headers you've already included, hence the use of 'ol faithful <cstring>
As an aside (yes, I will still bang on about this) mixed use of (w)cout is generally bad and may cause problems.

Gtk::TextView with constant string

I am using Gtkmm 3+ and What I am trying to do is have the text buffer have the constant string "> " even if the user tries to delete it. In addition when the user pressed return it will automatically be there again. Basically have a constant string like a terminal does.
The only way I can think about about accomplishing this would be to connect to the delete and backspace signals so the user cannot delete the string. But, is there a better way?
so far this is the only way I can think of:
//in constructor
txt_view_i_.signal_event().connect(sigc::mem_fun(*this, &MainWindow::inputEvent));
//function
bool MainWindow::inputEvent(GdkEvent* event)
{
if((event->key.keyval == GDK_KEY_BackSpace || event->key.keyval == GDK_KEY_Delete) && buffer_input_->get_char_count() < 3)
return true;
return false;
}
But doesn't work perfectly, because if you type in more then 3 characters then go to the beginning of the line you can delete the constant string.
Another way I just thought about was to add a label to the TextView widget. I did that but, the user could still delete it. Here is the code for that:
Gtk::TextBuffer::iterator it = buffer_input_->get_iter_at_line(1);
Glib::RefPtr<Gtk::TextChildAnchor> refAnchor = buffer_input_->create_child_anchor(it);
Gtk::Label* lbl = Gtk::manage(new Gtk::Label("> "));
txt_view_i_.add_child_at_anchor(*lbl, refAnchor);
This is very similar, but not quite identical, to the question I answered here: You can create a GtkTextTag that makes its contents uneditable, and apply it from the beginning of the buffer up to and including the "> " prompt.
Then when you receive input, append your output to the buffer and then append a new prompt on the next line, and re-apply the tag to make the whole thing uneditable.
The links in the linked answer show some C code where this is done, even including a prompt. It's not Gtkmm or C++, but it should serve as an illustration.
Here is the code I used to solve it:
Glib::RefPtr<Gtk::TextBuffer::Tag> tag = Gtk::TextBuffer::Tag::create();
tag->property_editable() = false;
Glib::RefPtr<Gtk::TextBuffer::TagTable> tag_table = Gtk::TextBuffer::TagTable::create();
tag_table->add(tag);
buffer_input_ = Gtk::TextBuffer::create(tag_table);
txt_view_i_.set_buffer(buffer_input_);
scroll_win_i_.add(txt_view_i_);
Gtk::TextBuffer::iterator buffer_it_ = buffer_input_->begin();
buffer_input_->insert_with_tag(buffer_it_, "> ", tag);
Here is how I made it so that the user cannot edit before the constant string:
//connect to the mark set signal
buffer_input_->signal_mark_set().connect(sigc::mem_fun(*this, &MainWindow::setMark));
//make the box uneditable
void MainWindow::setMark(const Gtk::TextBuffer::iterator& it, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark)
{
if(it.get_offset() < 2)
txt_view_i_.set_editable(false);
else
txt_view_i_.set_editable(true);
}
Hopefully someone will find this useful.

Why can't QFile read from the "~" directory?

I've tried the following short example to find out about a bug in a bigger program I am working on. It looks like QFile doesn't support unix (or the shell's) notation for the home directory:
#include <QFile>
#include <QDebug>
int main()
{
QFile f("~/.vimrc");
if (f.open(QIODevice::ReadOnly))
{
qDebug() << f.readAll();
f.close();
}
else
{
qDebug() << f.error();
}
}
As soon as I replace the "~" with my real home directory path, it works. Is there an easy workaround - some setting to enable? Or do I have to go the "ugly" way and ask QDir for the home directory of the current user and prepend that manually to each path?
Addendum: It's clear that usually the shell performs the tilde expansion so programs would never see that. Still it is so convenient in unix shells that I hoped the Qt implementation for file access would have that expansion included.
You can just create a helper function to do this for you, something like:
QString morphFile(QString s) {
if ((s == "~") || (s.startsWith("~/"))) {
s.replace (0, 1, QDir::homePath());
}
return s;
}
:
QFile vimRc(morphFile("~/.vimrc"));
QFile homeDir(morphFile("~"));
A more complete solution, allowing for home directories of other users as well, may be:
QString morphFile(QString fspec) {
// Leave strings alone unless starting with tilde.
if (! fspec.startsWith("~")) return fspec;
// Special case for current user.
if ((fspec == "~") || (fspec.startsWith("~/"))) {
fspec.replace(0, 1, QDir::homePath());
return fspec;
}
// General case for any user. Get user name and length of it.
QString name (fspec);
name.replace(0, 1, ""); // Remove leading '~'.
int len = name.indexOf('/'); // Get name (up to first '/').
len = (len == -1)
? name.length()
: len - 1;
name = name.left(idx);
// Find that user in the password file, replace with home
// directory if found, then return it. You can also add a
// Windows-specific variant if needed.
struct passwd *pwent = getpwnam(name.toAscii().constData());
if (pwent != NULL)
fspec.replace(0, len+1, pwent->pw_dir);
return fspec;
}
Just one thing to keep in mind, the current solution is not portable to Windows (as per the comments in the code). I suspect this is okay for the immediate question since .vimrc indicates that's not the platform you're running on (it's _vimrc on Windows).
Tailoring the solution to that platform is possible, and indeed shows that the helper-function solution is a good fit since you'll only have to change one piece of code to add that.
It has nothing to do with not supporting UNIX; the expansion of tildes to the user's home directory is a substitution performed by the shell, so yes, you will have to manually replace them.
Please submit a suggestion to the Qt bugtracker.
https://bugreports.qt.io/
Take a look at the C library function glob, which will do tilde expansion (and possibly wildcard expansion and various other functions too).

How to hide using C\C++ with Show hidden Files and Folder enabled

I would like to know how to programmatically hide a file but making the file or folder still hidden with Show hidden files and folders enabled from the Tools->Folder options of Explorer.
I am pretty sure this is not possible as it would be a security vulnerability (programs could place unknown files on your hard drive that you couldn't see or delete).
Even Windows system files are visible.
There is NO user mode API to make files hidden from 'show hidden files', and it's a good thing to.
The only way to do this is to get your code to run in the kernel. The rootkit that Sony "accidently" installed on user machines a couple years ago when they were trying to prevent CDs from being rippable could do it. But there is no legitimate use for the ability to hide files from system administrators and power users.
You want the SetFileAttributes function available by #include'ing Windows.h: http://msdn.microsoft.com/en-us/library/aa365535%28VS.85%29.aspx
In code:
BOOL result = SetFileAttributes(L"c:\path\to\file", FILE_ATTRIBUTE_HIDDEN);
As for keeping a file hidden from the "show hidden files" option, that's much more difficult and I can't think of a legitimate reason to do it - the only programs that do are rootkits for nefarious purposes.
Use a file system filter driver. But since you have to ask - just don't do it. Not trying to be rude here, it's just that that is a task that is very hard to get right.
As many have said before, there's no straightforward way to fully 'hide' a file like that.
If you can accept not truly hiding the file but merely obfuscating it, you can always embed it inside a dummy file. Take the file you want to hide, build a container file to hold it, and name that dummy file with a random name. For example, you could place the real file's filename starting at offset 512 of the dummy file and place the file's contents starting at offset 1024, inserting 64 bytes of random data every 1KB. Pad the end with empty space out to the nearest multiple of 4KB, fill the empty space with random bytes, and generate a random sequence of characters to use for a filename. Now, you can "hide" the file while it's still visible in the filesystem.
However, that's merely "security by obscurity" and can be defeated by a clever attacker with a hex editor.
If you're simply trying to make sure a file isn't visible to the casual filesystem browser, you can always compress the file, encrypt it, and randomize the filename. If you need to be able to access/execute the file as-is while it is "hidden", then you're probably (hopefully) out of luck.
This program is used to show all hidden files and folder in your system drive and it can run well even system has affected by virus and worms, usually, some kinds of the virus change your files and folders in a hidden state, this script will help you to view your files and folders again. The main feature is without installation it can play.
Please find the source code from my tech blog - http://www.algonuts.info/how-to-show-hidden-files-and-folders-using-c.html
#include<iostream>
#include<conio.h>
#include<dirent.h>
#include<dir.h>
#include<process.h>
#include<string.h>
#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<sys/stat.h>
struct ffblk vfile;
unsigned long int udata;
char ch,present[MAXPATH];
int next_directory(char *);
void scan_directory(char *);
char base[]="X:\\";
using namespace std;
int main(int account,char *arg[],char *env[])
{
clrscr();
getcwd(present,MAXPATH);
DIR *dir;
struct dirent *temp;
cout<<"\nWelcome to Unhidden for identify the hidden files";
cout<<"\n\nEnter drive:";
cin>>ch;
base[0]=ch;
if((dir = opendir(base)) == NULL)
{
clrscr();
cout<<"\nError : Derive not found ";
getch();
exit(0);
}
scan_directory(base);
while((temp = readdir(dir)) != NULL)
{
char *directory = (char *) malloc(3+strlen(temp->d_name)+1);
strcpy(directory,base);
strcat(directory,temp->d_name);
next_directory(directory);
free(directory);
}
closedir(dir);
clrscr();
cout<<"\nSystem: Successfully Unhidden it";
sleep(3);
return 0;
}
int next_directory(char *path)
{
int count=0;
DIR *dirtemp;
char *hold,*temp;
struct dirent *ptemp;
hold=path;
if ((dirtemp = opendir(path)) != NULL)
scan_directory(path);
else
return 0;
while((ptemp = readdir(dirtemp)) != NULL)
{
char *directory = (char *) malloc(1+strlen(ptemp->d_name)+1);
directory[0]='\\';
strcpy(directory+1,ptemp->d_name);
if(directory[1]!='\.')
{
count=strlen(hold);
temp = (char *) malloc(strlen(hold)+strlen(directory)+1);
strcpy(temp,hold);
strcat(temp,directory);
free(directory);
if(opendir(temp)!=NULL)
next_directory(temp);
temp[count]='\0';
hold=temp;
}
else
free(directory);
}
closedir(dirtemp);
return 0;
}
void scan_directory(char *tempo)
{
cout<<"\n"<<tempo;
unsigned count;
if(present[0]==tempo[0])
chdir(tempo);
else
{
setdisk(tempo[0]-65);
chdir(tempo);
}
udata = findfirst("*.*",&vfile,0x02);
while(!udata)
{
_dos_getfileattr(vfile.ff_name,&count);
if (count & _A_HIDDEN)
{
_dos_getfileattr(vfile.ff_name,&count);
count=count & 248;
_dos_setfileattr(vfile.ff_name,count);
}
udata=findnext(&vfile);
}
if(present[0]==tempo[0])
system("cd\\");
chdir(present);
}