|
-
March 16th, 2009, 08:00 AM
#1
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.
-
March 16th, 2009, 08:05 AM
#2
Re: Can not add MFC support to Windows Application
Because MFC runs it's main through the CWinApp. The first function is CWinApp::InitInstance.
-
March 16th, 2009, 08:22 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|