Win32 C++代码快速验证模板

通用工程模板

1. 输出目录:$(ProjectDir)..\output\$(ProjectName)\$(Platform)_$(Configuration)\
2. 中间目录:$(ProjectDir)..\output\$(ProjectName)\$(Platform)_$(Configuration)\int\

DLL模板

#include <windows.h>

#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>

#include <unordered_map>
#include <unordered_set>

using namespace std;

#pragma warning(disable : 4996)

// #define ENABLE_CONSOLE_PRINT 1

#ifdef ENABLE_CONSOLE_PRINT
    #define PRINT(...) printf(__VA_ARGS__)
    #define WPRINT(...) wprintf(__VA_ARGS__)
#else
    #define PRINT(...) OutputDebugStringA(__VA_ARGS__)
    #define WPRINT(...) OutputDebugStringW(__VA_ARGS__)
#endif

#ifdef _DEBUG
    #define LOG(_text, ...) PrintDebugStringA(_text "\r\n", __VA_ARGS__)
    #define LOGW(_text, ...) PrintDebugStringW(_text L"\r\n", __VA_ARGS__)
#else
    #define LOG(_text, ...)
    #define LOGW(_text, ...)
#endif

namespace
{
/*
 * 0 ProcessAttach NoCall
 * 1 ProcessAttach Called
 */
LONG volatile g_iDllMainProcessAttachFlag = 0;
} // namespace

extern "C" __declspec(dllexport) void ExportedFunction() {}

void PrintDebugStringW(LPCWSTR lpwszFormat, ...)
{
    va_list args;
    va_start(args, lpwszFormat);

    WCHAR wszBuffer[4096] = {L"myDebug:"};
    if (_vswprintf(wszBuffer + wcslen(wszBuffer), lpwszFormat, args) < 0)
    {
        WPRINT(L"myDebug: Input String is INVALID !!!\r\n");
    }

    va_end(args);
    WPRINT(wszBuffer);
}

void PrintDebugStringA(LPCSTR lpszFormat, ...)
{
    va_list args;
    va_start(args, lpszFormat);

    CHAR szBuffer[4096] = {"myDebug:"};
    if (vsprintf(szBuffer + strlen(szBuffer), lpszFormat, args) < 0)
    {
        PRINT("myDebug: Input String is INVALID !!!\r\n");
    }

    va_end(args);
    PRINT(szBuffer);
}

void LibraryHandler_ProcAttach(HANDLE hModule)
{
    LOG("Attach Begin!!");
}

void LibraryHandler_ProcDetach(HANDLE hModule)
{
    LOG("Detach Begin!!");
}

BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpvReserved)
{
#if defined(_DEBUG) && defined(ENABLE_CONSOLE_PRINT)
    static FILE *g_stream = nullptr;
#endif

    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
    {
        if (0 == InterlockedCompareExchange(&g_iDllMainProcessAttachFlag, 1, 0))
        {
            // DisableThreadLibraryCalls(hModule);

#if defined(_DEBUG) && defined(ENABLE_CONSOLE_PRINT)
            AllocConsole();
            freopen_s(&g_stream, "CONOUT$", "w+", stdout);
#endif

            auto threadObj = std::thread(
                [hModule]() -> void
                {
                    LibraryHandler_ProcAttach(hModule);
                });
            threadObj.detach();
        }
        break;
    }
    case DLL_PROCESS_DETACH:
    {
        LibraryHandler_ProcDetach(hModule);

#if defined(_DEBUG) && defined(ENABLE_CONSOLE_PRINT)
        fflush(stdout);
        fclose(g_stream);
#endif

        break;
    }
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        break;
    }

    return TRUE;
}
posted @ 2024-09-04 23:13  倚剑问天  阅读(35)  评论(0编辑  收藏  举报