Discussion:
File Open Dialog from Console App
(too old to reply)
Gilgamesh
2012-03-02 23:57:41 UTC
Permalink
Raw Message
All,
I have a console app I created in VC++ 2010. I've got it working.
Basically it processes a spreadsheet of data, transmogrifies the data,
then writes it out to another spreadsheet. That's all working. Now I
want to tidy things up. In particular when I open the input
spreadsheet, I'd like to be able to use a file open dialog to navigate
to the file.

I'm doing this:

//MicroSoft Office Objects
#import \
"C:\\Program Files (x86)\Common Files\Microsoft Shared
\OFFICE14\mso.dll" \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("RGB", "RBGXL")
//Microsoft VBA Objects
#import \
"C:\\Program Files (x86)\Common Files\Microsoft Shared\VBA
\VBA6\vbe6ext.olb"

//Excel Application Objects
#import "C:\\Program Files (x86)\Microsoft Office\OFFICE14\EXCEL.EXE"
\
rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("ReplaceText", "ReplaceTextXL") \
rename("CopyFile", "CopyFileXL") \
exclude("IFont", "IPicture") no_dual_interfaces

and later this:

ExcelAppPtr->Workbooks->Open(L"X:\\Awards\\2012\\InputData.xlsx");

It works fine, but I'd like to navigate to the file using a file open
dialog, rather than hardcode the file name or prompt for a string.
Prompting for a string seems error prone and not user friendly. If I
could use a FileOpenDialog then get the string for the file name to
give to the ExcelAppPtr->Workbooks->Open(L"X:\\Awards\\2012\
\InputData.xlsx"); call, the prog would be a lot easier to use.

Any help appreciated.

Mike
David Lowndes
2012-03-03 08:25:01 UTC
Permalink
Raw Message
... but I'd like to navigate to the file using a file open
dialog, rather than hardcode the file name or prompt for a string.
Mike,

Have you considered using the GetOpenFileName API?

Dave
Gilgamesh
2012-03-05 23:51:36 UTC
Permalink
Raw Message
Post by David Lowndes
... but I'd like to navigate to the file using a file open
dialog, rather than hardcode the file name or prompt for a string.
Mike,
Have you considered using the GetOpenFileName API?
Dave
Yes,
I did. I even tried to cut & paste the code example of it from MSDN
into my code. It took a while to get it to compile, then it would
not link. I do not know what to tell the console app in order to
link in the dlls that contain GetOpenFileName. If I could get
GetOpenFileName to work, that would be perfect.
I created the project as a console application.

Here are my header files:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

// Some includes of headers files I created
#include ...

// Get the MS Office dlls
#import \
"C:\\Program Files (x86)\Common Files\Microsoft Shared
\OFFICE14\mso.dll" \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("RGB", "RBGXL")
//Microsoft VBA Objects
#import \
"C:\\Program Files (x86)\Common Files\Microsoft Shared\VBA
\VBA6\vbe6ext.olb"

//Excel Application Objects
#import "C:\\Program Files (x86)\Microsoft Office\OFFICE14\EXCEL.EXE"
\
rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("ReplaceText", "ReplaceTextXL") \
rename("CopyFile", "CopyFileXL") \
exclude("IFont", "IPicture") no_dual_interfaces


Thanks,
Mike
David Lowndes
2012-03-06 00:09:18 UTC
Permalink
Raw Message
Post by Gilgamesh
Post by David Lowndes
Have you considered using the GetOpenFileName API?
Yes,
I did. I even tried to cut & paste the code example of it from MSDN
into my code. It took a while to get it to compile, then it would
not link. I do not know what to tell the console app in order to
link in the dlls that contain GetOpenFileName.
Mike,

The MSDN documentation tells you what you need - which is to link with
Comdlg32.lib.

Alternatively, add:

#pragma comment( lib, "Comdlg32.lib" )

to your source file to embed a library search record for the lib
directly.

Dave
Gilgamesh
2012-03-07 22:57:31 UTC
Permalink
Raw Message
Post by David Lowndes
Post by David Lowndes
Have you considered using the GetOpenFileName API?
Yes,
I did.  I even tried to cut & paste the code example of it from MSDN
into my code.   It took a while to get it to compile, then it would
not link.   I do not know what to tell the console app in order to
link in the dlls that contain GetOpenFileName.
Mike,
The MSDN documentation tells you what you need - which is to link with
Comdlg32.lib.
#pragma comment( lib, "Comdlg32.lib" )
to your source file to embed a library search record for the lib
directly.
Dave
Thanks.

I decided to try again. First, I created a new console application.
Then I cut & pasted the GetOpenFileName example from MSDN. It failed
to compile of course. The problems seemed to be around text strings.
I edited it and got it to compile and link. It runs just fine too,
and does what one would expect. The code below works in Visual
Studio 2010 running on 64-bit windows in a console application project
created with all settings left at default settings.

// GetFileExample.cpp : Defines the entry point for the console
application.
//


