CryptAcquireContext and CryptReleaseContext example

CryptAcquireContext and CryptReleaseContext example

#include <windows.h> 
#include <string> 
#include <winbase.h> 
#include <iostream> 
using namespace std;
#include <Wincrypt.h >
  
                                      
void main()
{
LPCSTR rgwchKeyContName = "Test123456";  
HCRYPTPROV m_hCryptoProviderFB;
BOOL ret;
BOOL ret2;

ret=CryptAcquireContext(&m_hCryptoProviderFB, rgwchKeyContName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_SILENT);
	
if (!ret && GetLastError() == NTE_BAD_KEYSET)

{
	
	printf("\nUnable to open Keyset.CryptAcquireContext failed with error: 0x%X . \nWe will try creating key",GetLastError());

	ret2=CryptAcquireContext(&m_hCryptoProviderFB, rgwchKeyContName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_SILENT);
		if (!ret2)
		{
		printf("\nCryptAcquireContext failed creating key.Error: 0x%X",GetLastError());
		}
		else
		{
		printf("\nKey created");
		}
	exit;
}


else if (!ret && GetLastError() == NTE_BAD_KEYSET)
{
printf("CryptAcquireContext failed with error: 0x%X",GetLastError());
}

else
{

	printf("CryptAcquireContext opened key. Return value is 0x%X.",ret);
}


	if (CryptReleaseContext(m_hCryptoProviderFB,0))
	{
	//printf("\nHandle has been released.\n");
	}
	else
	{
	printf("\nHandle could not be released.\n");
	}

}

Advertisement

Criticalsection example

Criticalsection and CreateThread example (EnterCriticalSection LeaveCriticalSection)

#include <windows.h> 
#include <string> 
#include <iostream> 
#include <process.h>    /* _beginthread, _endthread */
long a=0;
long b=0;
int Threadcount=64;
int s=Threadcount;
CRITICAL_SECTION  gcs; 
void Submain(void *x)
{
	for (int L=0;L<1000;L++) 
		{

			a=a++;
			EnterCriticalSection(&gcs);
						b=b++;
			LeaveCriticalSection(&gcs);
		}

/*
    s=s-1;  //Simple synchronization technique. May be useful if you like to increase the thread count WaitForMultipleObjects support value defined for MAXIMUM_WAIT_OBJECTS 64
	if(s==0)
	{
		d=TRUE;
	}
*/
_endthread();
}

 
void main()

{

HANDLE *hThreads;
hThreads = new HANDLE[Threadcount] ;
InitializeCriticalSection(&gcs);
for (int i=0;i<Threadcount;i++)
{
hThreads[i]=	CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE  )Submain,  NULL,  0,  NULL);
 	
		if (hThreads[i]==NULL)
		{
			printf("\nThread creation failed for thread %d with error %d",i,GetLastError());
		}

}

DWORD rw=WaitForMultipleObjects(Threadcount,hThreads,true,INFINITE);
DeleteCriticalSection(&gcs);
//while(!d); //Simple synchronization technique 

printf("Value of a is:%d\n" ,a);
printf("Value of b is:%d\n" ,b);
system("pause");
}

CreateProcess example

CreateProcess example

#include <windows.h> 
#include <string> 
#include <winbase.h> 
#include <iostream> 
using namespace std;

void main()
{
	int N=5;	
	cout<<"Enter count for process:";
	cin>>N;

	PROCESS_INFORMATION *x;
	STARTUPINFO *startup_info;

	startup_info = new STARTUPINFO[N];
	x =new  PROCESS_INFORMATION[N]; 
    HANDLE *h;
	h = new HANDLE[N];
	for (int i=0;i<N;i++)
		{
			memset((char *)&startup_info[i], 0, sizeof(STARTUPINFO));
			startup_info[i].cb = sizeof(STARTUPINFO);
			startup_info[i].dwFlags = STARTF_USESTDHANDLES;
			startup_info[i].hStdInput = GetStdHandle(STD_INPUT_HANDLE);
			printf("\nProcess creation starting:%d",i);
			CreateProcess("c:\\windows\\notepad.exe",NULL,NULL,NULL,FALSE,0x00010000,NULL,NULL,startup_info,&x[i]);
			h[i]= x[i].hProcess;
		
		}
		
	WaitForMultipleObjects(N, h,TRUE,INFINITE);
	
	for (int i=0;i<N;i++)
		{
		CloseHandle(x[i].hProcess);
		CloseHandle(x[i].hThread);
		}

}

CreateFileMapping or MapViewOfFileEx example

CreateFileMapping or MapViewOfFileEx example

#include <windows.h> 
#include <string> 
#include <winbase.h> 
#include <iostream> 
using namespace std;

void main()
{
	HANDLE  h;

	CHAR *filename;
 
	filename =new CHAR[2500];
	wcout<<"enter the file name:";
	cin.getline (filename,2500);
	h= CreateFile( filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
	cout<<filename;
	if (h!=INVALID_HANDLE_VALUE)
	{
	printf("\nFile is opened/created");
	DWORD size = GetFileSize(h, NULL);
	HANDLE hFileMapping = CreateFileMapping(h, NULL,PAGE_READONLY, 0, 0, NULL);	
	
	CloseHandle(h);

	MapViewOfFileEx(hFileMapping, FILE_MAP_READ, 0,  0,0,NULL);           
		system("Pause");

	UnmapViewOfFile(hFileMapping);
	
	}
	else
	{
	printf("\nUnable to open or create file");
	}
	system ("pause");

}