Thread: Weird optimalisation error (with code)

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    76

    Weird optimalisation error (with code)

    I've written some code to get the filename of the executable from a window handle. This code works perfect, but when i turn on optimalisation on in the compiler my program crashes. I cant debug this problem since it only happens when optimalisation is turned on.
    The reason why i think the problem is GetModuleFileNameExDLL, is because the program doesnt get past this line.

    I am using mingw in combination with code::blocks.

    Code:
    
    void GetFilenamFromWindowHandle(HWND hwnd,char* fileName,int sizeFileName)
    {
        typedef DWORD (*GETMODULEFILENAMEEX) (HANDLE hProcess,HMODULE hModule,LPTSTR lpFilename,DWORD nSize);
        typedef BOOL (*ENUMPROCESSMODULES) (HANDLE hProcess,HMODULE* lphModule,DWORD cb,LPDWORD lpcbNeeded);
        if(isWinNT)
        {
            HINSTANCE psapiLib = LoadLibrary("PSAPI");
            if(!psapiLib)
            {
                Msg("Missing or invallid psapi.dll, this program will now close.");
                ExitProcess(0);
                return;
            }
            GETMODULEFILENAMEEX GetModuleFileNameExDLL = (GETMODULEFILENAMEEX) GetProcAddress(psapiLib, "GetModuleFileNameExA");
            ENUMPROCESSMODULES EnumProcessModulesDLL = (ENUMPROCESSMODULES) GetProcAddress(psapiLib, "EnumProcessModules");
            if(GetModuleFileNameExDLL == 0 || EnumProcessModulesDLL == 0)
            {
                Msg("Missing or invallid psapi.dll, this program will now close.");
                ExitProcess(0);
                return;
            }
            HANDLE hMainProc;
            DWORD procId=0;
            GetWindowThreadProcessId(hwnd,&procId);
            hMainProc = OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,false, procId );
            if(!hMainProc)
                return;
            HMODULE mod ;
            DWORD needed;
            EnumProcessModulesDLL(hMainProc,&mod,sizeof(mod),&needed); 
            GetModuleFileNameExDLL(hMainProc,mod,fileName,sizeFileName);  //something goes wrong at this call (i think)  
            CloseHandle(hMainProc);
        }
        else
        {
            GetWindowModuleFileName(hwnd,fileName,sizeFileName);
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's pretty certain that where it falls over and where the problem is are two different things.

    Changing the build type usually means that you have some kind of uninitialised variable (possibly a pointer) or buffer overrun problem somewhere in the code.

    I mean, if you try this function in isolation (with just enough of a main() to call it), does it still crash, or does it work? If it works, then the problem is somewhere else in your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    I have replaced the GetModuleFileNameExDLL with the "real" GetModuleFileNameEx and now it does work when using optimalisation. I just dont get it. I am importing this function from a dll so my program can be run under win98. But somehow my imported functions doesnt work as good as the "real" one. And this problem only shows up when using optimalisation

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Your function pointers don't have the right calling convention.
    Code:
        typedef DWORD (WINAPI *GETMODULEFILENAMEEX)(HANDLE, HMODULE, LPTSTR, DWORD);
        typedef BOOL (WINAPI *ENUMPROCESSMODULES)(HANDLE, HMODULE*, DWORD, LPDWORD);
    gg

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    Thanks, now it works perfectly . But i dont know exactly why it now works. I found on google it has to do with calling conventions (__stdcall) , like you mentioned, but im still unsure what i am actualy doing with the typedef. I always copy it and modify it to suit my needs, but i never really understanded what this kind of typedef really does. The usual examples of typedef are really simple, but i cant find any info that explains something as "complicated" like the one i use. Could you give a link/info on this kind of typedefs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 01:31 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. weird code output
    By noob2c in forum C++ Programming
    Replies: 9
    Last Post: 08-03-2003, 07:34 AM
  4. weird code which compiles
    By pinko_liberal in forum C Programming
    Replies: 0
    Last Post: 07-06-2002, 12:52 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM