RtlCopyMemory crash MFC application

ankitkshah's Avatar, Join Date: Mar 2011
Newbie Member
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 shabbir; 23Apr2011 at 14:50.. Reason: Code blocks
0
ankitkshah's Avatar, Join Date: Mar 2011
Newbie Member
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