Visual C++

Moderators: Lundin
Number of threads: 379
Number of posts: 695

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
RtlCopyMemory crash MFC application Posted by ankitkshah on 23 Apr 2011 at 1:27 AM
Hi all,

I have some code as following.

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
Report
Re: RtlCopyMemory crash MFC application Posted by ankitkshah on 29 Apr 2011 at 12:10 AM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.