wWinMain解析传入参数

 项目中使用的代码,做个记录,方便以后查阅

// TestWin32.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "TestWin32.h"

#include <iostream>

#include <shellapi.h>

#define MAX_LOADSTRING 100

// 全局变量:
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDI_TESTWIN32, szWindowClass, MAX_LOADSTRING);

    //展示命令窗口
    AllocConsole();
    freopen("CONIN$", "r", stdin);
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);

    //解析命令参数
    LPWSTR* szArgList;
    int argCount;
    szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
    if (szArgList != nullptr)
    {
        for (int i = 0; i < argCount; i++)
        {
            LPCWSTR lpcwStr = szArgList[i];
            int num = WideCharToMultiByte(CP_OEMCP, NULL, lpcwStr, -1, NULL, 0, NULL, FALSE);
            char* pchar = new char[num];
            WideCharToMultiByte(CP_OEMCP, NULL, lpcwStr, -1, pchar, num, NULL, FALSE);
            std::cout << "parm:" << pchar << std::endl;
            delete pchar;
        }
        LocalFree(szArgList);
    }


    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDI_TESTWIN32));
    MSG msg;
    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int)msg.wParam;
}

猜你喜欢

转载自blog.csdn.net/auccy/article/details/119999711