#include "stdafx.h"
#include <windows.h>
#include <Commdlg.h>

//
// Gobal Variables and declarations.
//
OPENFILENAME ofn ;



// a memory buffer to contain the file name
const long kSizeFileName = 255 ;
TCHAR szFile[kSizeFileName] ;
LPCWSTR pszFile ;

int _tmain(int argc, _TCHAR* argv[])
{
// open a file name
pszFile = szFile ;
ZeroMemory( &ofn , sizeof( ofn));
ofn.lStructSize = sizeof ( ofn );
ofn.hwndOwner = NULL ;
ofn.lpstrFile = szFile ;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = kSizeFileName ;
ofn.lpstrFilter = _TEXT("All\0*.*\0Text\0*.TXT\0");
ofn.nFilterIndex =1;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir=NULL ;
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;

GetOpenFileName( &ofn );

// Now simply display the file name
LPCWSTR kstrFileName = _TEXT("File Name") ;
MessageBox ( NULL , ofn.lpstrFile , kstrFileName , MB_OK);
return 0;
}

//////////////////////////// end of code sample

Next I'm going to move this code over to my project and see if it will
work there.

Mike
Gilgamesh
2012-03-08 00:13:48 UTC
Permalink
Raw Message
Post by Gilgamesh
Post by David Lowndes
Post by David Lowndes
Have you considered using the GetOpenFileName API?
Yes,
I did.  I even tried to cut & paste the code example of it from MSDN
into my code.   It took a while to get it to compile, then it would
not link.   I do not know what to tell the console app in order to
link in the dlls that contain GetOpenFileName.
Mike,
The MSDN documentation tells you what you need - which is to link with
Comdlg32.lib.
#pragma comment( lib, "Comdlg32.lib" )
to your source file to embed a library search record for the lib
directly.
Dave
Thanks.
I decided to try again.  First, I created a new console application.
Then I cut & pasted the GetOpenFileName example from MSDN.  It failed
to compile of course.  The problems seemed to be around text strings.
I edited it and got it to compile and link.  It runs just fine too,
and does what one would expect.   The code below works in Visual
Studio 2010 running on 64-bit windows in a console application project
created with all settings left at default settings.
// GetFileExample.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include <windows.h>
#include <Commdlg.h>
//
// Gobal Variables and declarations.
//
OPENFILENAME ofn ;
// a memory buffer to contain the file name
const long kSizeFileName = 255 ;
TCHAR szFile[kSizeFileName] ;
LPCWSTR  pszFile ;
int _tmain(int argc, _TCHAR* argv[])
{
        // open a file name
        pszFile = szFile ;
        ZeroMemory( &ofn , sizeof( ofn));
        ofn.lStructSize = sizeof ( ofn );
        ofn.hwndOwner = NULL ;
        ofn.lpstrFile = szFile ;
        ofn.lpstrFile[0] = '\0';
        ofn.nMaxFile = kSizeFileName ;
        ofn.lpstrFilter = _TEXT("All\0*.*\0Text\0*.TXT\0");
        ofn.nFilterIndex =1;
        ofn.lpstrFileTitle = NULL ;
        ofn.nMaxFileTitle = 0 ;
        ofn.lpstrInitialDir=NULL ;
        ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;
        GetOpenFileName( &ofn );
        // Now simply display the file name
        LPCWSTR kstrFileName = _TEXT("File Name") ;
        MessageBox ( NULL , ofn.lpstrFile ,  kstrFileName , MB_OK);
        return 0;
}
//////////////////////////// end of code sample
Next I'm going to move this code over to my project and see if it will
work there.
Mike
Hmm. I added the code above to my vc++ project which is set up as a
console app and inludes these header:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

I added the above code for GetOpenFileName.

To get it to compile I had to change the _TEXT() calls to TEXT()
calls. So far so good. But it won't link. It cannot find a libary
with the GetOpenFileName in it.
I looked back at my GetOpenFileName project and rebuilt it with more
verbose messages. It told me that it was linking in
C:PROGRAM FILES (x86)\MICROSFOT SDKS\WINDOWS\V7.0A\LIB\COMSLG32.LIB
but I can find no such file on my PC.

I tried to add a reference to my project to the dll c:WINDOWS
\SYSTEM32\comdlg32.dll
but I get the error

"Could not add a reference to file 'c:WINDOWS\SYSTEM32\comdlg32.dll'
because ti is neither a .NET assembly nor a egistered ActiveX
control."

So how do I add I tell my project to look in 'c:WINDOWS
\SYSTEM32\comdlg32.dll' for GetOpenFileName() ?

