VS C++ common functions

get current time

#include <Winbase.h> 或者 #include<Windows.h>

void WINAPI GetLocalTime(
  __out LPSYSTEMTIME lpSystemTime
);

typedef struct _SYSTEMTIME {  
	WORD wYear; /* Valid values ​​are 1601 to 30827 */
	WORD wMonth;  		/* 1(January),2(February),3(March),4(April),5(May),6(June),7(July),8(August),9(September),10(October),11(November),12(December) */
	WORD wDayOfWeek; 	/* 0(Sunday), 1(Monday), 2(Tuesday), 3(Wednesday), 4(Thursday), 5(Friday), 6(Saturday)*/
	WORD wDay; /* valid values ​​from 1 to 31 */
	WORD wHour; /* Valid values ​​are 0 to 23 */
	WORD wMinute; /* Valid values ​​are 0 to 59 */
	WORD wSecond; /* Valid values ​​are 0 to 59 */
	WORD wMilliseconds; /* Valid values ​​are 0 to 999 */
} SYSTEMTIME,  *PSYSTEMTIME;

routine

#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
	SYSTEMTIME sysTime;
	GetLocalTime(&sysTime);
	printf("%d/%d/%d/%d - %d:%d:%d:%d\n", sysTime.wYear, sysTime.wMonth, sysTime.wDay
			, sysTime.wDayOfWeek, sysTime.wHour, sysTime.wMinute, sysTime.wSecond
			, sysTime.wMilliseconds);
	getchar();
	return 0;
}

Atomic operation, the variable is incremented by 1

#include<Winbase.h> 或 #include <Windows.h>

LONG __cdecl InterlockedIncrement(
  __in_out LONG volatile* Addend
);

routine

#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{

	long inc = 0;
	InterlockedIncrement(&inc);
	printf("inc = %d\n", inc);

	getchar();
	return 0;
}

Mutex

#include <Winbase.h> 或者 #include <Windows.h>
HANDLE WINAPI CreateMutex(
  __in LPSECURITY_ATTRIBUTES lpMutexAttributes,	// 属性
  __in BOOL bInitialOwner, // true the creator of the mutex is the owner, false the creator of the mutex does not own
  __in LPCTSTR lpName // mutex name
);
return value:
	On success, returns a handle to the mutex
	on failure, return NULL
	Mutex's lpName name exists GetLastError returns ERROR_ALREADY_EXISTS

routine

#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
	HANDLE hMutex = NULL;
	hMutex = CreateMutex(NULL, FALSE, (LPCTSTR)"TestMutex");
	if ( GetLastError() == ERROR_ALREADY_EXISTS )
	{
		printf("TestMutex already exist\n");
		CloseHandle(hMutex);
		hMutex = NULL;
	}
	 

	CloseHandle(hMutex);
	getchar();
	return 0;
}

View the current running process path (excluding the file name)

DWORD WINAPI GetCurrentDirectory(
  __in DWORD nBufferLength, // size of lpBuffer
  __out LPTSTR lpBuffer // return path content
);
return value
	On success, returns the number of bytes written to lpBuffer
	on failure, return 0
View the current running process path (including file name)
DWORD WINAPI GetModuleFileName(
  __in HMODULE hModule, // NULL, get the path of the current process
  __out LPTSTR lpFilename, // return pathname
  __in DWORD nSize // size of lpFilename
);
return value
	On success, returns the number of bytes written to lpFilename
	on failure, return 0

routine

#include <windows.h>
#include <stdio.h>
 
int main(int argc, char* argv[])
{
	char szCurPath[128] ;
   GetCurrentDirectory(sizeof(szCurPath),  szCurPath);
   printf("%s\n", szCurPath);
   
   GetModuleFileName(NULL,szCurPath, sizeof(szCurPath));
   printf("%s\n", szCurPath);

  getchar();
  return 0;
}
Note: If it does not print out, modify

Additional character skewer

char *strcat( char *strDestination, const char *strSource );
return value
	Return the concatenated string, no return value for errors
routine
#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
	char szBuf[128] = "hello";
	//strcat(szBuf, " world!");
	printf("%s\n", strcat(szBuf, " world!"));
	printf("%s\n", szBuf);

	getchar();
	return 0;
}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324400126&siteId=291194637