Windows System Programming
These programs are not perfect, but worked for me. Use on your own risk!
The GNU License GPL (Version 3 or later) applies to all the code published on this page
Duplicate (=Copy) a File to a Generated Filename
For example, »duplicate example.txt« might have the effect of »copy "example.txt" "example (1).txt"« or »copy "example.txt" "example (2).txt"«.
duplicate.c
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
_CRTIMP char* __cdecl _itoa (int, char*, int); /* can be removed *//* To compile as console application under Dev-Cpp 4.9.9.0 */
/* Should also compile under most other C compilers under Windows. *//* Duplicate - Copies a file to a generated filename.
Copyright (C) 2011 Stefan RamThis program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */int error1( LPCTSTR const argv0 )
{ _tprintf( _T("Error 1: Usage: %s <source file name>\n"), argv0 );
_tprintf( _T("Copies the source file to a generated file name"));
_tprintf( _T("Duplicate Copyright (C) 2011 Stefan Ram"));
_tprintf( _T("This program comes with ABSOLUTELY NO WARRANTY."));
_tprintf( _T("For details see see <http://www.gnu.org/licenses/>"));
return 1; }void error2( void )
{ _tprintf( _T("Error 2: Internal failure \"error2\" (lack of memory?).")); }int error3( void )
{ _tprintf( _T("Error 3: Can't allocate memory for a string buffer."));
return 0; }int error4( void )
{ _tprintf( _T("Error 4: Integer overflow (all filenames taken).")); return 0; }int error5()
{ int terminate = 0;
DWORD code = GetLastError();
if( code != ERROR_FILE_EXISTS )
{ if( code == ERROR_FILE_NOT_FOUND )
{ _ftprintf( stderr, _T("File not found.\n")); }
else
{ _ftprintf
( stderr, _T("Windows error code %#010lx.\n"),( unsigned long )code ); }
terminate = 1; }
return terminate; }int i = 0;
int tag( LPCTSTR source, LPTSTR target, size_t const size )
{ int result;
_tcscat( target, source );
_tcscat( target, _T(" (") );
{ LPTSTR t = target + _tcslen( target );
// size_t const s = size -( t - target )- 2;
// result = !_itot_s( i, t, s, 10 );
_itot( i, t, 10 ); result = 1; }
if( result )_tcscat( target, _T(")") );
return result; }int modify1( LPTSTR source, LPTSTR target, size_t const size )
{ *target = 0;
int result;
LPTSTR pos = _tcschr( source, *_T("."));
if( pos )
{ LPTSTR p; while( p = _tcschr( pos + 1, *_T(".")))pos = p;
*pos = 0;
tag( source, target, size );
*pos = *_T(".");
_tcscat( target, pos ); }
else
{ tag( source, target, size ); }
return result; }int modify( LPTSTR const source, LPTSTR target, size_t const size )
{ return ++i > 0 ? modify1( source, target, size ): error4(); }int setup( LPCTSTR const source, LPTSTR * const targetp, size_t * sizep )
{ size_t const len = _tcslen( source ) + 1;
*sizep =( len + sizeof( int )/2 + 10 )* sizeof( TCHAR );
return( *targetp = malloc( *sizep ) )? 1: error3(); }void release( LPTSTR * const targetp )
{ free( *targetp );
*targetp = 0; }int filecopy( LPTSTR const target, LPTSTR const source )
{ int const success = CopyFile( source, target, TRUE );
int const terminate = success ? 1 : error5();
return terminate; }int main2( LPTSTR const target, LPTSTR const source, size_t const size )
{ for( int looping = 1; looping; )
{ if( !modify( source, target, size ))looping = 0; else
if( filecopy( target, source ))looping = 0; }
return 0; }int main1( LPTSTR const source )
{ LPTSTR target; size_t size;
if( setup( source, &target, &size ))
{ main2( target, source, size );
release( &target ); }
return 0; }int _tmain( int argc, LPTSTR const argv[] )
{ return argc == 2 ? main1( argv[ 1 ]): error1( argv[ 0 ]); }
Change Registry Settings
This is the classical version of a program that was written by Stefan Ram in 1997 or 1998.
exereg.c
#include <Windows.h>
int main( int argc, char * argv[] ){ int result; HKEY key; /* Copyright (C) 1997-2011 Stefan Ram */
long effect = RegOpenKeyEx( HKEY_CURRENT_USER, argv[ 2 ], 0, KEY_ALL_ACCESS, &key );
if( !effect ) { long effect = RegSetValueEx( key, argv[ 3 ], 0, REG_SZ, argv[ 4 ], strlen( argv[ 4 ]) + 1 );
if( !effect ) { SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0, (LONG)(( LPSTR )argv[ 2 ] )); }} return 0; }
example usage
exereg HKEY_CURRENT_USER "SOFTWARE\Microsoft\Internet Explorer\Main" "Display Inline Images" "no"
usage
exereg HKEY_CURRENT_USER <key> <entry> <value>
UTF-8
Die Windows-Konsole unterstützt UTF-8 nicht vollständig.
Man kann die Ausgabe in eine Datei umleiten und diese mit einem UTF-8-Editor öffnen.
main.c
#include <stdlib.h>
#include <wchar.h>
#include <Windows.h>
int main()
{ //C:\Windows\System32\chcp.com 65001
UINT oldcp = GetConsoleOutputCP();
if (!SetConsoleOutputCP(CP_UTF8))
{ fprintf(stderr, "chcp failed\n");
return EXIT_FAILURE; }
unsigned char utf8data[] =
{ 'H',
0xc3, 0xa4,
'l',
0xC2, 0xA2, // "cent"
0xE2, 0x82, 0xAC, // "euro"
0xCE, 0xB1, 0xAC, // "alpha"
// 0xF0, 0x90, 0x8D, 0x88, // U+10348
'l',
0xc3, 0xb6,
'!',
'\n',
0x00 };
DWORD size=(DWORD)(sizeof(utf8data) / sizeof(*utf8data)) - 1;
DWORD written = 0;
BOOL success = WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), utf8data, size, &written, 0 );
if (!success)
{
fprintf(stderr, "WriteFile failed\n");
return EXIT_FAILURE;
}
SetConsoleOutputCP(oldcp);
return EXIT_SUCCESS;
}
main.cpp
#include <iostream>
#include <ostream>
#include <cstdlib>
#include <cwchar>
#include <windows.h>
int main()
{ //C:\Windows\System32\chcp.com 65001
UINT oldcp = GetConsoleOutputCP();
if( !SetConsoleOutputCP( CP_UTF8 )){ ::std::cerr << "chcp failed\n"; return EXIT_FAILURE; }
unsigned char utf8data[] = u8"Häl¢€αlö!\n"; // u8 ist nicht nötig, wenn Quelltext schon UTF-8
DWORD size=( DWORD )( sizeof( utf8data )/sizeof( *utf8data )) - 1;
DWORD written = 0;
BOOL success = WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), utf8data, size, &written, 0 );
if (!success) { ::std::cerr << "WriteFile failed\n"; return EXIT_FAILURE; }
// Enable buffering to prevent VS from chopping up UTF-8 byte sequences
// setvbuf(stdout, nullptr, _IOFBF, 1000);
::std::cout << utf8data << ::std::endl;
SetConsoleOutputCP( oldcp );
return EXIT_SUCCESS; }
Protokoll
Häl¢€alö!
Häl¢€alö!