⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writever.cpp

📁 DtWinVer is a C++ class which provides a comprehensive method to determine which OS the program that
💻 CPP
字号:
#include <windows.h>
#include <stdio.h>
#include "Dtwinver.h"


//The CWriteVerCommandLineInfo class aids in parsing the 
//command line at application startup. The 
//structure is styled upon the MFC class CCommandLineInfo
class CWriteVerCommandLineInfo
{
public:
// Constructors / Destructors
	CWriteVerCommandLineInfo();

//Methods
	virtual void ParseParam(LPCTSTR pszParam, BOOL bFlag, BOOL bLast);

//Member variables
  char m_pszFilename[_MAX_PATH];
};


CWriteVerCommandLineInfo::CWriteVerCommandLineInfo()
{
  strcpy(m_pszFilename, "");
}

void CWriteVerCommandLineInfo::ParseParam(LPCTSTR pszParam, BOOL bFlag, BOOL /*bLast*/)
{
	if (!bFlag)
	{
		strcpy(m_pszFilename, pszParam);
	}
}

//Based upon the function of the same name in CWinApp
void ParseCommandLine(CWriteVerCommandLineInfo& rCmdInfo)
{
	for (int i = 1; i < __argc; i++)
	{
		LPCTSTR pszParam = __argv[i];
		BOOL bFlag = FALSE;
		BOOL bLast = ((i + 1) == __argc);
		if (pszParam[0] == '-' || pszParam[0] == '/')
		{
			// remove flag specifier
			bFlag = TRUE;
			++pszParam;
		}
		rCmdInfo.ParseParam(pszParam, bFlag, bLast);
	}
}

int main()
{  
  //Parse the command line             
  CWriteVerCommandLineInfo CmdInfo;
  ParseCommandLine(CmdInfo);

  //Assume the best
  DWORD dwErrorCode = 0;

  if (strlen(CmdInfo.m_pszFilename))
  {
    //The line which will be written to disk
    char szWriteBuf[1024];

    //Get the OS details
    COSVersion::OS_VERSION_INFO osvi;
    memset(&osvi, 0, sizeof(osvi));
    COSVersion os;  
    if (os.GetVersion(&osvi))
      wsprintf(szWriteBuf, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%s", osvi.dwUnderlyingMajorVersion, osvi.dwUnderlyingMinorVersion, 
               osvi.dwUnderlyingBuildNumber, osvi.UnderlyingPlatform, osvi.wUnderlyingServicePackMajor, osvi.wUnderlyingServicePackMinor, 
               osvi.dwSuiteMask, osvi.OSType, osvi.UnderlyingProcessorType, osvi.szUnderlyingCSDVersion);

    //Open the file we want to write to
    HANDLE hFile = CreateFile(CmdInfo.m_pszFilename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if (hFile == INVALID_HANDLE_VALUE)
    {
      //handle the error
      return GetLastError();
    }

    //And write the OS version info to the designated file  
    DWORD dwBytesWritten = 0;
    if (!WriteFile(hFile, szWriteBuf, (DWORD) strlen(szWriteBuf), &dwBytesWritten, 0))
    {
      //handle the error
      CloseHandle(hFile);
      return GetLastError();
    }

    //Close the file now that we are finished with it
    CloseHandle(hFile);
  }
  else
    dwErrorCode = ERROR_FILE_NOT_FOUND;

  return dwErrorCode;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -