Hello, I have a very strange issue. I am new to Win32 programming, therefore I am probably doing something wrong.
I wrote a function which reads text from a file selected by the user, then puts that text inside an Edit control. The function works perfectly, however, once I declare a new variable ( int i; ), the function "GetOpenFileName" behaves strangely and always returns 0, despite the fact that "int i" is declared AFTER GetOpenFileName is called.
Here is the short "lazy" version of my code, all lines are ommited except for the important ones (the full version is below this one):
Code:
void OpenTextFile(char * title, HWND targetEditControl)
{
HANDLE fileHandle;
OPENFILENAME fileStruct;
char str[512];
/*Structure is initlized here*/
if (GetOpenFileName(&fileStruct)==0)
{
MessageBox(NULL, "Failed to open file.", title,MB_OK | MB_ICONASTERISK); //this is triggered when I uncomment "int i" below.
}
else
{
//int i; //Once this is uncommented, GetOpenFileName will return 0.
/*Additional code here*/
}
return;
}
Full code:
Code:
void OpenTextFile(char * title, HWND targetEditControl)
{
HANDLE fileHandle;
OPENFILENAME fileStruct;
char str[512];
str[0]=NULL;
fileStruct.lStructSize=sizeof(OPENFILENAME);
fileStruct.lpstrTitle=title;
fileStruct.lpstrFile=str;
fileStruct.nMaxFile=512;
fileStruct.lpstrFilter="Text files ( .txt )\0*.txt\0All files\0*\0"; //selection filters
fileStruct.hInstance=NULL;
fileStruct.lpstrCustomFilter=NULL;
fileStruct.nMaxCustFilter=NULL;
fileStruct.nFilterIndex=NULL;
fileStruct.lpstrFileTitle=NULL;
fileStruct.nMaxFileTitle=NULL;
fileStruct.lpstrInitialDir=NULL;
fileStruct.hwndOwner=NULL;
if (GetOpenFileName(&fileStruct)==0)
{
MessageBox(NULL, "Failed to open file.", title,MB_OK | MB_ICONASTERISK); //this is triggered when I uncomment "int i" below.
}
else
{
char path[1024];
//int i; //Once this is uncommented, GetOpenFileName will return 0, no matter what.
HANDLE hFile;
DWORD wmWritten;
char strVal[1024];
ConvertSlashes(fileStruct.lpstrFile,path);
hFile = CreateFile(path,GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
ReadFile(hFile,strVal,1024,&wmWritten,NULL);
CloseHandle(hFile);
SendToEditBox(targetEditControl,strVal);
}
return;
}
I tried omitting different combinations of lines from the "else" section, the results were strange:
- If I omit all code, except for "int i", or ("int i" and "char path"), the problem does NOT exist. GetOpenFileName does NOT return 0, style 2 appears (see image below)
- As I mentioned above, If only "int i" is omitted, the code works. the problem does NOT exist. GetOpenFileName does NOT return 0, style 2 appears (see image below)
- If I omit all code, except for the variable declarations, the problem DOES exist. (no selection window appears, 0 is returned)
- If I omit all code, except for (i,hFile) OR (i,wmWritten), The selection dialog shows up, but in a different style. (see image below)
Additional info:
I am using Windows XP, SP 2
Code::Blocks IDE V10.05, MINGW compiler.
Note: I had to link an additional in order to use the GetOpenFileName function, the following libraries are linked with my project:
- libgdi32.a (gdi32)
- libuser32.a (user32)
- libkernel32.a (kernel32)
- libcomdlg32.a
The first 3 libraries are linked by default when "Win32 template" is selected in the IDE, the last one was manually linked by me(through the IDE) in order to use "GetOpenFileName".
I also have a side question, are there names for the 2 styles in the image above?
__
Thanks in advance.