分析IPMSG源代码

WinMain与main

WinMain的原型:

int CALLBACK WinMain(
  __in  HINSTANCE hInstance,
  __in  HINSTANCE hPrevInstance,
  __in  LPSTR lpCmdLine,
  __in  int nCmdShow
);

WinMain的定义:

WinMain is the conventional name used for the application entry point for a graphical Windows-based application.

#include <Windows.h>
int WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR cmdline, int iCmdShow)
{
    MessageBox(nullptr, cmdline, "WinMain", MB_OK | MB_ICONINFORMATION);
    return 0;
}
注意编译上述代码需要修改Configuration Properties工程配置属性/Generl通用/Character Set字符集为Not Set不指定。因为这能通知编译器在函数选择中是选用支持char或者支持unicode字符。如果需要支持unicode字符,则需要将上述代码修改成
MessageBoxA(nullptr, cmdline, "WinMain", MB_OK | MB_ICONINFORMATION)

或者查看有关字符集转换的页面






main的原型:

int main(int argc , char *argv[])

其中argc是命令行参数的个数,argv[]是命令行参数的字符数组指针。

在网上闲逛,无意中发现,原来Windows API的C语言编程,并不一定需要使用WinMain入口函数。

如果不使用WinMain的四个参数,那么直接使用main代替WinMain就完全可以了。

如果程序中使用了WinManin的某个参数,那么也可以用main替代,但是需要增加WinMain的四个参数作为变量:

    ....
   HINSTANCE hInstance;
    int iCmdShow;
    LPTSTR szCmdLine; 
    hInstance=GetModuleHandle(NULL);  //获取程序本身的实例句柄
    iCmdShow=SW_SHOWNORMAL;//定义窗口显示模式
    szCmdLine=GetCommandLine();//获取命令行字符串
    ....(hPrevInstance=NULL;这个是历史遗留问题,一般程序用不到这个参数)

不过有一点要说明的就是GetCommandLine()函数返回的命令行参数带有执行程序本身的名字,

而WinMain的参数LPSTR lpCmdLine是不包含执行程序的名字本身的。




原文地址:https://www.cnblogs.com/rogerroddick/p/2846692.html