1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
#include <windows.h> #include "mditest.h" HWND hWndClient = NULL; HINSTANCE hInst; // current instance LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LPCTSTR lpszAppName = L"MyMDIApp"; LPCTSTR lpszChild = L"MDIChild"; LPCTSTR lpszTitle = L"MDI Test Application"; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hWnd; WNDCLASSEX wc; // Register the main application window class. //............................................ wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon( hInstance, lpszAppName ); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1); wc.lpszMenuName = lpszAppName; wc.lpszClassName = lpszAppName; wc.cbSize = sizeof( WNDCLASSEX ); //wc.hIconSm = LoadImage( hInstance, lpszAppName, // IMAGE_ICON, 16, 16, // LR_DEFAULTCOLOR ); if ( !RegisterClassEx( &wc ) ) return( FALSE ); // Register the window class for the MDI child windows. //..................................................... wc.lpfnWndProc = (WNDPROC)ChildWndProc; wc.hIcon = LoadIcon( hInstance, lpszChild ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = lpszChild; wc.cbSize = sizeof( WNDCLASSEX ); //wc.hIconSm = LoadImage( hInstance, lpszChild, // IMAGE_ICON, 16, 16, // LR_DEFAULTCOLOR ); if ( !RegisterClassEx( &wc ) ) return( FALSE ); hInst = hInstance; // Create the main application window. //.................................... hWnd = CreateWindow( lpszAppName, lpszTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL ); if ( !hWnd ) return( FALSE ); ShowWindow( hWnd, nCmdShow ); UpdateWindow( hWnd ); while( GetMessage( &msg, NULL, 0, 0) ) { if ( hWndClient && TranslateMDISysAccel( hWndClient, &msg ) ) continue; TranslateMessage( &msg ); DispatchMessage( &msg ); } return( msg.wParam ); } // An ID that is different than all menu ids. //........................................... #define ID_CHILDWINDOW 1000 LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch( uMsg ) { case WM_CREATE : { CLIENTCREATESTRUCT ccs; // Assign the 'Window' menu. //.......................... ccs.hWindowMenu = GetSubMenu( GetMenu( hWnd ), 1 ); ccs.idFirstChild = ID_CHILDWINDOW; // Create the client window. //.......................... hWndClient = CreateWindowEx( WS_EX_CLIENTEDGE, L"MDICLIENT", NULL, WS_CHILD | WS_CLIPCHILDREN, 0, 0, 0, 0, hWnd, (HMENU)0xCA0, hInst, &ccs); ShowWindow( hWndClient, SW_SHOW ); } break; case WM_SIZE : // Size the client window to the size of the client // area of the main application window. //................................................. MoveWindow( hWndClient, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE ); break; case WM_COMMAND : switch( LOWORD( wParam ) ) { case IDM_NEW : { HWND hWndChild; // Create a new child window. //........................... hWndChild = CreateMDIWindow( (LPTSTR)lpszChild, L"Document", 0L, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWndClient, hInst, 0L); ShowWindow( hWndChild, SW_SHOW ); } break; case IDM_CLOSE : { HWND hActiveWnd; // Close the active child window. //............................... hActiveWnd = (HWND)SendMessage( hWndClient, WM_MDIGETACTIVE, 0, 0 ); if ( hActiveWnd ) SendMessage( hWndClient, WM_MDIDESTROY, (WPARAM)hActiveWnd, 0 ); } break; case IDM_CASCADE : CascadeWindows( hWndClient, MDITILE_SKIPDISABLED, NULL, 0, NULL ); break; case IDM_TILEHORZ : TileWindows( hWndClient, MDITILE_HORIZONTAL, NULL, 0, NULL ); break; case IDM_TILEVERT : TileWindows( hWndClient, MDITILE_VERTICAL, NULL, 0, NULL ); break; case IDM_ARRANGE : ArrangeIconicWindows( hWndClient ); break; case IDM_ABOUT : DialogBox( hInst, L"AboutBox", hWnd, (DLGPROC)About ); break; case IDM_EXIT : DestroyWindow( hWnd ); break; default : return( DefFrameProc( hWnd, hWndClient, uMsg, wParam, lParam ) ); } break; case WM_DESTROY : PostQuitMessage(0); break; default : return( DefFrameProc( hWnd, hWndClient, uMsg, wParam, lParam ) ); } return( 0L ); } LRESULT CALLBACK ChildWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { //switch( uMsg ) //{ // default : // return( DefMDIChildProc( hWnd, uMsg, wParam, lParam ) ); //} return( 0L ); } LRESULT CALLBACK About( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return (TRUE); case WM_COMMAND: if ( LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, TRUE); return (TRUE); } break; } return (FALSE); } |
Thanks everyone, I finally found the real culprit, it’s a typo, I use LPSTR lpCmdLine
instead of LPTSTR lpCmdLine
. The real mystery is why it compiled at all under VC6 — it did use wWinMain
, but somehow it was OK for lpCmdLine to be char *
instead of WCHAR *
.
Now I changed it to:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
And it works under VS2008 too.
Edit: I successfully compiled and even ran the program with this function definition under VC6:
int APIENTRY wWinMain(int *hInstance, float hPrevInstance, int *lpCmdLine, float nCmdShow)
{
MessageBox(0,L"Running.",0,0);
return 0;
}
Interestingly, replacing float nCmdShow
to double nCmdShow
does give a linker error, I assume because float is 32-bits but double is not.
Permalink
Cannot retrieve contributors at this time
description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid |
---|---|---|---|---|---|
Learn more about: Compiler Error C2731 |
Compiler Error C2731 |
11/04/2016 |
C2731 |
C2731 |
9b563999-febd-4582-9147-f355083c091e |
Compiler Error C2731
‘identifier’ : function cannot be overloaded
The functions main
, WinMain
, DllMain
, and LibMain
cannot be overloaded.
The following sample generates C2731:
// C2731.cpp extern "C" void WinMain(int, char *, char *); void WinMain(int, short, char *, char*); // C2731
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
#include <windows.h> #include "mditest.h" HWND hWndClient = NULL; HINSTANCE hInst; // current instance LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LPCTSTR lpszAppName = L"MyMDIApp"; LPCTSTR lpszChild = L"MDIChild"; LPCTSTR lpszTitle = L"MDI Test Application"; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hWnd; WNDCLASSEX wc; // Register the main application window class. //............................................ wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon( hInstance, lpszAppName ); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1); wc.lpszMenuName = lpszAppName; wc.lpszClassName = lpszAppName; wc.cbSize = sizeof( WNDCLASSEX ); //wc.hIconSm = LoadImage( hInstance, lpszAppName, // IMAGE_ICON, 16, 16, // LR_DEFAULTCOLOR ); if ( !RegisterClassEx( &wc ) ) return( FALSE ); // Register the window class for the MDI child windows. //..................................................... wc.lpfnWndProc = (WNDPROC)ChildWndProc; wc.hIcon = LoadIcon( hInstance, lpszChild ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = lpszChild; wc.cbSize = sizeof( WNDCLASSEX ); //wc.hIconSm = LoadImage( hInstance, lpszChild, // IMAGE_ICON, 16, 16, // LR_DEFAULTCOLOR ); if ( !RegisterClassEx( &wc ) ) return( FALSE ); hInst = hInstance; // Create the main application window. //.................................... hWnd = CreateWindow( lpszAppName, lpszTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL ); if ( !hWnd ) return( FALSE ); ShowWindow( hWnd, nCmdShow ); UpdateWindow( hWnd ); while( GetMessage( &msg, NULL, 0, 0) ) { if ( hWndClient && TranslateMDISysAccel( hWndClient, &msg ) ) continue; TranslateMessage( &msg ); DispatchMessage( &msg ); } return( msg.wParam ); } // An ID that is different than all menu ids. //........................................... #define ID_CHILDWINDOW 1000 LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch( uMsg ) { case WM_CREATE : { CLIENTCREATESTRUCT ccs; // Assign the 'Window' menu. //.......................... ccs.hWindowMenu = GetSubMenu( GetMenu( hWnd ), 1 ); ccs.idFirstChild = ID_CHILDWINDOW; // Create the client window. //.......................... hWndClient = CreateWindowEx( WS_EX_CLIENTEDGE, L"MDICLIENT", NULL, WS_CHILD | WS_CLIPCHILDREN, 0, 0, 0, 0, hWnd, (HMENU)0xCA0, hInst, &ccs); ShowWindow( hWndClient, SW_SHOW ); } break; case WM_SIZE : // Size the client window to the size of the client // area of the main application window. //................................................. MoveWindow( hWndClient, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE ); break; case WM_COMMAND : switch( LOWORD( wParam ) ) { case IDM_NEW : { HWND hWndChild; // Create a new child window. //........................... hWndChild = CreateMDIWindow( (LPTSTR)lpszChild, L"Document", 0L, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWndClient, hInst, 0L); ShowWindow( hWndChild, SW_SHOW ); } break; case IDM_CLOSE : { HWND hActiveWnd; // Close the active child window. //............................... hActiveWnd = (HWND)SendMessage( hWndClient, WM_MDIGETACTIVE, 0, 0 ); if ( hActiveWnd ) SendMessage( hWndClient, WM_MDIDESTROY, (WPARAM)hActiveWnd, 0 ); } break; case IDM_CASCADE : CascadeWindows( hWndClient, MDITILE_SKIPDISABLED, NULL, 0, NULL ); break; case IDM_TILEHORZ : TileWindows( hWndClient, MDITILE_HORIZONTAL, NULL, 0, NULL ); break; case IDM_TILEVERT : TileWindows( hWndClient, MDITILE_VERTICAL, NULL, 0, NULL ); break; case IDM_ARRANGE : ArrangeIconicWindows( hWndClient ); break; case IDM_ABOUT : DialogBox( hInst, L"AboutBox", hWnd, (DLGPROC)About ); break; case IDM_EXIT : DestroyWindow( hWnd ); break; default : return( DefFrameProc( hWnd, hWndClient, uMsg, wParam, lParam ) ); } break; case WM_DESTROY : PostQuitMessage(0); break; default : return( DefFrameProc( hWnd, hWndClient, uMsg, wParam, lParam ) ); } return( 0L ); } LRESULT CALLBACK ChildWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { //switch( uMsg ) //{ // default : // return( DefMDIChildProc( hWnd, uMsg, wParam, lParam ) ); //} return( 0L ); } LRESULT CALLBACK About( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return (TRUE); case WM_COMMAND: if ( LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, TRUE); return (TRUE); } break; } return (FALSE); } |
// Include the Windows header file that’s needed for all Windows applications
#include <windows.h>
#include <string.h>
#include <string>
#include <tchar.h>
#include <stdlib.h>
#include <list>
#include <iostream>
HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
// make class name into a global variable
TCHAR szClassName[ ] = _T(«WindowsApp»);
// forward declarations
bool initWindow( HINSTANCE hInstance );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow )
{
// Initialize the window
if ( !initWindow( hInstance ) )
return false;
// main message loop:
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message!=WM_QUIT )
{
// Check the message queue
while (GetMessage(&msg, wndHandle, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
return (int) msg.wParam;
}
// bool initWindow( HINSTANCE hInstance )
//initWindow registers the window class for the application, creates the window
bool initWindow( HINSTANCE hInstance )
{
WNDCLASSEX wcex;
// Fill in the WNDCLASSEX structure. This describes how the window
// will look to the system
wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);// the default cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
wcex.lpszMenuName = NULL; // the resource name for the menu
wcex.lpszClassName = _T(«WindowsApp»); // the class name being created
wcex.hIconSm = 0; // the handle to the small icon
if (!RegisterClassEx(&wcex));
return 0;
// Create the window
wndHandle = CreateWindowEx(
0,
szClassName,
_T(«Windows App»),
WS_OVERLAPPEDWINDOW,
// the window class to use
// the title bar text
// the window style
CW_USEDEFAULT, // the starting x coordinate
CW_USEDEFAULT, // the starting y coordinate
640, // the pixel width of the window
480, // the pixel height of the window
NULL, // the parent window; NULL for desktop
NULL, // the menu for the application; NULL for
// none
hInstance, // the handle to the application instance
NULL); // no values passed to the window
// Make sure that the window handle that is created is valid
if (!wndHandle)
return false;
// Display the window on the screen
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}
Я обновил старый проект с VC6 до VS2008, и теперь я получаю эту ошибку компиляции:
error C2731: 'wWinMain' : function cannot be overloaded
В этих строках кода:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
Тот же проект прекрасно компилируется под VC6.
4
Решение
Спасибо всем, я наконец нашел настоящего виновника, это опечатка, я использую LPSTR lpCmdLine
вместо LPTSTR lpCmdLine
, Настоящая загадка заключается в том, почему он вообще скомпилирован под VC6 — он использовал wWinMain
, но каким-то образом это было нормально для lpCmdLine, чтобы быть char *
вместо WCHAR *
,
Теперь я изменил это на:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
И это работает под VS2008 тоже.
Редактировать: Я успешно скомпилировал и даже запустил программу с этим определением функции под VC6:
int APIENTRY wWinMain(int *hInstance, float hPrevInstance, int *lpCmdLine, float nCmdShow)
{
MessageBox(0,L"Running.",0,0);
return 0;
}
Интересно, что замена float nCmdShow
в double nCmdShow
я даю ошибку компоновщика, я полагаю, потому что float 32-битный, а double — нет.
9
Другие решения
У меня была такая же ошибка с Консольное приложение Win32. Исправление было:
- открыто проект > Свойства …,
- расширять Свойства конфигурации > Linker > система
- Задавать SubSystem в Не установлен
- Нажмите Хорошо
0
// Include the Windows header file that’s needed for all Windows applications
#include <windows.h>
#include <string.h>
#include <string>
#include <tchar.h>
#include <stdlib.h>
#include <list>
#include <iostream>
HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
// make class name into a global variable
TCHAR szClassName[ ] = _T(«WindowsApp»);
// forward declarations
bool initWindow( HINSTANCE hInstance );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow )
{
// Initialize the window
if ( !initWindow( hInstance ) )
return false;
// main message loop:
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message!=WM_QUIT )
{
// Check the message queue
while (GetMessage(&msg, wndHandle, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
return (int) msg.wParam;
}
// bool initWindow( HINSTANCE hInstance )
//initWindow registers the window class for the application, creates the window
bool initWindow( HINSTANCE hInstance )
{
WNDCLASSEX wcex;
// Fill in the WNDCLASSEX structure. This describes how the window
// will look to the system
wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);// the default cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
wcex.lpszMenuName = NULL; // the resource name for the menu
wcex.lpszClassName = _T(«WindowsApp»); // the class name being created
wcex.hIconSm = 0; // the handle to the small icon
if (!RegisterClassEx(&wcex));
return 0;
// Create the window
wndHandle = CreateWindowEx(
0,
szClassName,
_T(«Windows App»),
WS_OVERLAPPEDWINDOW,
// the window class to use
// the title bar text
// the window style
CW_USEDEFAULT, // the starting x coordinate
CW_USEDEFAULT, // the starting y coordinate
640, // the pixel width of the window
480, // the pixel height of the window
NULL, // the parent window; NULL for desktop
NULL, // the menu for the application; NULL for
// none
hInstance, // the handle to the application instance
NULL); // no values passed to the window
// Make sure that the window handle that is created is valid
if (!wndHandle)
return false;
// Display the window on the screen
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}