.exe runs fine, but .cpp won't compile, what?

closed account (DL09216C)
The following code was found at "http://www.eriugena.org/code/sendto/" and used in a Wincows XP Pro backup batch file, and it (sendtoEFS.exe) worked great, except for an unwanted pause at the end caused by a messagebox. The website made available the GPL sendtoEFS.cpp source code, so I thought, even though my C++ knowledge was limited, that I would simply delete said messagebox from .cpp and recompile with (downloaded) Quincy 2005. However, the .cpp code wouldn't compile, and returned several errors of type "variable was not declared in this scope" (for OpenEncryptedFileRaw, CloseEncryptedFileRaw, WriteEncryptedFileRaw, and ReadEncryptedFileRaw variables). I've spent many hours studying and trying to figure this out, but don't know why sendtoEFS.exe runs fine, but sendtoEFS.cpp won't compile. Help would be appreciated; thank you.

[/code]
/*
Author: Kevin Connolly http://www.eriugena.org/
(sendtoEFS.cpp) released under GPL
*/

#include <windows.h>
#include <winuser.h>
#include <winbase.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <TCHAR.h>
#include <wincrypt.h>

DWORD WINAPI WriteCallback(PBYTE pbData, PVOID pvCallbackContext, PULONG ulLength)
{
HANDLE *phRawFile=(HANDLE *)pvCallbackContext;
unsigned long nBytesRead=0;
ReadFile(*phRawFile,(LPVOID)pbData,*ulLength,&nBytesRead,0);
*ulLength=nBytesRead;
return ERROR_SUCCESS;
}

int CopyFileFromRaw(LPCTSTR szFileIn, LPCTSTR szFileOut)
{
PVOID pvContext;
HANDLE hRawFile;
hRawFile=CreateFile(szFileIn,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(hRawFile==INVALID_HANDLE_VALUE)
{
printf("\n Error opening %s!\n", szFileIn);
return 2;
}

if(OpenEncryptedFileRaw(szFileOut,CREATE_FOR_IMPORT,&pvContext))
{
printf("\n Error opening %s for writing.", szFileOut);
return 3;
}

WriteEncryptedFileRaw(&WriteCallback, &hRawFile, pvContext);
CloseEncryptedFileRaw(pvContext);
CloseHandle(hRawFile);
// printf(" copied: raw->EFS\n");
return 0;
}

DWORD WINAPI ReadCallback(PBYTE pbData, PVOID pvCallbackContext, ULONG ulLength)
{
HANDLE *phRawFile=(HANDLE *)pvCallbackContext;
unsigned long nBytesWrote;
WriteFile(*phRawFile,(LPCVOID)pbData,ulLength,&nBytesWrote,0);
return false;
}

int CopyFileRaw(LPCTSTR szFileIn, LPCTSTR szFileOut)
{
PVOID pvContext;
HANDLE hRawFile;

if(OpenEncryptedFileRaw(szFileIn,0,&pvContext))
{
if(!(GetFileAttributes(szFileIn)&FILE_ATTRIBUTE_ENCRYPTED))
{
printf("\n %s is not encrypted!\n", szFileIn);
return 4;
}
printf("\n Error opening input %s!\n", szFileIn);
return 1;
}
hRawFile=CreateFile(szFileOut,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(hRawFile==INVALID_HANDLE_VALUE)
{
printf("\n Error opening output %s!\n", szFileOut);
CloseEncryptedFileRaw(pvContext);
return 2;
}

ReadEncryptedFileRaw(&ReadCallback, &hRawFile, pvContext);
CloseEncryptedFileRaw(pvContext);
CloseHandle(hRawFile);
// printf(" copied: EFS->raw\n");
return 0;
}

VOID main(DWORD argc,LPSTR argv[])
{
DWORD dwRet;
DWORD dwFileSystemFlags = 0;
int i, j, len, rc;
int nRaw, nPlain, nFiles;
TCHAR szNewPath[MAX_PATH], szDrive[16];
char *cp;

if(argc < 2) {
MessageBox(NULL, "convert files to/from EFSRAW encrypted", NULL, NULL);
exit(1);
}

nRaw=nPlain=0;
for(i=1;i<argc;i++) {
dwRet=GetFileAttributes(argv[i]);
if(dwRet & FILE_ATTRIBUTE_DIRECTORY) {
MessageBox(NULL, "Cannot process folders. Input must be files. Use WinZIP first.", NULL, NULL);
exit(1);
}

len=lstrlen(argv[i]);
cp = argv[i];
if(len > 6) j = lstrlen(argv[i]) - 6;
if((len > 6) && (!lstrcmp("efsraw", cp+j)) ) {
nRaw++;
printf("EFSraw: %s\n", argv[i]);
} else {
dwRet=GetFileAttributes(argv[i]); /* only check if not efsraw */
if( !(dwRet & FILE_ATTRIBUTE_ENCRYPTED)) {
nPlain++;
printf("not EFS encrypted: %s\n", argv[i]);
}
}
}
if(nPlain) {
MessageBox(NULL, "input files must be EFS encrypted", NULL, NULL);
exit(1);
}

nFiles = argc-1;

if(!nRaw) {
printf("All files are EFS\n");
for(i=1;i<argc;i++) {
lstrcpy(szNewPath, argv[i]);
lstrcat(szNewPath, ".efsraw");
rc = CopyFileRaw(argv[i], szNewPath);
printf("%u %s\n", rc, argv[i]);
}
}

if( (nRaw) &&(nRaw==nFiles) ) {
printf("All files are EFSRAW\n");

lstrcpyn(szDrive, argv[1], 4);
GetVolumeInformation( szDrive, NULL, 0, NULL, NULL, &dwFileSystemFlags, NULL, 0);
if( !(dwFileSystemFlags & FILE_SUPPORTS_ENCRYPTION)) {
printf("Filesystem does not support encryption on %s\n", szDrive);
MessageBox(NULL, "Filesystem does not support encryption. Copy file to another drive.", NULL, NULL);
exit(1);
}

for(i=1;i<argc;i++) {
j = lstrlen(argv[i]) - 6; /* ignore 7 chars ".efsraw" */
lstrcpyn(szNewPath, argv[i], j);
rc = CopyFileFromRaw(argv[i], szNewPath);
printf("%u %s\n", rc, argv[i]);
}
}
if( (nRaw) &&(nRaw<nFiles) ) {
MessageBox(NULL, "Please select only all EFS or all EFSRAW files and not a mix", NULL, NULL);
}

/*
BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
); */

MessageBox(NULL, "Done", "OK", NULL);
}

[/code]
Topic archived. No new replies allowed.