首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请教 main()和winMain()在底层是什么区别

2012-04-04 
请问 main()和winMain()在底层是什么区别能否在windows程序里用main写窗口程序为什么?谢谢 [解决办法]连接

请问 main()和winMain()在底层是什么区别
能否在windows程序里用main写窗口程序   为什么?
谢谢


[解决办法]
连接参数/subsystem:windows生成gui程序,系统以WinMainCRTStartup寻找winmain为入口执行此程序,/subsystem:console生成cui程序,系统以mainCRTStartup寻找main为入口函数,以下是用tdump显示的一个pe格式执行文件头的一部分:
Turbo Dump Version 5.0.16.6 Copyright (c) 1988, 1999 Inprise Corporation
Display of File 001.EXE

Old Executable Header

DOS File Size A00h ( 2560. )
Load Image Size 450h ( 1104. )
Relocation Table entry count 0000h ( 0. )
Relocation Table address 0040h ( 64. )
Size of header record (in paragraphs) 0004h ( 4. )
Minimum Memory Requirement (in paragraphs) 0000h ( 0. )
Maximum Memory Requirement (in paragraphs) FFFFh ( 65535. )
File load checksum 0000h ( 0. )
Overlay Number 0000h ( 0. )

Initial Stack Segment (SS:SP) 0000:00B8
Program Entry Point (CS:IP) 0000:0000


Portable Executable (PE) File

Header base: 000000B0

CPU type 80386
Flags 10F [ fixed executable backwards 32bit ]
DLL flags 0000 [ ]
Linker Version 5.C
Time stamp 3C1EFDB3 : Tue Dec 18 16:26:27 2001
O/S Version 4.0
User Version 0.0
Subsystem Version 4.0
Subsystem 0002 [ Windows GUI ] ;注意这个
Object count 00000003
Symbols offset 00000000

subsystem项表征了这个执行文件是什么子系统的,也就说明了这个程序是以哪个函数为入口的,当然了,gui程序可以向终端写stdout,cui的也一样可以出窗口,如果没用过sdk,没自己写过makefile,这个一般是不大清楚,不过清楚了好象也没什么用
[解决办法]
控制台和windows之间?
参看孙鑫视频教程第一个代码:

#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;


wndcls.lpszClassName= "Weixin2003 ";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);

HWND hwnd;
hwnd=CreateWindow( "Weixin2003 ", "北京维新科学技术培训中心 ",WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar, "char is %d ",wParam);
MessageBox(hwnd,szChar, "weixin ",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "mouse clicked ", "weixin ",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50, "计算机编程语言培训 ",strlen( "计算机编程语言培训 "));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0, "维新培训 ",strlen( "维新培训 "));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd, "是否真的结束? ", "weixin ",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
[解决办法]
两个不同的入口函数,
能否在windows程序里用main写窗口程序 为什么?
不行吧,
windows程序有一个WinMain()入口函数够用了吧,设计WinMain()这个函数应该是为了与dos、
console程序区别

[解决办法]
在VC.net的CLR项目里可以
比如这个向导生成的代码:
int main(array <System::String ^> ^args)
{
// 在创建任何控件之前启用 Windows XP 可视化效果
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// 创建主窗口并运行它
Application::Run(gcnew Form1());
return 0;
}

热点排行