Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   RtlCopyMemory crash MFC application (http://www.go4expert.com/forums/rtlcopymemory-crash-mfc-application-t25598/)

ankitkshah 23Apr2011 14:11

RtlCopyMemory crash MFC application
 
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

ankitkshah 29Apr2011 12:43

Re: RtlCopyMemory crash MFC application
 
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


All times are GMT +5.5. The time now is 05:11.