Thursday, July 14, 2011

c++ secure library - from visual studio 2005

_s ==> secure library


  1. sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l 
    • MSDN: http://msdn.microsoft.com/en-us/library/ce3zzk1k(VS.80).aspx
    • Korean blogs: http://blog.naver.com/tomatoalive?Redirect=Log&logNo=40077924809
  2. fopen_s, _wfopen_s 
    • MSDN: http://msdn.microsoft.com/en-us/library/z5hh6ee9(v=VS.80).aspx
    • Korean blogs: http://warmz.tistory.com/87
  3. Examples from MSDN
    • // crt_sprintf_s.c
      // This program uses sprintf_s to format various
      // data and place them in the string named buffer.
      //
      
      #include <stdio.h>
      
      int main( void )
      {
         char  buffer[200], s[] = "computer", c = 'l';
         int   i = 35, j;
         float fp = 1.7320534f;
      
         // Format and print various data: 
         j  = sprintf_s( buffer, 200,     "   String:    %s\n", s );
         j += sprintf_s( buffer + j, 200 - j, "   Character: %c\n", c );
         j += sprintf_s( buffer + j, 200 - j, "   Integer:   %d\n", i );
         j += sprintf_s( buffer + j, 200 - j, "   Real:      %f\n", fp );
      
         printf_s( "Output:\n%s\ncharacter count = %d\n", buffer, j );
      }
      #include <stdio.h>
      
      FILE *stream, *stream2;
      
      int main( void )
      {
         int numclosed;
         errno_t err;
      
         // Open for read (will fail if file "crt_fopen_s.c" does not exist)
         if( (err  = fopen_s( &stream, "crt_fopen_s.c", "r" )) !=0 )
            printf( "The file 'crt_fopen_s.c' was not opened\n" );
         else
            printf( "The file 'crt_fopen_s.c' was opened\n" );
      
         // Open for write 
         if( (err = fopen_s( &stream2, "data2", "w+" )) != 0 )
            printf( "The file 'data2' was not opened\n" );
         else
            printf( "The file 'data2' was opened\n" );
      
         // Close stream if it is not NULL 
         if( stream)
         {
            if ( fclose( stream ) )
            {
               printf( "The file 'crt_fopen_s.c' was not closed\n" );
            }
         }
      
         // All other files are closed:
         numclosed = _fcloseall( );
         printf( "Number of files closed by _fcloseall: %u\n", numclosed );
      }

Wednesday, July 6, 2011

POSIX Thread and MS Visual Studio 2008


For the application of POSIX Thread to Windows (MS Visual Studio), POSIX Thread for Win32 is required. By efforts of Ben Elliston et al, the source tree and precompiled .DLL, .LIB and necessary header files are included in the self-extracting zip file named "pthread-w32-v-v-v-release.exe" at: ftp://sourceware.org/pub/pthreads-win32 (unless visit http://sourceware.org/pthreads-win32/)

After extraction,
1) Copy those files as follows (in my case):
- *.dll: C:\PThread2.8\bin
- *.h (three files): C:\PThread2.8\include
- *.lib & *.a: C:\PThread2.8\lib


2) Set MS Visual Studio Options as follows:
- Tools->Options->Project and Solutions->VC++ Directories
--> Add C:\PThread2.8\bin in Executable files
--> Add C:\PThread2.8\include in Include files
--> Add C:\PThread2.8\lib in Library files

3) Add a library file MS Visual Studio Project Property
- Project Property -> Configuration Properties -> Linker->Additional Dependencies
- 'pthreadVC2.lib' is recommended for MS Visual Studio


* Note
For examples given by the Internet, thread functions needs to add a line "return NULL;".
(It may be a problem for win32 application only.)