/* Création d'un lien symbolique (Harlink) sous Windows 2000
   N'est possible qu'avec des partitions NTFS
*/
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "winbase.h"
#include <string.h>
#include <stdio.h>

//--------------------------------------------------------------------	
void oemprintf(char* mess) {
	CharToOem(mess,mess);
	printf(mess);
	}

void syntaxe() {
	oemprintf("Hardlink - JCB © 2000\n");
	oemprintf("=====================\n");
	oemprintf("Syntaxe :\n");
	oemprintf("---------\n");
	oemprintf("hardlink <chemin_reel> <nom_du_lien>\n\n");
	oemprintf("   <chemin_reel> : le nom du fichier ou dossier\n");
	oemprintf("                   pour lequel on veut créer un lien\n");
	oemprintf("   <nom_du_lien> : l'emplacement du lien\n");
	}
//--------------------------------------------------------------------
int main(int argc, char* argv[])
	{
	char nomreel[256],nomlien[256], mess[256];
	LPVOID lpmess;
	DWORD errcode; 
	if (argc<=1) {
		syntaxe();		
		return 1;
		}
	strcpy(nomreel,CharLower(argv[1]));
	strcpy(nomlien,CharLower(argv[2]));
/*
	Set security descriptor argument to NULL to leave 
	file's security descriptor alone.
*/ 
	if (!CreateHardLink(nomlien,nomreel,NULL)) { //  erreur
		errcode=GetLastError();
		FormatMessage( 
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | 
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			errcode,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
			(LPTSTR) &lpmess,
			0,
			NULL 
			);
		strcpy(mess,(LPCTSTR)lpmess);
		oemprintf(mess);
		printf("(code %u)",errcode);
		return errcode;
		}
	oemprintf("Lien créé avec succès\n");
	return 0;
	}
