CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2005
    Posts
    336

    Can not add MFC support to Windows Application

    Hi

    We can add MFC support to console apps but hhy can't we add MFC support to Windows application?

    I tried this:

    Code:
    #include <afx.h>
    #include <afxwin.h>
    #include <afxext.h> 
    
    CWinApp theApp;
    
    
    int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	DWORD dwErrorCode;
    	CString strErrorMessage;
    	if(!(AfxWinInit(hInstance, hPrevInstance, (LPTSTR)lpCmdLine, nShowCmd)))
    	{
    		dwErrorCode = GetLastError();
    		strErrorMessage.Format(L"Error is = %d", dwErrorCode);
    		AfxMessageBox(strErrorMessage);
    
    	}
    
    	HANDLE hFile = CreateFile(L"\\\\.\\C:\\a.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, 
    		CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
    	
    	CString cs;
    
    	if(hFile == INVALID_HANDLE_VALUE)
    	{
    		dwErrorCode = GetLastError();
    		strErrorMessage.Format(L"Error is = %d", dwErrorCode);
    		AfxMessageBox(strErrorMessage);
    	};
    
    	CloseHandle(hFile);
    
    	return 0;
    }
    It is compiled and executed but only CWinApp theApp; line is executed, before Winmain starts program exit. Why is it so?

    Thanks...
    Last edited by sawer; March 16th, 2009 at 08:03 AM.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Can not add MFC support to Windows Application

    Because MFC runs it's main through the CWinApp. The first function is CWinApp::InitInstance.

  3. #3
    Join Date
    Sep 2005
    Posts
    336

    Re: Can not add MFC support to Windows Application

    OK. But this works:
    Code:
    #include <afx.h>
    #include <afxwin.h>
    #include <afxext.h> 
    
    CWinApp theApp;
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    	DWORD dwErrorCode;
    	CString strErrorMessage;
    	if(!(AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)))
    	{
    		dwErrorCode = GetLastError();
    		strErrorMessage.Format(L"Error is = %d", dwErrorCode);
    		AfxMessageBox(strErrorMessage);
    
    	}
    
    	HANDLE hFile = CreateFile(L"\\\\.\\C:\\al.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, 
    		CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
    
    
    	if(hFile == INVALID_HANDLE_VALUE)
    	{
    		dwErrorCode = GetLastError();
    		strErrorMessage.Format(L"Error is = %d", dwErrorCode);
    		AfxMessageBox(strErrorMessage);
    	};
    
    	CloseHandle(hFile);
    
    	return 0;
    }
    Why WinMain() doesn't work but _tmain() works?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured