Win32 Equivalents for C Run-Time Functions

The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), included with:
    • Operating system: Microsoft Windows NT, versions 3.1 , 3.5 , 3.51,4.0,2k
    • Microsoft Windows 95

SUMMARY

Many of the C Run-time functions have direct equivalents in the Win32 application programming interface (API). This article lists the C Run-time functions by category with their Win32 equivalents or the word "none" if no equivalent exists.

MORE INFORMATION

NOTE: the functions that are followed by an asterisk (*) are part of the 16-bit C Run-time only. Functions that are unique to the 32-bit C Run-time are listed separately in the last section. All other functions are common to both C Run-times.

 

Buffer Manipulation

C Run time
Win32 Equivalent
_memccpy
none
memchr
none
memcmp
none
memcpy
CopyMemory (See Note 6)
_memicmp
none
memmove
MoveMemory (See Note 6)
memset
FillMemory, ZeroMemory (See Note 6)
_swab
None

 

Character Classification

C Run time
Win32 Equivalent
Isalnum
IsCharAlphaNumeric
Isalpha
IsCharAlpha, GetStringTypeW (Unicode)
__isascii
None
iscntrl
None, GetStringTypeW (Unicode)
__iscsym
None
__iscsymf
None
isdigit
None, GetStringTypeW (Unicode)
isgraph
None
islower
IsCharLower, GetStringTypeW (Unicode)
isprint
None
ispunct
None, GetStringTypeW (Unicode)
isspace
None, GetStringTypeW (Unicode)
isupper
IsCharUpper, GetStringTypeW (Unicode)
isxdigit
None, GetStringTypeW (Unicode)
__toascii
None
tolower
CharLower
_tolower
None
toupper
CharUpper
_toupper
None

 

Directory Control

C Run time
Win32 Equivalent
_chdir
SetCurrentDirectory
_chdrive
SetCurrentDirectory
_getcwd
GetCurrentDirectory
_getdrive
GetCurrentDirectory
_mkdir
CreateDirectory
_rmdir
RemoveDirectory
_searchenv
SearchPath

 

File Handling

C Run time
Win32 Equivalent
_access
none
_chmod
SetFileAttributes
_chsize
SetEndOfFile
_filelength
GetFileSize
_fstat
See Note 5
_fullpath
GetFullPathName
_get_osfhandle
none
_isatty
GetFileType
_locking
LockFileEx
_makepath
none
_mktemp
GetTempFileName
_open_osfhandle
None
remove
DeleteFile
rename
MoveFile
_setmode
None
_splitpath
None
_stat
None
_umask
None
_unlink
DeleteFile

 

Creating Text Output Routines

C Run time
Win32 Equivalent
_displaycursor*
SetConsoleCursorInfo
_gettextcolor*
GetConsoleScreenBufferInfo
_gettextcursor*
GetConsoleCursorInfo
_gettextposition*
GetConsoleScreenBufferInfo
_gettextwindow*
GetConsoleWindowInfo
_outtext*
WriteConsole
_scrolltextwindow*
ScrollConsoleScreenBuffer
_settextcolor*
SetConsoleTextAttribute
_settextcursor*
SetConsoleCursorInfo
_settextposition*
SetConsoleCursorPosition
_settextwindow*
SetConsoleWindowInfo
_wrapon*
SetConsoleMode

 

Stream Routines

C Run time
Win32 Equivalent
clearerr
none
fclose
CloseHandle
_fcloseall
none
_fdopen
none
feof
none
ferror
none
fflush
FlushFileBuffers
fgetc
none
_fgetchar
none
fgetpos
none
fgets
none
_fileno
none
_flushall
none
fopen
CreateFile
fprintf
none
fputc
none
_fputchar
none
fputs
none
fread
ReadFile
freopen (std handles)
SetStdHandle
fscanf
None
fseek
SetFilePointer
fsetpos
SetFilePointer
_fsopen
CreateFile
ftell
SetFilePointer (check return value)
fwrite
WriteFile
getc
None
getchar
None
gets
None
_getw
None
printf
None
putc
None
putchar
None
puts
None
_putw
None
rewind
SetFilePointer
_rmtmp
None
scanf
None
setbuf
None
setvbuf
None
_snprintf
None
sprintf
Wsprintf
sscanf
None
_tempnam
GetTempFileName
tmpfile
None
tmpnam
GetTempFileName
ungetc
None
vfprintf
None
vprintf
None
_vsnprintf
None
vsprintf
Wvsprintf

 

