I'm building an XLL addin for excel with VC++ 2010.In my addin, I need to open one txt file to read information.I've been using Windows API to display the OpenFileDialog and get the full path of the txt file to open.I compile and run my XLL looks OK, but if I put my txt file in C: \ (eg,
C: \ Ck.txt) may be open (there are some files still will not open ...) but put in another drive (D: \, E :: \ ...) or in a subdirectory is completelydoes not open.Please help me!
(my system is Windows 7 64bit)
My code is as follows:
OPENFILENAME ofn; / / variables to manage your open dialog
szFile char [260]; / / buffer to contain the file name
HWND hWnd = 0, / / parent window
HANDLE hf; / / handle of file
/ / Initialize values
ZeroMemory (& ofn, sizeof (ofn)), / / first variable to 0 for all
/ / Below is the argument must be passed before you open the dialog box open
ofn.lStructSize = sizeof (ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
ofn.lpstrFile [0] = '\ 0';
ofn.nMaxFile = sizeof (szFile);
/ / This line to decide what you want the file in the dialog, here it is. TXT
ofn.lpstrFilter = "All \ 0 *. * \ 0Text \ 0 *. TXT \ 0Keyfile \ 0 *. keys \ 0 ';
ofn.nFilterIndex = 2;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
/ / Initialize finished, so display dialog
if (GetOpenFileName (& ofn) == TRUE)
{Hf = CreateFile (ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
}
/ / Use the file
/ / Handle file path "szFile"
int i;
Tenfile char [260];
for (i = 0; i <260; i)
{If ((int) (szFile [i]) == 92) {Tenfile [i] = (char) (47);}
else {Tenfile [i] = szFile [i];}
}
xlw :: XlfServices.Commands.Alert (Tenfile);
/ / Open the file to read
ifstream ifs;
ifs.open (Tenfile, ios :: in);
if (ifs.is_open ())
{
/ / Read the file contents
const int SIZE = 500; / / length of string
char buffer [SIZE];
ifs.getline (buffer, SIZE);
xlw :: XlfServices.Commands.Alert (buffer);
}
else
{
xlw :: XlfServices.Commands.Alert ("Can not open file");
return 0;
}
/ / Close the file
ifs.close ();