Mike
Gilgamesh
2012-03-08 01:17:38 UTC
Permalink
Raw Message
Post by Gilgamesh
Post by David Lowndes
Post by David Lowndes
Have you considered using the GetOpenFileName API?
Yes,
I did.  I even tried to cut & paste the code example of it from MSDN
into my code.   It took a while to get it to compile, then it would
not link.   I do not know what to tell the console app in order to
link in the dlls that contain GetOpenFileName.
Mike,
The MSDN documentation tells you what you need - which is to link with
Comdlg32.lib.
#pragma comment( lib, "Comdlg32.lib" )
to your source file to embed a library search record for the lib
directly.
Dave
Thanks.
I decided to try again.  First, I created a new console application.
Then I cut & pasted the GetOpenFileName example from MSDN.  It failed
to compile of course.  The problems seemed to be around text strings.
I edited it and got it to compile and link.  It runs just fine too,
and does what one would expect.   The code below works in Visual
Studio 2010 running on 64-bit windows in a console application project
created with all settings left at default settings.
// GetFileExample.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include <windows.h>
#include <Commdlg.h>
//
// Gobal Variables and declarations.
//
OPENFILENAME ofn ;
// a memory buffer to contain the file name
const long kSizeFileName = 255 ;
TCHAR szFile[kSizeFileName] ;
LPCWSTR  pszFile ;
int _tmain(int argc, _TCHAR* argv[])
{
        // open a file name
        pszFile = szFile ;
        ZeroMemory( &ofn , sizeof( ofn));
        ofn.lStructSize = sizeof ( ofn );
        ofn.hwndOwner = NULL ;
        ofn.lpstrFile = szFile ;
        ofn.lpstrFile[0] = '\0';
        ofn.nMaxFile = kSizeFileName ;
        ofn.lpstrFilter = _TEXT("All\0*.*\0Text\0*.TXT\0");
        ofn.nFilterIndex =1;
        ofn.lpstrFileTitle = NULL ;
        ofn.nMaxFileTitle = 0 ;
        ofn.lpstrInitialDir=NULL ;
        ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;
        GetOpenFileName( &ofn );
        // Now simply display the file name
        LPCWSTR kstrFileName = _TEXT("File Name") ;
        MessageBox ( NULL , ofn.lpstrFile ,  kstrFileName , MB_OK);
        return 0;
}
//////////////////////////// end of code sample
Next I'm going to move this code over to my project and see if it will
work there.
Mike
Hmm.   I added the code above to my vc++ project which is set up as a
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
I added the above code for GetOpenFileName.
To get it to compile I had to change the _TEXT() calls to TEXT()
calls.  So far so good.  But it won't link.   It cannot find a libary
with the GetOpenFileName in it.
I looked back at my GetOpenFileName project and rebuilt it with more
verbose messages.  It told me that it was linking in
C:PROGRAM FILES (x86)\MICROSFOT SDKS\WINDOWS\V7.0A\LIB\COMSLG32.LIB
but I can find no such file on my PC.
I tried to add a reference to my project to the dll c:WINDOWS
\SYSTEM32\comdlg32.dll
but I get the error
"Could not add a reference to file 'c:WINDOWS\SYSTEM32\comdlg32.dll'
because ti is neither a .NET assembly nor a egistered ActiveX
control."
So how do I add I tell my project to look in 'c:WINDOWS
\SYSTEM32\comdlg32.dll' for GetOpenFileName() ?
Mike
Hmm. The file "C:\\Program Files (x86)\Microsoft SDKs\Windows\v7.0A
\Lib\ComDlg32.lib"
is there.
Reading the vc++ documentation it states that all I have to do is
include the appropriate .h file and ensure the ComDlg32.lib nd
ComDlg32.dll are installed. They are.
So I cannot understand why one console app, where all I do is include
the .h and everything takes care of itself, works fine, whereas the
other does not work.
Doing this:
#pragma comment(lib, "C:\\Program Files (x86)\Microsoft SDKs\Windows
\v7.0A\Lib\ComDlg32.lib" )
or this:
#import "C:\\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib
\ComDlg32.lib"
in the source code both fail.

Any help appreciated.
David Lowndes
2012-03-08 08:30:44 UTC
Permalink
Raw Message
Post by Gilgamesh
Hmm. The file "C:\\Program Files (x86)\Microsoft SDKs\Windows\v7.0A
\Lib\ComDlg32.lib"
is there.
Reading the vc++ documentation it states that all I have to do is
include the appropriate .h file and ensure the ComDlg32.lib nd
ComDlg32.dll are installed. They are.
You have to add the lib to the link command - or add the #pragma to
your source as I indicated earlier.

Dave
Gilgamesh
2012-03-08 18:05:25 UTC
Permalink
Raw Message
Post by David Lowndes
Hmm.  The file "C:\\Program Files (x86)\Microsoft SDKs\Windows\v7.0A
\Lib\ComDlg32.lib"
is there.
Reading the vc++ documentation it states that all I have to do is
include the appropriate .h file and ensure the ComDlg32.lib nd
ComDlg32.dll are installed.  They are.
You have to add the lib to the link command - or add the #pragma to
your source as I indicated earlier.
Dave
Oops. That works. I tried it earlier and it failed - I must have
misspelled something. This line works as you suggested:
#pragma comment(lib, "ComDlg32.lib" )


Thank you so much.
Mike

Loading...