出于教育目的,我正在尝试使用此方法“加入”一个 .exe 文件和一个 .zip 文件。我从这里借用了这段代码:http: //www.chilkatforum.com/questions/2354/how-to-write-a-self-extractor-for-the-windows-os 。这是我的代码:
#include <windows.h>
#include <CkByteData.h>
#include <CkFileAccess.h>
int main(int argc, char* argv[])
{
const char *exeFile = "C:/program.exe";
CkFileAccess foo;
int exeSize = foo.FileSize(exeFile);
int nZeros = 4 - (exeSize % 4);
if (nZeros < 4)
{
CkByteData zeros;
zeros.appendCharN(0,nZeros);
// Append the zeros to the .exe
zeros.appendFile(exeFilee);
}
// Append the .zip to the .exe
CkByteData zipData;
zipData.loadFile("c:/mine.zip");
zipData.appendFile(exeFile);
return 0;
}
这些是头文件:
CkByteData.h:
// CkByteData.h: interface for the CkByteData class.
//
//////////////////////////////////////////////////////////////////////
#ifndef _CKBYTEDATA_H
#define _CKBYTEDATA_H
#include "CkObject.h"
#include "CkString.h"
// The CkByteData class is a convenient class for holding a block of
// binary data, or non-null terminated text data.
#ifndef __sun__
#pragma pack (push, 8)
#endif
// CLASS: CkByteData
class CkByteData : public CkObject
{
public:
CkByteData();
~CkByteData();
CkByteData(const CkByteData &);
CkByteData &operator=(const CkByteData &);
// BEGIN PUBLIC INTERFACE
bool get_Utf8(void) const;
void put_Utf8(bool b);
// Clears the object of data.
void clear(void);
// Get the size in bytes.
unsigned long getSize(void) const;
bool equals2(const void *pByteData, unsigned long szByteData) const;
bool equals(const CkByteData &db) const;
// Get a pointer to the data.
const unsigned char *getData(void) const;
const unsigned char *getBytes(void) const;
const unsigned char *getDataAt(unsigned long byteIndex) const;
const unsigned char *getRange(unsigned long byteIndex, unsigned long numBytes);
const char *getRangeStr(unsigned long byteIndex, unsigned long numAnsiChars);
unsigned char getByte(unsigned long byteIndex) const;
char getChar(unsigned long byteIndex) const;
unsigned int getUInt(unsigned long byteIndex) const;
int getInt(unsigned long byteIndex) const;
unsigned short getUShort(unsigned long byteIndex) const;
short getShort(unsigned long byteIndex) const;
void appendRandom(int numBytes);
void appendInt(int v, bool littleEndian);
void appendShort(short v, bool littleEndian);
const char *getEncodedRange(const char *encoding, unsigned long index, unsigned long numBytes);
void appendRange(const CkByteData &byteData, unsigned long index, unsigned long numBytes);
void ensureBuffer(unsigned long numBytes);
// Return -1 if not found, otherwise returns the index.
int findBytes2(const void *pByteData, unsigned long szByteData);
int findBytes(const CkByteData &byteData);
bool beginsWith2(const void *pByteData, unsigned long szByteData);
bool beginsWith(const CkByteData &byteData);
void removeChunk(unsigned long index, unsigned long numBytes);
void byteSwap4321(void);
void pad(int blockSize, int paddingScheme);
void unpad(int blockSize, int paddingScheme);
bool is7bit(void) const;
const char *computeHash(const char *hashAlg, const char *encoding);
// Encoding can be "base64", "quoted-printable", "hex", or "url"
const char *getEncoded(const char *encoding);
const wchar_t *getEncodedW(const wchar_t *encoding);
void replaceChar(unsigned char c, unsigned char replacement);
// Append more data
//void append(const unsigned char *data, unsigned long numBytes);
//void append(const char *byteData, unsigned long numBytes);
void append2(const void *pByteData, unsigned long szByteData);
void append(const CkByteData &db);
// Appends null-terminated sequence of bytes, which may be characters in any multibyte charset.
void appendStr(const char *str);
// Appends a string, but the charset must be explictly specified because
// the Unicode string is first converted to this charset (such as utf-8) prior
// to appending.
void appendStrW(const wchar_t *str, const wchar_t *charset);
void appendChar(char ch);
void appendCharN(char ch, int numTimes);
// Encoding can be "base64", "quoted-printable", "hex", or "url"
void appendEncoded(const char *str, const char *encoding);
void appendEncodedW(const wchar_t *str, const wchar_t *encoding);
void encode(const char *encoding, CkString &str);
void encodeW(const wchar_t *encoding, CkString &str);
// Load a complete file into this object. The contents of this object
// are automatically cleared before the file is loaded, so the result
// is a mirror image (in memory) of the bytes in the file.
bool loadFile(const char *path);
bool saveFile(const char *path);
bool loadFileW(const wchar_t *path);
bool saveFileW(const wchar_t *path);
// Create a new file, or append to an existing file.
bool appendFile(const char *path);
bool appendFileW(const wchar_t *path);
// Discards the last numBytes of data.
void shorten(unsigned long numBytes);
// The CkByteData will use *your* memory buffer, and will not delete
// it when the object is destructed.
void borrowData(void *pByteData, unsigned long szByteData);
// Removes the data from the CkByteData object. The caller will receive
// a pointer to the memory buffer, and is responsible for deleting it.
// (Example:
// unsigned char *data = byteData.removeData();
// ... do something with the data....
// delete [] data;
unsigned char *removeData(void);
void preAllocate(unsigned long expectedNumBytes);
const wchar_t *to_ws(const char *charset);
const char *to_s(void);
const char *getXmlCharset(void);
// BYTES_INSERT_POINT
// END PUBLIC INTERFACE
private:
// Internal implementation.
void *m_impl;
bool m_utf8; // If true, all input "const char *" parameters are utf-8, otherwise they are ANSI strings.
void *m_pResultData; // For to_s(), getRange(), getRangeStr()
public:
// Used internally by Chilkat.
void *getImpl(void) const;
const void *getImplC(void) const;
void setImpl(void *impl);
};
#ifndef __sun__
#pragma pack (pop)
#endif
#endif
和 CkFileAccess.h:
// CkFileAccess.h: interface for the CkFileAccess class.
//
//////////////////////////////////////////////////////////////////////
#ifndef _CKFILEACCESS_H
#define _CKFILEACCESS_H
class CkByteData;
class CkDateTime;
#include "CkString.h"
#include "CkMultiByteBase.h"
#ifdef WIN32
#pragma warning( disable : 4068 )
#pragma unmanaged
#endif
/*
IMPORTANT: Objects returned by methods as non-const pointers must be deleted
by the calling application.
*/
#ifndef __sun__
#pragma pack (push, 8)
#endif
// CLASS: CkFileAccess
class CkFileAccess : public CkMultiByteBase
{
public:
CkFileAccess();
virtual ~CkFileAccess();
// May be called when finished with the object to free/dispose of any
// internal resources held by the object.
void dispose(void);
// BEGIN PUBLIC INTERFACE
bool ReadEntireTextFile(const char *path, const char *charset, CkString &outStrFileContents);
const char *readEntireTextFile(const char *path, const char *charset);
// CURRENTDIR_BEGIN
void get_CurrentDir(CkString &str);
const char *currentDir(void);
// CURRENTDIR_END
// ENDOFFILE_BEGIN
bool get_EndOfFile(void);
// ENDOFFILE_END
// SETCURRENTDIR_BEGIN
bool SetCurrentDir(const char *path);
// SETCURRENTDIR_END
// APPENDANSI_BEGIN
bool AppendAnsi(const char *text);
// APPENDANSI_END
// APPENDTEXT_BEGIN
bool AppendText(const char *text, const char *charset);
// APPENDTEXT_END
// APPENDUNICODEBOM_BEGIN
bool AppendUnicodeBOM(void);
// APPENDUNICODEBOM_END
// APPENDUTF8BOM_BEGIN
bool AppendUtf8BOM(void);
// APPENDUTF8BOM_END
// DIRAUTOCREATE_BEGIN
bool DirAutoCreate(const char *path);
// DIRAUTOCREATE_END
// DIRCREATE_BEGIN
bool DirCreate(const char *path);
// DIRCREATE_END
// DIRDELETE_BEGIN
bool DirDelete(const char *path);
// DIRDELETE_END
// DIRENSUREEXISTS_BEGIN
bool DirEnsureExists(const char *filePath);
// DIRENSUREEXISTS_END
// FILECLOSE_BEGIN
void FileClose(void);
// FILECLOSE_END
// FILECOPY_BEGIN
bool FileCopy(const char *existingPath, const char *newPath, bool failIfExists);
// FILECOPY_END
// FILEDELETE_BEGIN
bool FileDelete(const char *path);
// FILEDELETE_END
// FILEEXISTS_BEGIN
bool FileExists(const char *path);
// FILEEXISTS_END
// FILEOPEN_BEGIN
bool FileOpen(const char *path, unsigned int accessMode,
unsigned int shareMode, unsigned int createDisp, unsigned int attr);
// FILEOPEN_END
// FILEREAD_BEGIN
bool FileRead(int numBytes, CkByteData &outBytes);
// FILEREAD_END
// FILERENAME_BEGIN
bool FileRename(const char *existingPath, const char *newPath);
// FILERENAME_END
// FILESEEK_BEGIN
bool FileSeek(int offset, int origin);
// FILESEEK_END
// FILESIZE_BEGIN
int FileSize(const char *path);
// FILESIZE_END
// FILEWRITE_BEGIN
bool FileWrite(const CkByteData &data);
// FILEWRITE_END
// GETTEMPFILENAME_BEGIN
bool GetTempFilename(const char *dirName, const char *prefix, CkString &outStr);
const char *getTempFilename(const char *dirName, const char *prefix);
// GETTEMPFILENAME_END
// READENTIREFILE_BEGIN
bool ReadEntireFile(const char *path, CkByteData &outBytes);
// READENTIREFILE_END
// TREEDELETE_BEGIN
bool TreeDelete(const char *path);
// TREEDELETE_END
// WRITEENTIREFILE_BEGIN
bool WriteEntireFile(const char *path, const CkByteData &fileData);
// WRITEENTIREFILE_END
// WRITEENTIRETEXTFILE_BEGIN
bool WriteEntireTextFile(const char *path, const char *fileData, const char *charset, bool includePreamble);
// WRITEENTIRETEXTFILE_END
// SPLITFILE_BEGIN
bool SplitFile(const char *fileToSplit, const char *partPrefix, const char *partExtension, int partSize, const char *destDir);
// SPLITFILE_END
// REASSEMBLEFILE_BEGIN
bool ReassembleFile(const char *partsDirPath, const char *partPrefix, const char *partExtension, const char *reassembledFilename);
// REASSEMBLEFILE_END
// READBINARYTOENCODED_BEGIN
bool ReadBinaryToEncoded(const char *path, const char *encoding, CkString &outStr);
const char *readBinaryToEncoded(const char *path, const char *encoding);
// READBINARYTOENCODED_END
// FILEOPENERROR_BEGIN
int get_FileOpenError(void);
// FILEOPENERROR_END
// FILEOPENERRORMSG_BEGIN
void get_FileOpenErrorMsg(CkString &str);
const char *fileOpenErrorMsg(void);
// FILEOPENERRORMSG_END
// OPENFORREAD_BEGIN
bool OpenForRead(const char *filePath);
// OPENFORREAD_END
// OPENFORWRITE_BEGIN
bool OpenForWrite(const char *filePath);
// OPENFORWRITE_END
// OPENFORREADWRITE_BEGIN
bool OpenForReadWrite(const char *filePath);
// OPENFORREADWRITE_END
// OPENFORAPPEND_BEGIN
bool OpenForAppend(const char *filePath);
// OPENFORAPPEND_END
// REPLACESTRINGS_BEGIN
int ReplaceStrings(const char *path, const char *charset, const char *existingString, const char *replacementString);
// REPLACESTRINGS_END
// SETFILETIMES_BEGIN
bool SetFileTimes(const char *path, CkDateTime &create, CkDateTime &lastAccess, CkDateTime &lastModified);
bool SetLastModified(const char *path, CkDateTime &lastModified);
// SETFILETIMES_END
// FILEACCESS_INSERT_POINT
// END PUBLIC INTERFACE
// For internal use only.
private:
// Don't allow assignment or copying these objects.
CkFileAccess(const CkFileAccess &) { }
CkFileAccess &operator=(const CkFileAccess &) { return *this; }
CkFileAccess(void *impl);
};
#ifndef __sun__
#pragma pack (pop)
#endif
#endif
用这些构建解决方案会给我以下错误:
1>------ Build started: Project: General, Configuration: Debug Win32 ------
1> silent.cpp
1>silent.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CkFileAccess::~CkFileAccess(void)" (??1CkFileAccess@@UAE@XZ) referenced in function _main
1>silent.obj : error LNK2019: unresolved external symbol "public: bool __thiscall CkByteData::loadFile(char const *)" (?loadFile@CkByteData@@QAE_NPBD@Z) referenced in function _main
1>silent.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CkByteData::~CkByteData(void)" (??1CkByteData@@UAE@XZ) referenced in function _main
1>silent.obj : error LNK2019: unresolved external symbol "public: bool __thiscall CkByteData::appendFile(char const *)" (?appendFile@CkByteData@@QAE_NPBD@Z) referenced in function _main
1>silent.obj : error LNK2019: unresolved external symbol "public: void __thiscall CkByteData::appendCharN(char,int)" (?appendCharN@CkByteData@@QAEXDH@Z) referenced in function _main
1>silent.obj : error LNK2019: unresolved external symbol "public: __thiscall CkByteData::CkByteData(void)" (??0CkByteData@@QAE@XZ) referenced in function _main
1>silent.obj : error LNK2019: unresolved external symbol "public: int __thiscall CkFileAccess::FileSize(char const *)" (?FileSize@CkFileAccess@@QAEHPBD@Z) referenced in function _main
1>silent.obj : error LNK2019: unresolved external symbol "public: __thiscall CkFileAccess::CkFileAccess(void)" (??0CkFileAccess@@QAE@XZ) referenced in function _main
1>C:\Users\Simboyd\documents\visual studio 2010\Projects\General\Debug\General.exe : fatal error LNK1120: 8 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
谁能告诉我这里应该改变什么?我知道错误在头文件中,对吗?先谢谢了