Thread: Why is this crashing on my face?

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Why is this crashing on my face?

    Code:
    void Save(HWND hwnd)
    {
    	
    	//open the Save As prompt, which is built in in windows (set it up here)
    	OPENFILENAME ofn;
        char szFileName[MAX_PATH] = "";
    
        ZeroMemory(&ofn, sizeof(ofn));
    
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = hwnd;
        ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
        ofn.lpstrFile = szFileName;
        ofn.nMaxFile = MAX_PATH;
        ofn.Flags = OFN_EXPLORER |OFN_OVERWRITEPROMPT;
        ofn.lpstrDefExt = "txt";
        if(GetSaveFileName(&ofn)) //Open The Save As Prompt
        {
    
    	 HWND DialogBoxH;
    	
    	
    	//Get the info on the Edit Box
    
    	 int len = GetWindowTextLength(DialogBoxH=GetDlgItem(hwnd, IDC_EDIT2));//Use GetDlgItem(hwnd, IDC_TEXT)
    															  //to get a handle to the 
    															  //Edit Box
         if(len > 0)//if it's more than zero, meaning it has characters
         {
            
            char* buf;
    
            buf = (char*)GlobalAlloc(GPTR, len + 1);//we add one here for the null 
            GetDlgItemText(hwnd, IDC_EDIT2, buf, len + 1);//get the text from the edit box
    		memcpy(Cliente.ClientName,buf,strlen (buf)); //copy the text from buf to the struct
    		
    		MessageBox (DialogBoxH, Cliente.ClientName,"Something", MB_OK);
    		
            //... do stuff with text ...
    
            GlobalFree((HANDLE)buf);
    	 }
    
    	 LPCTSTR FileName;
    
    	 HANDLE FileSave=NULL;
    	 BOOL Success;
    	 FileSave=CreateFile(FileName, GENERIC_WRITE, 0, NULL,  CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    	 
    	  DWORD dwWritten;
    
    	 if (FileSave!= INVALID_HANDLE_VALUE)
    	 {
    		 WriteFile(FileSave, &Cliente, sizeof Cliente,&dwWritten, NULL); 
    	 }
    
    
    
    
    	
            // Do something usefull with the filename stored in szFileName 
        } 
    }
    Last edited by incognito; 04-22-2003 at 09:55 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Use your debugger to find the line it crashes on.

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Originally posted by golfinguy4
    Use your debugger to find the line it crashes on.

    ::whispers::: read my post on the GD forums.........


    I don't have to use a debugger, though I will read a tutorial I found on it.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Originally posted by elchulo2002
    ::whispers::: read my post on the GD forums.........


    I don't have to use a debugger, though I will read a tutorial I found on it.
    Yeah I know.... that's why I said it. You should learn. A debugger is a very helpful tool. What compiler do you have? Most come with debuggers.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    VC++.......
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I had that problem too at one time...then I read some obscure MS doc which stated that lpstrFile[0] must be initialized to zero ELSE SOMETHING UNDEFINED may happen! Strangely enough, that was it (for me anyway). Give it a go.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Learn Debugging NOW!!!!!!!!!!!!!!!

    It would take me hours to find that problem if I looked at the code. I could isolate it in minutes with the debugger.

    VC++ debugger is very good.

    Make sure you are building a debug version of your app.

    Get the cursor on the line

    ZeroMemory(&ofn, sizeof(ofn));

    press F9. This will place a break point on that line. A red dot should appear.

    Run the app.
    Move thru it until you can get to call this problem function. When you do the complier will open on the line with the break point.

    Make sure you can see the watch window in VC++ v6 is under View->Debug Window->Watch.

    You can drag variables into this window to see their value or just hover the mouse over them.

    Use F10 to step a single line of code.
    Continue until the crash occurs.



    PS
    I think your problem is the filter
    Try
    "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0"

    >>obscure MS doc which stated that lpstrFile[0] must be initialized to zero

    I set it to the name I suggest the user save as and have no problems.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Whohoo!!!!!!!!

    I caught the problem with my handy debugger, we have a new found friendship now. Hehehehe!!!

    I was passing FileName to createfile which didn't contain anything. I caught that with our handy debugger....!!!!

    look

    Code:
    	 LPCTSTR FileName;
    
    	 HANDLE FileSave=NULL;
    	 BOOL Success;
    	 FileSave=CreateFile(FileName, GENERIC_WRITE, 0, NULL,  CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    Thanks guys.
    Last edited by incognito; 04-23-2003 at 08:54 AM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange windows STL crashing problem
    By kdmiller in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2010, 02:25 PM
  2. std::map::find() crashing
    By noobcpp in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2008, 11:12 PM
  3. Program crashing, Need help!
    By Obsidian_179 in forum C Programming
    Replies: 3
    Last Post: 06-19-2008, 05:26 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Compiler Crashing... WHY?
    By kippwinger in forum C++ Programming
    Replies: 6
    Last Post: 07-03-2003, 12:10 AM