Open files Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
JRDiaz
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Oct 03, 2008 4:05 am
Location: Mexico

Open files

Post by JRDiaz »

Hi, I need help, I did a very long time ago an app capable to open any kind of files. In the button code I used to write:
system (start calc.exe); to open windows calculator, the thing is that it uses windows console and a dark window appears to open the file. I had been using this code until i found another piece of code which opened files without that dark window and it was even faster. I don't remember that code. Could you help me please?

Searching in Google I found this, but I don't know how to use it.

1
Include files
#include <wx/filesys.h>
Code
virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location)

2
Include files
#include <wx/filesys.h>
Code
wxFSFile* OpenFile(const wxString& location, int flags = wxFS_READ)
JRDíaz - Is. 41:10
BarrRobot
Knows some wx things
Knows some wx things
Posts: 32
Joined: Thu Nov 26, 2009 12:50 am
Location: Birmingham, UK

Re: Open files

Post by BarrRobot »

OpenFIle will open a file in your program so that you can read or write the contents. You want to start (execute) another program. Try:

wxExecute(.....)
Documentation is here http://docs.wxwidgets.org/trunk/group__ ... cctrl.html
and there is also a sample program - see http://docs.wxwidgets.org/trunk/page_sa ... mples_exec

[The link in the documentation is bad, here is the example program: http://svn.wxwidgets.org/viewvc/wx/wxWi ... ples/exec/]
Look at the code for MyFrame::DoAsyncExec and MyFrame::OnSyncExec


Failing that, look at the Windows API and CreateProcess, WinExec, ExecProgram, etc.
If, 6 months later, you can't understand what you coded, it was too complicated to begin with.
JRDiaz
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Oct 03, 2008 4:05 am
Location: Mexico

Re: Open files

Post by JRDiaz »

Thanks for your answer. After a few days on vacation i'm back to my project, and searching in google I found this:

#include <windows.h>

ShellExecute(NULL, "open", "1.exe", NULL, NULL, NULL);

but I can't make it work. Any suggestions?
Last edited by JRDiaz on Fri Jan 13, 2012 12:33 am, edited 1 time in total.
JRDíaz - Is. 41:10
BarrRobot
Knows some wx things
Knows some wx things
Posts: 32
Joined: Thu Nov 26, 2009 12:50 am
Location: Birmingham, UK

Re: Open files

Post by BarrRobot »

It would help if you told us what the error message was, or what ShellExecute returns.

This works for me:

Code: Select all

/*
 * WxButton1Click
 */
void StartFrm::WxButton1Click(wxCommandEvent& event)
{
	// insert your code here

    // the PID of the process we launch
    long m_pid;

    // command we execute
    wxString cmd;

    cmd = wxT("notepad.exe");

    m_pid = wxExecute(cmd, wxEXEC_ASYNC);

    if ( !m_pid )
    {
        ; // Execution failed ... tell the user
    }
    else
    {
        ; // The process was launched ... tell the user or do something useful
    }
}
Start from this and add functionality as you wish. You should ALWAYS check the value that is returned - m_pid in this case - to know whether the function succeeded or failed.
If, 6 months later, you can't understand what you coded, it was too complicated to begin with.
JRDiaz
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Oct 03, 2008 4:05 am
Location: Mexico

Re: Open files

Post by JRDiaz »

Thanks a lot, it worked perfectly!

Now there's something else I'd like to try:
Is there any way to force windows to open a file using a specific program?
For example:
If I have a file called "test.tst" how could i open it using windows notepad without associating *.tst files to the notepad.
JRDíaz - Is. 41:10
BarrRobot
Knows some wx things
Knows some wx things
Posts: 32
Joined: Thu Nov 26, 2009 12:50 am
Location: Birmingham, UK

Re: Open files

Post by BarrRobot »

If I have a file called "test.tst" how could i open it using windows notepad without associating *.tst files to the notepad.
If the program you want to use accepts arguments on the command line, you write:
notepad.exe test.tst
(If the path includes spaces, you must quote it, e.g. "C:\Documents and Settings\test.tst")

The link I gave you on December 18th does tell you all of this:
Parameters:
command The command to execute and any parameters to pass to it as a single string, i.e. "emacs file.txt".
If you are going to use wxWidgets, you must learn how to read and understand the documentation. It is there to help you. Look at the documentation page at http://docs.wxwidgets.org/trunk/group__ ... cctrl.html
Look down the page at the heading Function Documentation. Immediately below this is:
long wxExecute ( const wxString & command,
int flags = wxEXEC_ASYNC,
wxProcess * callback = NULL,
const wxExecuteEnv * env = NULL
)
"long" means the function returns a variable of type "long", the parameters that are written with a value (e.g. wxProcess * callback = NULL) are optional and take the value shown (NULL in this case) if you do not supply that parameter.
The notes give you more details on how the function works and the effect of the parameters. It should tell you what the return value is - this is an omission, but in this case you can look at the example program to work out what it is.
If, 6 months later, you can't understand what you coded, it was too complicated to begin with.
Post Reply