So I was writing a program that would take a string from clipboard, do some stuff and put the new string back to clipboard. My code so far is posted below. The problem is I want my program to wait for me until I 'ctrl+C' something. I know that GetClipboardData()
returns NULL if something goes wrong. Is it true that it also returns NULL if clipboard is empty ?
Code :
# include <stdio.h>
# include <math.h>
# include <windows.h>
int prime_check ( int i )
{
int j = 0;
for(j=2; j <= sqrt(i); j++ )
if(!(i%j)) break;
if (j>sqrt(i))
return 1 ;
else
return 0 ;
}
int main (void)
{
HANDLE h, hData ;
int n, i, j = 0, prime = 0, composite = 0 ;
int nStrLen = 0 ;
char arr[50], num[20], str[1000], *ptrData = NULL ;
printf ("starting") ;
EmptyClipboard();
if (!OpenClipboard(NULL))
perror("Error in clipboard") ;
printf ("\nready") ;
if ( GetClipboardData(CF_TEXT) == NULL )
h = GetClipboardData(CF_TEXT);
strcpy( str, (char *)h ) ;
for ( i = 0; str[i] != '\0'; i++ )
{
if ( isdigit(str[i]) )
{
n = str[i] - '0' ;
if ( n != 1 && n != 0)
{
if ( prime_check (n) == 1 )
prime += n ;
else
composite += n ;
}
}
else
if ( (i < 25) && (isdigit (str[i]) == 0) )
arr[j++] = str[i] + 1 ;
}
itoa ( prime*composite, num, 10 ) ;
strcat ( arr, num ) ;
nStrLen = strlen ( arr ) ;
hData = GlobalAlloc ( GMEM_MOVEABLE | GMEM_DDESHARE, nStrLen + 1 ) ;
ptrData = (char*)GlobalLock(hData) ;
memcpy ( ptrData, arr, nStrLen + 1 ) ;
GlobalUnlock ( hData ) ;
EmptyClipboard () ;
SetClipboardData ( CF_TEXT,hData ) ;
CloseClipboard () ;
return 0;
}
Thank You