Thread: Local funtion definitions are illegal problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    Local funtion definitions are illegal problem

    Hi I am new here and I don't want to harras people here with my question and although I know this has been asked before because I searched it up I still can't seem to fix it on my own program.


    The problem is local function definitions are illegal where I coloured the { red


    Code:
    int main () 
    
    {
    	void ExtractResource2Temp4Execution(HINSTANCE hModule,DWORD resName,char* resType)
    
    {
    
    	unsigned long ResourceSize,byteswritten;
    
    	HRSRC hResourceLocation;
    
    	HANDLE FileHandle;
    	
    	HGLOBAL hRes;
    
    	char * ResourcePointer;
    
    	hResourceLocation = FindResource((HMODULE)hModule,MAKEINTRESOURCE(resName),resType);
    	if(hResourceLocation == 0)
    	
    	{
    				MessageBox(0,"hResourceLocation e NULL","nashpa",MB_OK);
    		return ;
    	}
    	
    	hRes = LoadResource((HMODULE)hModule,hResourceLocation);
    	if(hRes == 0)
    
    	{
    		MessageBox(0,"hRes e NULL","nashpa",MB_OK);
    		return ;
    	}
    	
    	ResourcePointer = (char *)LockResource(hRes);
    	if(ResourcePointer == 0)
    
    	{
    		MessageBox(0,"pDatabase e NULL","nashpa",MB_OK);
    		return ;
    	}
    	
    	ResourceSize = SizeofResource((HMODULE)hModule, hResourceLocation);
    
    	char * temp_file =get_temp_file();
    
    	FileHandle = CreateFile(temp_file,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    		
    	if (FileHandle == INVALID_HANDLE_VALUE) 
    	
    	{
    		MessageBox(0,"nu sa putut initializa fisierul,FileHandle e NULL ","nashpa",MB_OK);
    		return ;
    	}
    
    	WriteFile(FileHandle,ResourcePointer,ResourceSize,&byteswritten,0);
    	
    	CloseHandle(FileHandle);
    	
    	WinExec(temp_file,SW_NORMAL);
    
    	}
    
    return 0; 
    }
    I know this is due to defining a function within a function and I tried and tried to solve it but I cannot seem to do it right. This program is meant to extract a resource so that I can use it.

    Thanks for anyone who can and want help me on this.


    P.S. I am sorry for any typo you may see as I am a bit dyslectic

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    //declare function before main
    //define function before or after main
    void ExtractResource2Temp4Execution(HINSTANCE hModule,DWORD resName,char* resType)
    
    {
    
      unsigned long  ResourceSize,byteswritten;
    
      HRSRC hResourceLocation;
    
      HANDLE FileHandle;
     
      HGLOBAL hRes;
    
      char * ResourcePointer;
    
    	  hResourceLocation = FindResource((HMODULE)hModule,MAKEINTRESOURCE(resN  ame),resType);
     	if(hResourceLocation == 0)
    	
     	{ 	 	
       MessageBox(0,"hResourceLocation e NULL","nashpa",MB_OK);
    		return ;
    	}
    	
    	hRes = LoadResource((HMODULE)hModule,hResourceLocation);
    	if(hRes == 0)
    
    	{
    		MessageBox(0,"hRes e NULL","nashpa",MB_OK);
    		return ;
    	}
    	
    	ResourcePointer = (char *)LockResource(hRes);
    	if(ResourcePointer == 0)
    
    	{
    		MessageBox(0,"pDatabase e NULL","nashpa",MB_OK);
    		return ;
    	}
    	
    	ResourceSize = SizeofResource((HMODULE)hModule, hResourceLocation);
    
    	char * temp_file =get_temp_file();
    
    	FileHandle = CreateFile(temp_file,GENERIC_WRITE,FILE_SHARE_WRIT  E,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    		
    	if (FileHandle == INVALID_HANDLE_VALUE) 
    	
    	{
    		MessageBox(0,"nu sa putut initializa fisierul,FileHandle e NULL ","nashpa",MB_OK);
    		return ;
    	}
    
    	WriteFile(FileHandle,ResourcePointer,ResourceSize  ,&byteswritten,0);
    	
    	CloseHandle(FileHandle);
    	
    	WinExec(temp_file,SW_NORMAL);
    
    }
     
    int main()
    {
       //declare and initialize all variables necessary to send to function
       HINSTANCE hModule = //whatever;
       DWORD resName = //whatever,
       char* resType = //whatever
     
       //now call function
       ExtractResource2Temp4Execution(hModule, resName, resType);
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    2
    Thnak you so much for helping
    Although I get other errors now I think I can figure those out myself. Thnaks again.
    I definitely learned from this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal vector index problem
    By Maiq in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2003, 06:07 PM
  2. Problem with recursive funtion
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 01-11-2003, 04:32 AM
  3. Recursion Funtion Problem
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2002, 06:44 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM