AverageJoez
asked on
GUI
I have a Windows console app that gives the user the option to print/merge files. Right now the user has to manually enter in the file path, but I was wondering if there is a way to open a GUI, i.e. File Open window or Print Options dialog, and allow the user to select the files that way, and then have the file paths automatically be sent back to the console.
Does anyone know of a way to do this?
Thanks.
Does anyone know of a way to do this?
Thanks.
There is a way for all apps to use the default dialogs
(e.g. ->
//=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-
//
// _DoSearch
//
void CP2PPreferences::_DoSearch ( TCHAR *pszInitial, TCHAR *pszFile, UINT cch )
{
OPENFILENAME ofn;
CHAR szFileUrl[MAX_PATH+1];
szFileUrl[0] = '\0';
if( !pszInitial || !_tcslen( pszInitial ) )
pszInitial = _T("C:\\");
memset (&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = m_hWnd;
ofn.lpstrFilter = "Applications (*.exe)\0*.exe\0\0";
ofn.lpstrDefExt = "exe";
ofn.lpstrFile = szFileUrl;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = pszInitial;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrTitle = "Select the executable of the P2P application you want to monitor.";
if (GetOpenFileName(&ofn) )
{
if( _tcslen( szFileUrl ) > 0 )
_tcsncpy( pszFile, szFileUrl, MAX_PATH );
}
)
(e.g. ->
//=-=-=-=-=-=-=-=-=-=-=-=-
//
// _DoSearch
//
void CP2PPreferences::_DoSearch
{
OPENFILENAME ofn;
CHAR szFileUrl[MAX_PATH+1];
szFileUrl[0] = '\0';
if( !pszInitial || !_tcslen( pszInitial ) )
pszInitial = _T("C:\\");
memset (&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = m_hWnd;
ofn.lpstrFilter = "Applications (*.exe)\0*.exe\0\0";
ofn.lpstrDefExt = "exe";
ofn.lpstrFile = szFileUrl;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = pszInitial;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrTitle = "Select the executable of the P2P application you want to monitor.";
if (GetOpenFileName(&ofn) )
{
if( _tcslen( szFileUrl ) > 0 )
_tcsncpy( pszFile, szFileUrl, MAX_PATH );
}
)
ASKER
mrwad99, I'm sure most people would be more appreciative of using a GUI, but currently I only know ANSI C++, not VC++.
Daij-Djan, does that function need the rest of the class that it belongs to. And, what needs to be passed to fill the TCHAR *pszInitial, TCHAR *pszFile, UINT cch vars?
Daij-Djan, does that function need the rest of the class that it belongs to. And, what needs to be passed to fill the TCHAR *pszInitial, TCHAR *pszFile, UINT cch vars?
no, it is indepndant
And TCHARs are only chars or wchars
pszInitial = dir to start browsing in (e.g. C:\\windows or so...)
pszFile = pointer to receive output -> I am copying to this at the end (BAD now that I see it)
cch = size of pointer of output in characters
And TCHARs are only chars or wchars
pszInitial = dir to start browsing in (e.g. C:\\windows or so...)
pszFile = pointer to receive output -> I am copying to this at the end (BAD now that I see it)
cch = size of pointer of output in characters
ASKER
Daij-Djan, what headers have to be included, also do any libraries need to be included?
Thanks.
Thanks.
vc++ is just a compiler it's not a new language. what your refering to is wizards that allow you to create dialogs and MFC apps, but you can use the vc++ compiler for what your doing now as well as win32 api gui apps and it'll run in any other compiler.
ASKER
I know what VC++ is, but what I'm asking is what .h files must be included? Because, the data type OPENFILENAME is not recognized.
ASKER
I included windows.h to rid some of the errors, but the errors that do remain are basically undefined identifier. These are the errors that remain:
Error : undefined identifier '_tcslen'
hello.cpp line 15 if( !pszInitial || !_tcslen( pszInitial ) )
Error : undefined identifier '_T'
hello.cpp line 16 pszInitial = _T("C:\\");
Error : undefined identifier 'm_hWnd'
hello.cpp line 21 ofn.hwndOwner = m_hWnd;
Error : undefined identifier '_tcslen'
hello.cpp line 34 if( _tcslen( szFileUrl ) > 0 )
Error : undefined identifier '_tcsncpy'
hello.cpp line 35 _tcsncpy( pszFile, szFileUrl, MAX_PATH );
Error : expression syntax error
hello.cpp line 37 )
Error : ### Error: Compilation aborted at end of file ###
Error : undefined identifier '_tcslen'
hello.cpp line 15 if( !pszInitial || !_tcslen( pszInitial ) )
Error : undefined identifier '_T'
hello.cpp line 16 pszInitial = _T("C:\\");
Error : undefined identifier 'm_hWnd'
hello.cpp line 21 ofn.hwndOwner = m_hWnd;
Error : undefined identifier '_tcslen'
hello.cpp line 34 if( _tcslen( szFileUrl ) > 0 )
Error : undefined identifier '_tcsncpy'
hello.cpp line 35 _tcsncpy( pszFile, szFileUrl, MAX_PATH );
Error : expression syntax error
hello.cpp line 37 )
Error : ### Error: Compilation aborted at end of file ###
The above errors are TCHAR related.
include tchar.h OR
remove all t methods.
Use strlen, strncmp and no _T() around string
Last but not least, replace m_hWnd with your hwnd or set it to NULL
also, do this:
Header Declared in Commdlg.h, include Windows.h
Import library Comdlg32.lib
include tchar.h OR
remove all t methods.
Use strlen, strncmp and no _T() around string
Last but not least, replace m_hWnd with your hwnd or set it to NULL
also, do this:
Header Declared in Commdlg.h, include Windows.h
Import library Comdlg32.lib
ASKER
Daij-Djan I get an Unhandled Exception every time I try to open a file.
an exception at runtime? So.. what does it say? WHERE is the crash. -> DEBUG and what are the values of the variables.
It so works in a gui app.
well maybe you need a msgloop? NAH .. or a parent window?
It so works in a gui app.
well maybe you need a msgloop? NAH .. or a parent window?
ASKER
Some of the sub-values of ofn have the error: "Can't read the memory from the address"
lpstrFilter, lpstrCustomFilter, lpstrFile are all of the sub-values of ofn that get the error. And, pszFile also has the same error.
szFileUrl shows up red.
(Compiled under Metrowerks CodeWarrior v. 7)
When compiled under MS VC++, I do not get an unhandled exception, but the program does crash.
lpstrFilter, lpstrCustomFilter, lpstrFile are all of the sub-values of ofn that get the error. And, pszFile also has the same error.
szFileUrl shows up red.
(Compiled under Metrowerks CodeWarrior v. 7)
When compiled under MS VC++, I do not get an unhandled exception, but the program does crash.
Mmmh. Post your code, please.
Maybe because you are using a console? Weird
Maybe because you are using a console? Weird
ASKER
#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include <tchar.h>
using namespace std; //introduces namespace std
void main()
{
TCHAR *pszInitial="C:\\Windows";
TCHAR *pszFile;
UINT cch;
OPENFILENAME ofn;
CHAR szFileUrl[MAX_PATH+1];
szFileUrl[0] = '\0';
if( !pszInitial || !_tcslen( pszInitial ) )
pszInitial = _T("C:\\");
memset (&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Data Files (*.txt)\0*.txt\0\0";
ofn.lpstrDefExt = "txt";
ofn.lpstrFile = szFileUrl;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = pszInitial;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrTitle = "Select data files.";
if (GetOpenFileName(&ofn) )
{
if( _tcslen( szFileUrl ) > 0 )
{
_tcsncpy( pszFile, szFileUrl, MAX_PATH );
}
}
}
#include <commdlg.h>
#include <stdio.h>
#include <tchar.h>
using namespace std; //introduces namespace std
void main()
{
TCHAR *pszInitial="C:\\Windows";
TCHAR *pszFile;
UINT cch;
OPENFILENAME ofn;
CHAR szFileUrl[MAX_PATH+1];
szFileUrl[0] = '\0';
if( !pszInitial || !_tcslen( pszInitial ) )
pszInitial = _T("C:\\");
memset (&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Data Files (*.txt)\0*.txt\0\0";
ofn.lpstrDefExt = "txt";
ofn.lpstrFile = szFileUrl;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = pszInitial;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrTitle = "Select data files.";
if (GetOpenFileName(&ofn) )
{
if( _tcslen( szFileUrl ) > 0 )
{
_tcsncpy( pszFile, szFileUrl, MAX_PATH );
}
}
}
Looks fine, ...
I just studied my msdn and I assume you need a gui app. Not a console.
I will ask in the msdn newsgroups but I THINK it won't work in a console.
I just studied my msdn and I assume you need a gui app. Not a console.
I will ask in the msdn newsgroups but I THINK it won't work in a console.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
WHAT is the difference? Im only curious as to why the above fails.
the problem look as if its not even coming from the openfilename stuff its probably the fact that he created a char pointer
TCHAR *pszFile;
and the used
_tcsncpy( pszFile, szFileUrl, MAX_PATH );
he never allocated any space for the string
TCHAR *pszFile;
and the used
_tcsncpy( pszFile, szFileUrl, MAX_PATH );
he never allocated any space for the string
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think your users would be much more appreciate of a GUI over a console app any day..
But if you really really want to do it as you propose then it may be possible using pipes to pass data between the two processes (console app and GUI), but that is quite a lot of work....
:)