Low-Level I/O

C Run time
Win32 Equivalent
_close
_lclose, CloseHandle
_commit
FlushFileBuffers
_creat
_lcreat, CreateFile
_dup
DuplicateHandle
_dup2
none
_eof
none
_lseek
_llseek, SetFilePointer
_open
_lopen, CreateFile
_read
_lread, ReadFile
_sopen
CreateFile
_tell
SetFilePointer (check return value)
_write
_lread

 

Console and Port I/O Routines

C Run time
Win32 Equivalent
_cgets
none
_cprintf
none
_cputs
none
_cscanf
none
_getch
ReadConsoleInput
_getche
ReadConsoleInput
_inp
none
_inpw
none
_kbhit
PeekConsoleInput
_outp
none
_outpw
none
_putch
WriteConsoleInput
_ungetch
none

 

Memory Allocation

C Run time
Win32 Equivalent
_alloca
none
_bfreeseg*
none
_bheapseg*
none
calloc
GlobalAlloc
_expand
none
free
GlobalFree
_freect*
GlobalMemoryStatus
_halloc*
GlobalAlloc
_heapadd
none
_heapchk
none
_heapmin
none
_heapset
none
_heapwalk
none
_hfree*
GlobalFree
malloc
GlobalAlloc
_memavl
GlobalMemoryStatus
_memmax
GlobalMemoryStatus
_msize*
GlobalSize
realloc
GlobalReAlloc
_set_new_handler
none
_set_hnew_handler*
None
_stackavail*
None

 

Process and Environment Control Routines

C Run time
Win32 Equivalent
abort
none
assert
none
atexit
none
_cexit
none
_c_exit
none
_exec functions
none
exit
ExitProcess
_exit
ExitProcess
getenv
GetEnvironmentVariable
_getpid
GetCurrentProcessId
Longjmp
none
_onexit
none
Perror
FormatMessage
_putenv
SetEnvironmentVariable
Raise
RaiseException
Setjmp
none
signal (ctrl-c only)
SetConsoleCtrlHandler
_spawn functions
CreateProcess
system
CreateProcess

 

String Manipulation

C Run time
Win32 Equivalent
strcat, wcscat
lstrcat
strchr, wcschr
none
strcmp, wcscmp
lstrcmp
strcpy, wcscpy
lstrcpy
strcspn, wcscspn
none
_strdup, _wcsdup
none
Strerror
FormatMessage
_strerror
FormatMessage
_stricmp, _wcsicmp
lstrcmpi
Strlen, wcslen
lstrlen
_strlwr, _wcslwr
CharLower, CharLowerBuffer
Strncat, wcsncat
none
Strncmp, wcsncmp
none
Strncpy, wcsncpy
none
_strnicmp, _wcsnicmp
none
_strnset, _wcsnset
FillMemory, ZeroMemory
strpbrk, wcspbrk
none
strrchr, wcsrchr
none
_strrev, _wcsrev
none
_strset, _wcsset
FillMemory, ZeroMemory
strspn, wcsspn
none
strstr, wcsstr
none
strtok, wcstok
none
_strupr, _wcsupr
CharUpper, CharUpperBuffer

 

MS-DOS Interface

C Run time
Win32 Equivalent
_bdos*
none
_chain_intr*
none
_disable*
none
_dos_allocmem*
GlobalAlloc
_dos_close*
CloseHandle
_dos_commit*
FlushFileBuffers
_dos_creat*
CreateFile
_dos_creatnew*
CreateFile
_dos_findfirst*
FindFirstFile
_dos_findnext*
FindNextFile
_dos_freemem*
GlobalFree
_dos_getdate*
GetSystemTime
_dos_getdiskfree*
GetDiskFreeSpace
_dos_getdrive*
GetCurrentDirectory
_dos_getfileattr*
GetFileAttributes
_dos_getftime*
GetFileTime
_dos_gettime*
GetSystemTime
_dos_getvect*
none
_dos_keep*
none
_dos_open*
OpenFile
_dos_read*
ReadFile
_dos_setblock*
GlobalReAlloc
_dos_setdate*
SetSystemTime
_dos_setdrive*
SetCurrentDirectory
_dos_setfileattr*
SetFileAttributes
_dos_setftime*
SetFileTime
_dos_settime*
SetSystemTime
_dos_setvect*
none
_dos_write*
WriteFile
_dosexterr*
GetLastError
_enable*
none
_FP_OFF*
none
_FP_SEG*
none
_harderr*
See Note 1
_hardresume*
See Note 1
_hardretn*
See Note 1
_int86*
none
_int86x*
none
_intdos*
none
_intdosx*
none
_segread*
none

 

Time

C Run time
Win32 Equivalent
asctime
See Note 2
clock
See Note 2
ctime
See Note 2
difftime
See Note 2
_ftime
See Note 2
_getsystime
GetLocalTime
gmtime
See Note 2
localtime
See Note 2
mktime
See Note 2
_strdate
See Note 2
_strtime
See Note 2
time
See Note 2
_tzset
See Note 2
_utime
SetFileTime

 

Virtual Memory Allocation

C Run time
Win32 Equivalent
_vfree*
See Note 3
_vheapinit*
See Note 3
_vheapterm*
See Note 3
_vload*
See Note 3
_vlock*
See Note 3
_vlockcnt*
See Note 3
_vmalloc*
See Note 3
_vmsize*
See Note 3
_vrealloc*
See Note 3
_vunlock*
See Note 3

 

32-Bit C Run Time

C Run time
Win32 Equivalent
_beginthread
CreateThread
_cwait
WaitForSingleObject w/ GetExitCodeProcess
_endthread
ExitThread
_findclose
FindClose
_findfirst
FindFirstFile
_findnext
FindNextFile
_futime
SetFileTime
_get_osfhandle
none
_open_osfhandle
none
_pclose
See Note 4
_pipe
CreatePipe
_popen
See Note 4



Note 1: The _harderr functions do not exist in the Win32 API. However, much of their functionality is available through structured exception handling.

Note 2: The time functions are based on a format that is not used in Win32. There are specific Win32 time functions that are documented in the Help file.

Note 3: The virtual memory functions listed in this document are specific to the MS-DOS environment and were written to access memory beyond the 640K of RAM available in MS-DOS. Because this limitation does not exist in Win32, the standard memory allocation functions should be used.

Note 4: While _pclose() and _popen() do not have direct Win32 equivalents, you can (with some work) simulate them with the following calls:

 _popen	CreatePipe
		CreateProcess

 _pclose	WaitForSingleObject
		CloseHandle

Note 5: GetFileInformationByHandle() is the Win32 equivalent for the _fstat() C Run-time function. However, GetFileInformationByHandle() is not supported by Win32s version 1.1. It is supported in Win32s 1.2. GetFileSize(), GetFileAttributes(), GetFileTime(), and GetFileTitle() are supported by Win32s 1.1 and 1.2.

Note 6: There are Win32 Buffer-Manipulation Routines that are actually not document but exist in kernel32.dll. Win32 functions such as FillMemory, CopyMemory, MoveMemory, and ZeroMemory are all actually macros that use the CLIB equivalents. After removing all default libraries we no longer have the CLIB, therefore we must use an alternative. RtlFillMemory, RtlMoveMemory, and RtlZeroMemory are also macros that use CLIB, although these functions actually do exist in kernel32.dll as Win32 API calls. Even though they exist they are not document and chances are that there may indeed be a reason behind that. These functions may be missing in some versions of Windows and are not support by Microsoft. A solution to this is to write your own Buffer-Manipulation Routines.

The following example is a header file that contains the source code to FillMemory, MoveMemory, CopyMemory, and ZeroMemory.

//**********************************************************************
// File: buffer.h			Last Modified:	03/02/01
//				Modified by:	PM
//
// Purpose:      Custom Buffer-Manipulation Routines
//
// Developed By: Piotr Mintus 2001
//**********************************************************************

#ifndef _BUFFER_H_
#define _BUFFER_H_

#ifdef FillMemory
#undef FillMemory
#endif
#ifdef ZeroMemory
#undef ZeroMemory
#endif
#ifdef CopyMemory
#undef CopyMemory
#endif
#ifdef MoveMemory
#undef MoveMemory
#endif

__forceinline
void FillMemory(void *dest,unsigned __int8 c,unsigned __int32 count)
{
unsigned __int32	size32=count>>2;
unsigned __int32	fill=(c<<24|c<<16|c<<8|c);
unsigned __int32	*dest32=(unsigned __int32*)dest;

switch( (count-(size32<<2)) )
{
case 3:	((unsigned __int8*)dest)[count - 3] = c;
case 2:	((unsigned __int8*)dest)[count - 2] = c;
case 1:	((unsigned __int8*)dest)[count - 1] = c;
}

while( size32-- > 0 )
*(dest32++) = fill;

}  /* FillMemory */

#define ZeroMemory(dest,count) FillMemory(dest,0,count)

__forceinline
void CopyMemory(void *dest,const void *src,unsigned __int32 count)
{
unsigned __int32	size32=count>>2;
unsigned __int32	*dest32=(unsigned __int32*)dest;
unsigned __int32	*src32=(unsigned __int32*)src;

switch( (count-(size32<<2)) )
{
case 3:	((unsigned __int8*)dest)[count-1] =
((unsigned __int8*)src)[count-1];
case 2:	((unsigned __int8*)dest)[count-2] =
((unsigned __int8*)src)[count-2];
case 1:	((unsigned __int8*)dest)[count-3] =
((unsigned __int8*)src)[count-3];
}

while( size32-- > 0 )
dest32[size32] = src32[size32];

}  /* CopyMemory */

__forceinline
void MoveMemory(void *dest,const void *src,unsigned __int32 count)
{
unsigned __int32	size32=count>>2,i;
unsigned __int32	*dest32=(unsigned __int32*)dest;
unsigned __int32	*src32=(unsigned __int32*)src;

if( dest > src )
{
switch( (count-(size32<<2)) )
{
case 3:	((unsigned __int8*)dest)[count-1] =
((unsigned __int8*)src)[count-1];
case 2:	((unsigned __int8*)dest)[count-2] =
((unsigned __int8*)src)[count-2];
case 1:	((unsigned __int8*)dest)[count-3] =
((unsigned __int8*)src)[count-3];
}

while( size32-- > 0 )
dest32[size32] = src32[size32];
}
else
{
for(i=0;i<size32;i++)
*(dest32++) = *(src32++);

switch( (count-(size32<<2)) )
{
case 3:	((unsigned __int8*)dest)[count-3] =
((unsigned __int8*)src)[count-3];
case 2:	((unsigned __int8*)dest)[count-2] =
((unsigned __int8*)src)[count-2];
case 1:	((unsigned __int8*)dest)[count-1] =
((unsigned __int8*)src)[count-1];
}
}

}  /* MoveMemory */

#endif // _BUFFER_H_

 

Now you can freely use FillMemory, MoveMemory, CopyMemory, and ZeroMemory within your application.

Observation: The preceding source code uses the __forceinline keyword. This will achieve greater speed. If small size is desired over speed, please remove the keyword and use the preceding source as normal functions.

 

Bibliography:

http://www.hailstorm.net/papers/smallwin32.htm

 

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q99456

 

Download Files

The original files were compressed and can be downloaded here (You will need Winzip to open these files - www.winzip.com)

Win32 Equivalents for C Run-Time Functions

Edited by:

Beyond2000!

March, 17, 2.002