RtlCopyMemory crash MFC application

Discussion in 'C' started by ankitkshah, Apr 23, 2011.

  1. ankitkshah

    ankitkshah New Member

    Joined:
    Mar 21, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,

    I have some code as following.

    Code:
    BOOL qw = ExtractResource(AfxGetResourceHandle(), IDR_DLL1, _T("C:\\VBDLL.dll") );
    
    bool ExtractResource(const HINSTANCE hInstance, WORD resourceID, LPCTSTR szOutputFilename)
    {
    	/*LPTSTR sArgv = argv[1];
    	 LPTSTR sArgv2 = argv[2];*/
    
    	TCHAR  sResName[5]  = _T("#101");
    	TCHAR sRestype[4] = _T("DLL");
    
    HRSRC hres = FindResource(AfxGetResourceHandle(), sResName,sRestype);
            
           if (hres == 0)
           {
               MessageBox(NULL, L"ERROR FIND RESOURCES SUCCESS.", L"MYProject", MB_OK);
          _tcprintf(_T("An Error Occurred.\n Could Not Locate Resource File."));
            return 1;
            }
    
        HGLOBAL    hbytes = LoadResource(NULL, hres);
      
        LPVOID pdata = LockResource(hbytes);
      
        DWORD dwSize = SizeofResource(hInstance, hres); 
    
    
           
        HANDLE hFile = CreateFile(szOutputFilename, GENERIC_READ |  GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
     
    
        /// INSERT DATA IN FILE
        HANDLE hFilemap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwSize, NULL);  
     
        LPVOID lpBaseAddress = MapViewOfFile(hFilemap, FILE_MAP_WRITE, 0, 0, 0);    
       
        MessageBox(NULL, L"1", L"MYProject", MB_OK);
        try
        {
            MessageBox(NULL, L"2", L"MYProject", MB_OK);
        RtlCopyMemory(lpBaseAddress,pdata,dwSize);
        }
        catch ( ... )
        {
            MessageBox(NULL, L"error in rtl.", L"MYProject", MB_OK);
        }
    
        UnmapViewOfFile(lpBaseAddress);
      
        CloseHandle(hFilemap);          
       
        CloseHandle(hFile);  
    
    This code works fine in the CONSOLE Application. Dll copied to local drive. But when same code I am using in MFC application then It does not work. It crash from line

    "RtlCopyMemory(lpBaseAddress,pdata,dwSize);"

    Because of this line my application crashed. I make dll from this code. And I use this dll as customAction in setup. Please guid me. What should I do? Why RtlCopyMemory does not work in MFC and why It work in Console application? Please help me....

    Thanks

    Ankit
     
    Last edited by a moderator: Apr 23, 2011
  2. ankitkshah

    ankitkshah New Member

    Joined:
    Mar 21, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi ,

    I resolve it my self when I pass hInstace in Loadresources.

    HGLOBAL hbytes = LoadResource(NULL, hres); so it works when I pass

    HGLOBAL hbytes = LoadResource(hInstance, hres);

    Thanks

    Ankit
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice