Slide 14.6: Linking to C/C++ (cont.)
Slide 14.8: Calling C/C++ functions
Home

Linking to C/C++ (Cont.)


Code Example—Encrypting a File (Cont.)

 main.cpp 
// main.cpp - Encrypting a file.

#include  <iostream>
#include  <fstream>
#include  "Encrypt.h"
using namespace std;

const int  BUFSIZE = 2000;
char  buffer[BUFSIZE];
unsigned int   count;                   // character count
unsigned char  encryptCode;

int main( int argcount, char* args[ ] ) {
  
  // Read input and output files from the command line.
  if ( argcount < 3 ) {
    cout << "Usage: Encrypt infile outfile" << endl;
    return( -1 );
  }
  cout << "Encryption code [0-255]? ";
  cin  >> encryptCode;

  ifstream  infile(  args[1], ios::binary );
  ofstream  outfile( args[2], ios::binary );
  cout << "Reading " << args[1] << " and creating " << args[2] << endl;

  while ( !infile.eof( ) ) {
    infile.read( buffer, BUFSIZE );
    count = infile.gcount( );
       
outfile.write( buffer, count ); } return( 0 ); }
 Encrypt.h 
// Encript.h

extern "C" {
  void  Encrypt( char* buf, long count, char eChar );
}
 Encrypt.asm 
TITLE Encrypt Procedure      (Encrypt.asm)

.586
.model  flat, C

Encrypt PROTO,
      
.code Encrypt PROC,
      
mov esi, buf mov ecx, count mov al, eChar L1: xor [esi], al inc esi loop L1 ret Encrypt ENDP END