Thread: GetOpenFileName() ZeroMemory error?

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    Question GetOpenFileName() ZeroMemory error?

    hello,
    I am new to these boards but I am not new to cprogramming.com.

    anyways I am trying to add a GetOpenFileName() function to my application and compiler is giving an error for ZeroMemory(). I honestly don't know what is wrong here, error message doesn't help much, and I look at open source code that compiles fine and are doing it the same way I am. here is the error it is giving me:

    fatal error C1057: unexpected end of file in macro expansion
    I am not using any macros or preprocessor directives (other than #include of course). When I comment out the ZeroMemory() I get:

    error C2039: 'hWndOwner' : is not a member of 'tagOFNA'
    error C2360: initialization of 'szFileName1' is skipped by 'case' label
    error C2146: syntax error : missing ';' before identifier 'ofn'
    fatal error C1057: unexpected end of file in macro expansion
    here is my code:

    Code:
    HWND ADDDlg = NULL;
    
    BOOL CALLBACK ADDDlgProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    	switch(Message) {
    	case WM_INITDIALOG:
    		break;
    	case WM_COMMAND:
    		switch(wParam) {
    		case IDC_BROWSE_1:
    			OPENFILENAME ofn;
    			char szFileName1[MAX_PATH] = "";
    
    			ZeroMemory(&ofn, (sizeof(ofn));	 // error is here??? O_o
    
    			ofn.lStructSize = sizeof(ofn);
    			ofn.hWndOwner = hWnd;
    			ofn.lpstrFilter = "All Files (*.*)\0*.*\0";
    			ofn.lpstrTitle = "Browse for file path 1\0";
    			ofn.lpstrFile = szFileName1;
    			ofn.nMaxFile = MAX_PATH;
    			ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
    
    			if(GetOpenFileName(&ofn) == TRUE) {
    				SetWindowText(GetDlgItem(hWnd, IDC_PATH_1), szFileName1);
    			}
    			break;
    		case IDC_BROWSE_2:
    			char OPENFILENAME ofn;
    			char szFileName2[MAX_PATH] = "";
    
    			ZeroMemory(&ofn, (sizeof(ofn));
    
    			ofn.lStructSize = sizeof(ofn);
    			ofn.hWndOwner = hWnd;
    			ofn.lpstrFilter = "All Files (*.*)\0*.*\0";
    			ofn.lpstrTitle = "Browse for file path 2\0";
    			ofn.lpstrFile = szFileName2;
    			ofn.nMaxFile = MAX_PATH;
    			ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
    
    			if(GetOpenFileName(&ofn) == TRUE) {
    				SetWindowText(GetDlgItem(hWnd, IDC_PATH_2), szFileName2);
    			}
    			break;
    		case WM_CLOSE:
    			EndDialog(hWnd, 0);
    			break;
    		}
    	}
    	return FALSE;
    }
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_ADD_DLG), NULL, ADDDlgProc);
    }
    that is full code from below includes including main. can someone please tell me what is going wrong here? also any tips to improve code size/efficiency are always appreciated.

    thanks alot!


    -piecer

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I am not using any macros or preprocessor directives
    Actually you are. ZeroMemory() is just a macro for memset().

    Anyways, the reason for all your errors is probably due to the fact that you cannot declare variables in case labels unless you declare them in a new block like so:

    Code:
    case 5:
       {
          int i = 5;
       }
       break;

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Just to add:
    error C2039: 'hWndOwner' : is not a member of 'tagOFNA'
    This error is pretty self-explanitory. The correct member name is hwndOwner.

    Code:
    char OPENFILENAME ofn;
    What is this? Are you trying to declare a char, or an OPENFILENAME?

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Hi, welcome to the forums.

    What they said, plus you have an extra bracket in your ZeroMemory call. Delete it.
    Code:
    ZeroMemory(&ofn, (sizeof(ofn));

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What they said
    Is that your subtle way of making fun of my weight mouse?

  6. #6
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    thanks alot all for the quick replies!

    bithub: the char was there just for testing :lol: . didn't know it was still in the code I posted (wasn't meant to be, I mis-read msdn when it said to include commdlg.h, I thought it said everything in commdlg was included in windows.h with the way they have it arranged there, and kept getting "OPENFILENAME undeclared identifier").

    thanks bithub, didn't know ZeroMemory() was a macro for memset(). will declare those variables out of the switch. and also thanks anonytmouse for pointing out that un-necessary bracket.


    -piecer
    Last edited by Bleech; 12-06-2005 at 04:52 PM.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by bithub
    Is that your subtle way of making fun of my weight mouse?
    He he. Didn't realise you were responsible for both posts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM