Global Variables

I am writing a program in Win32 API and I have some variable problems so I thought I better post in here. I think it is a basic thing but I just can't think how I would fix it. You probably want to see some code.

 
ofn.lpstrFileTitle = szFileName;


I want this variable to be global so I can set it here

 
char upd_txt[1000] = "";


Any help would be much appreciated.
I think I'm misunderstanding this because it seems too easy, just declare ofn as a global variable, or declare szFileName as a global variable and an LPTSTR data type with the value of "whatever" then when the code hits the point where it declares ofn.lpstrFileTitle set that equal to szFileName. Are you getting an error? Am I way off the mark?
Yeah but how do I declare ofn as a global variable.

Is it
 
char ofn;


I got confused when I read some stuff on another website so I came here.
No, if I understand correctly you are looking to use the OPENFILENAME Struct in the Win32 API, in which case the data type is OPENFILENAME.

http://msdn.microsoft.com/en-us/library/ms646839(VS.85).aspx

This should help.

So: OPENFILENAME ofn; I'm about %90 sure that's right but I'm just getting comfortable with the API myself.
Just realised how stupid I was. Thats what I get for coding half asleep whilst listening to music and doing homework.

I was trying to get the variable so I could write it to a file. I have already tried GetDlgItemText but because the Edit Box was getting text from the Open File Dialog for some reason it refused to write with that. I ran a test and intercepted the variable that had the GetDlgItemText and displayed it in a message box and it recieved it ok. It would only write though if I typed it in myself in the Edit Control. Here is my code and Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

//The include header file
#include "resource.h"

BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{

    HWND cboGame;
    
    char szFileName[MAX_PATH] = "";

    const char *Game[] = { "Death Match", "Team Death Match", "Capture The Flag", "Assault" };

	switch(Message)
	{
		case WM_INITDIALOG:
			// Default values
			SetDlgItemText(hwnd, IDC_TEXT, "Press the browse button and choose your map file...");
			SetDlgItemText(hwnd, IDC_TEXT1, "Type the full name of your map here...");
			                                            
            cboGame = GetDlgItem(hwnd, IDC_COMBO);
            

            for(int Count = 0; Count < 4; Count++)
            {
                SendMessage(cboGame, CB_ADDSTRING, 0, (LPARAM) Game[Count]);

            }
            return TRUE;
		break;
		
		case WM_COMMAND:
             switch(LOWORD(wParam))
			{
                case IDC_ADD:
                     {
                        OPENFILENAME ofn;
   
                        ZeroMemory(&ofn, sizeof(ofn));
   
                        ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
                        ofn.hwndOwner = hwnd;
                        ofn.lpstrFilter = "Map Files (*.ctm)\0*.ctm\0All Files (*.*)\0*.*\0";
                        ofn.nMaxFile = MAX_PATH;
                        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
                        ofn.lpstrDefExt = "ctm";
                        ofn.lpstrFileTitle = szFileName;
                        ofn.nMaxFileTitle = 100;
                                              
                        if(GetOpenFileName(&ofn))
                        {
                            SetDlgItemText(hwnd, IDC_TEXT, szFileName);
                        }
                     }
                     break;
                case IDC_ABOUT:
                     {
                            int ret = DialogBox(GetModuleHandle(NULL), 
                            MAKEINTRESOURCE(IDD_ABOUT), hwnd, DlgProc);
						       
					        if(ret == -1)
                            {
						           MessageBox(hwnd, "Error, Dialog failed to initialise!", "Error",
					           	MB_OK | MB_ICONINFORMATION);
                            }
                     }
                break;
                
                case IDC_UPDATE:
                     {                                                        
                            if (MessageBox(hwnd, "Are you happy with your settings?", "Attention!", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
                            {                                            
                               HANDLE hFile = CreateFile("xmaplist.int", GENERIC_READ|GENERIC_WRITE,
                               0,0,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

                               if( hFile != INVALID_HANDLE_VALUE)
                               {
                               char text[] = "Map+=(FileName=\"";
                               char upd_txt[1000] = szFileName;
                               
                               DWORD dwBytesWritten = 0;
                               SetFilePointer(hFile, 0, 0, FILE_END);
                               WriteFile(hFile, text, strlen(text), &dwBytesWritten, 0);
                               WriteFile(hFile, upd_txt, strlen(upd_txt), &dwBytesWritten, 0);
                               CloseHandle(hFile);
                               
                               MessageBox(hwnd, "Your map list was updated successfully!", "Success!", MB_OK | MB_ICONEXCLAMATION); 
                               }                      
                            }
                            else
                            {
                            }
                     }
                     break;
            }
        break;
        case WM_CLOSE:
			EndDialog(hwnd, 0);
		break;
		default:
			return FALSE;
	}
	return TRUE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}
Topic archived. No new replies allowed.