Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 :

/////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: Add your control notification handler code here
	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); // 16Bit Windows Needs This
	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;
	// For testing purpose only I am using the hardcoded path.... 
	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 ....
Posted
Updated 13-Apr-11 23:43pm
v2
Comments
Richard MacCutchan 14-Apr-11 6:05am    
A sledgehammer to crack a nut!
ShilpiP 14-Apr-11 6:22am    
Do you have any solution?? My requirement is that I have to embed an Executable in my resource file and than execute it. Its an R&D for me ...
If you have than please suggest me :)
Richard MacCutchan 14-Apr-11 7:29am    
You already said that you have the solution, so was that not correct? I doubt that there is a real solution to what you are trying to do since it is a rather futile exercise. Why embed an executable file as a resource in another application just so you can run it? Just run the original application normally and dispense with all this unnecessary wrapping.
ShilpiP 14-Apr-11 7:52am    
Yes I understand :)
Reason: There is one VB.net executable that user want to run on Windows 7 and later version but if it is executed on XP than it crashes because to execute this application some framework is to be present in system. In Windows 7 it is present by default but in XP it is not.One solution is we can create a vc++ application that first check that is OS is Windows 7 or not. If it is not than just display a MessageBox and exit, else execute that executable. It is a worst case scenario but if we do not find any solution than we can do by this.

1 solution

I have never tried this but the Windows LoadLibrary()[^] function may be able to assist.
 
Share this answer
 
Comments
ShilpiP 14-Apr-11 5:46am    
Hi Richard,

Thanks for your answer. My problem is solved now and I am updating my question also.
Thanks again...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900