1. 程式人生 > >windows api 之複製檔案到指定目錄和獲得系統資訊

windows api 之複製檔案到指定目錄和獲得系統資訊


#include <iostream>
#include <WINDOWS.H>
using namespace std;

void copySelf()
{
	char szSelfName[MAX_PATH] ;
	char szDestName[MAX_PATH] ;
	char szSystemPath[MAX_PATH] ;
	char szWindowPath[MAX_PATH] ;
	GetModuleFileName(NULL,szSelfName,MAX_PATH);
	cout<<szSelfName<<endl;
	GetSystemDirectory(szSystemPath,MAX_PATH);
	cout<<szSystemPath<<endl;
	GetWindowsDirectory(szWindowPath,MAX_PATH);
	cout<<szWindowPath<<endl;
	strcpy(szDestName,"Z:\\sougou");
	strcat(szDestName,"\\va_test.exe");
	if(CopyFile(szSelfName,szDestName,FALSE))
	{
		cout<<"copy !"<<endl;
	}
}

void GetSysInfo()
{
	char szComputerName[MAXBYTE];
	char szUserName[MAXBYTE];
	OSVERSIONINFO OsVer;
	OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);//不指定大小得到的是隨機數
	GetVersionEx(&OsVer);
	//cout<<OsVer.dwMajorVersion<<" "<<OsVer.dwMinorVersion<<" "<<OsVer.dwOSVersionInfoSize<<endl;
	if(OsVer.dwPlatformId = VER_PLATFORM_WIN32_NT)
	{
		if(OsVer.dwMajorVersion == 5 && OsVer.dwMinorVersion == 1)
			cout<<"Windows XP  "<<OsVer.szCSDVersion<<endl;
		else if(OsVer.dwMajorVersion == 5 && OsVer.dwMinorVersion == 0)
			cout<<"Windows 2K "<<endl;
		else if(OsVer.dwMajorVersion == 6 && OsVer.dwMinorVersion == 1)
			cout<<"Windows 7  "<<OsVer.szCSDVersion<<endl;
		else
			cout<<"Other System "<<endl;
	}
	unsigned long nSize = MAXBYTE;
	GetComputerName(szComputerName,&nSize);
	cout<<"Computer Name is "<<szComputerName<<endl;
	GetUserName(szUserName,&nSize);
	cout<<"User name " <<szUserName<<endl;
}

int main()
{
	copySelf();
	GetSysInfo();
	return 0;
}