Making Windows Header File by .exe

Things to mention in this post:
* Windows.h Library
* Making Header File using windows features

In this post, we are writing c code that makes header file which includes windows username and date for our next projects.

About windows Library

Windows.h is a Windows-specific header file for the C and C++ programming languages which contains declarations for all of the functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the various functions and subsystems. Thanks to these features, we can achieve the specific informations such as USERNAME , DATE .

Let’s Code

First of all, Libraries are the most important part of these section. Except the standard C libraries Windows and Lmcons are working togethor so we need to add both of them, at the same area.

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include <windows.h>
#include <Lmcons.h>

After that, we need a struct that can involve the needed information.


DWORD unl = 1025;

typedef struct{
	unsigned char dirName[256];
	unsigned char fileName[256];
 	char userName[1024];
}date;

Let’s get into the main ;

In the first lines of main code, we see that our top of header format buffered into headerStart and headerFinish pointers. Also, time information handles via ‘tm’ struct which is supported by an time.h library.


int main(int argc, char **argv) {
   
   printf("Date File generated");
   
   time_t t = time(0);
   struct tm *p = localtime(&t);
   date variables = {0};
   
   char *headerStart = "#ifndef __DATE_H\n#define __DATE_H";
   char *headerFinish = "#endif  /* __DATE_H */ ";

In these lines, this API’S are supported us by windows.h library , thanks to that we can handled directory, file name and username also the information we can get by windows.h library is not limited with just these APIS.

   // Project  file path
   GetCurrentDirectoryA(256,variables.dirName);
   // Project .exe file path
   GetModuleFileName(NULL,variables.fileName,256);
   // Windows User Name
   GetUserName(variables.userName, &unl);

The last part of the main includes the file creation and writing into it. To Sum up, we complete the header format and content with these lines.


	FILE *dosya;
	
	
	dosya = fopen("date.h","w");
		
			if(dosya == NULL)
	{
		printf("File creation is failed ");
		exit(EXIT_FAILURE);
	}
	

	fprintf(dosya,"\n%s\n\n\n",headerStart );

	fprintf(dosya,"\n#define USERNAME \"%s\" \n",&variables.userName );
	
	fprintf(dosya,"\n#define COMPILE_DAY    %d"  ,p->tm_mday );
	fprintf(dosya,"\n#define COMPILE_MONTH  %d"	,p->tm_mon+1 );
	fprintf(dosya,"\n#define COMPILE_YEAR   %d\n",p->tm_year+1900 );

	fprintf(dosya,"\n#define HOUR    %d", p->tm_hour );
	fprintf(dosya,"\n#define MINUTE  %d", p->tm_min );
	fprintf(dosya,"\n#define SECOND  %d", p->tm_sec );

	fprintf(dosya,"\n\n\n%s\n",headerFinish );

	
	fclose(dosya);
 
 
return 0;
}

After writing that code in the Dev C/C++, you will get the .exe file in the project directory.

You can check the GitHub Link,

Click Here For GitHub

Leave a Reply

Your email address will not be published.