Hi All,
Good morning :),
Now my problem is solved, I am sending code for those who are facing the same problem.. :)
I am creating one MFC dialog based application and on click OK button I want to execute the other exe that is embedded in My resource file. Its a requirement.
What i do is:
I added other exe as a custom resource:
Than on OK button, I load that resource and get the handle of that resource.
Now I am stuck how to execute that executable using handle...
My code is :
HRSRC hSource = NULL;
int nSize = 0;
HGLOBAL hGlobal = NULL;
LPVOID lpVoid = NULL;
hSource = FindResource(NULL, MAKEINTRESOURCE(IDR_EXE2), _T("EXE"));
if(hSource == NULL)
{
HWND hWnd = m_hWnd;
MessageBoxEx(hWnd, _T("FindResource() Failed\t"), _T("Error Message"), MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return;
}
hGlobal = LoadResource(NULL, hSource);
if(hGlobal == NULL)
{
HWND hWnd = m_hWnd;
MessageBoxEx(hWnd, _T("LoadResource() Failed\t"), _T("Error Message"), MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return;
}
lpVoid = LockResource(hGlobal);
if(lpVoid == NULL)
{
HWND hWnd = m_hWnd;
MessageBoxEx(hWnd, _T("LockResource() Failed\t"), _T("Error Message"), MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return;
}
nSize = (UINT)SizeofResource(NULL, hSource);
SaveExecutableAndExecute((BYTE*)hGlobal, nSize);
UnlockResource(hGlobal);
FreeResource(hGlobal);
void CEmbedExeAsResourceDlg::SaveExecutableAndExecute(BYTE *pBuffer, int nSize)
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
if(hGlobal == NULL)
{
HWND hWnd = m_hWnd;
MessageBoxEx(hWnd, _T("Can not allocate enough memory\t"), _T("Error"), MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return;
}
void* pData = GlobalLock(hGlobal);
memcpy(pData, pBuffer, nSize);
GlobalUnlock(hGlobal);
CStdioFile m_OutPutFile;
m_OutPutFile.Open(_T("c:\\sample.exe"),CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
m_OutPutFile.Write(pData,nSize+1);
m_OutPutFile.Close();
GlobalFree(hGlobal);
}
Thanks in advance ....