//-------------------------------------------------------------------------DWORD WINAPI MyThreid(LPVOID xx)
{ //复制文件线程,xx传入指向安装路径的指针,必须xx指向的变量在本线程生存期内存在
//比如:在一个按钮的OnClick事件中生成该线程,如果xx指向的是在此事件中定义的非静态
//变量,则会出错,因为该线程运行期间,OnClick的代码已经结束,xx指向的变量已被删除。
char num[5];
char buf2[MAX_PATH];
num[0]=NULL;
LoadString(myexe,5000,num,sizeof(num));
if(num[0]&& !cancel)
{//有num个要创始的安装目录
strcpy(buf1,(char*)xx);
CreateDirectory(buf1,NULL);
for (int i=5001;i<=5000+StrToInt(AnsiString(num));i++)
{ //目录名的资源ID从5000开始
LoadString(myexe,i,buf,MAX_PATH);
lstrcpy(buf1,(char*)xx);
lstrcat(buf1,buf);
Form1->StatusBar1->SimpleText="正在创建目录:"+AnsiString(buf1);
Form1->Memo1->Lines->Add("正在创建目录:"+AnsiString(buf1));
CreateDirectory(buf1,NULL);
}
}
num[0]=NULL;
LoadString(myexe,0,num,sizeof(num));
if(num[0] && !cancel)
{ //有num个要复制到安装目录中的文件
for (int i=1;i<=StrToInt(AnsiString(num));i++)
{ //文件名的资源ID从1开始
if (cancel) break;
MyWriteFile(i,(char*)xx);
}
}
num[0]=NULL;
LoadString(myexe,6000,num,sizeof(num));
if(num[0] && !cancel)
{//有num个要复制到Windows目录中的文件
GetWindowsDirectory(buf2,MAX_PATH);
strcat(buf2,"\\");
for (int i=6001;i<=6000+StrToInt(AnsiString(num));i++)
{//文件名的资源ID从6000开始
if (cancel) break;
MyWriteFile(i,buf2);
}
}
num[0]=NULL;
LoadString(myexe,7000,num,sizeof(num));
if(num[0]&& !cancel)
{//有num个要创始的快捷方式
//有*号的语句是创始快捷方式的套话
HRESULT hres; //*
IShellLink *psl; //*
IPersistFile *ppf; //*
wchar_t wsz[MAX_PATH];//*
CoInitialize(NULL); //*
hres=CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&psl); //*
if(FAILED(hres)) ShowMessage("不能建立快捷方式");
GetWindowsDirectory(buf1,MAX_PATH);
lstrcat(buf1,"\\Start Menu\\Programs\\");
LoadString(myexe,10001,buf,MAX_PATH);
lstrcat(buf1,buf);
Form1->Memo1->Lines->Add("正在创建目录:"+AnsiString(buf1));
CreateDirectory(buf1,NULL);
for (int i=7001;i<=7000+StrToInt(AnsiString(num))*2;i+=2)
{ //快捷方式的资源ID从7001开始
if (cancel) break;
LoadString(myexe,i,buf1,MAX_PATH); //i 获取要创建快捷//方式的文件名
lstrcpy(buf,setuppath);
lstrcat(buf,buf1);
psl->SetPath(buf); //*
GetWindowsDirectory(buf1,MAX_PATH);
lstrcat(buf1,"\\Start Menu\\Programs\\");
LoadString(myexe,10001,buf,MAX_PATH);
lstrcat(buf1,buf);
lstrcat(buf1,"\\");
LoadString(myexe,i+1,buf,MAX_PATH); //i+1 获取快捷方//式的名称
lstrcat(buf1,buf);
lstrcat(buf1,".LNK"); //*
Form1->StatusBar1->SimpleText="正在创建快捷方式:"+AnsiString(buf1);
Form1->Memo1->Lines->Add("正在创建快捷方式:"+AnsiString(buf1));
hres=psl->QueryInterface(IID_IPersistFile,(void**)&ppf); //*
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,buf1, -1, wsz,MAX_PATH); //*
hres=ppf->Save(wsz,STGM_READWRITE); //*
}
ppf->Release(); //*
psl->Release(); //*
CoUninitialize(); //*
}
if(!cancel)
{ok=!ok;
cancel=!cancel;
Form1->StatusBar1->SimpleText="安装成功!";
Form1->Memo1->Lines->Add("安装成功!");
Form1->Button2->Enabled=false;
}
else
{Form1->StatusBar1->SimpleText="安装被用户终止!";
Form1->Memo1->Lines->Add("安装被用户终止!");
}
ExitThread(0);
return(true);
}
//-------------------------------------------------------------------------
unsigned int MySize(void) //从资源文件中获取安装文件的总大小
{
unsigned int setupsize=0;
char num[5];
char *p;
char *buf1=new char[MAX_PATH];
p=buf1;
num[0]=NULL;
LoadString(myexe,0,num,MAX_PATH);
if(num[0])
for (int i=1;i<=StrToInt(AnsiString(num));i++)
{
lstrcpy(p,"f");
lstrcat(p,IntToStr(i).c_str());
setupsize+=SizeofResource(myexe,FindResource(myexe,p,"MYRES"));
}
num[0]=NULL;
LoadString(myexe,6000,num,MAX_PATH);
if(num[0])
for (int i=6001;i<=6001+StrToInt(AnsiString(num));i++)
{
lstrcpy(p,"f");
lstrcat(p,IntToStr(i).c_str());
setupsize+=SizeofResource(myexe,FindResource(myexe,p,"MYRES"));
}
delete[] buf1;
return (setupsize);
}
//-------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
char *buf=new char[20];
EnumWindows((WNDENUMPROC)MyHideWinzip,0); //如果使用了WinZip Self-Extractor则隐藏之
allfilesize=MySize(); //获得被安装文件的总字节数
Label1->Caption=allfilesize;
Label1->Caption=Label1->Caption+" 字节";
Label2->Caption=FreeDiskSpace('c'); //获取c盘的自由空间字节数
Label2->Caption=Label2->Caption+" 字节";
Edit1->Text="c:\\program files\\";
myexe=GetModuleHandle(NULL); //获取资源句柄
LoadString(myexe,10000,buf,20); //获取安装目录
Edit1->Text=Edit1->Text+AnsiString(buf);
Button2->Enabled=false;
delete[] buf;
}
//-------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
cancel=false;
DWORD threid;
lstrcpy(setuppath,Edit1->Text.c_str());
if(setuppath[lstrlen(setuppath)-1]!='\\') lstrcat(setuppath,"\\");
CreateThread(NULL,0,MyThreid, (LPVOID)setuppath,0,&threid);
//生成线程,开始复制文件。
//单独生一线程拷贝文件是必须的,在长操作中,它可以保证主程序对输入正常响应,可使终止操作及时得到响应
Form1->Button2->Enabled=true;
Form1->Button1->Enabled=false;
}
//-------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1->Button1->Enabled=true;
Form1->Button2->Enabled=false;
cancel=true; //设置中止标志
}
//-------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Form2->ShowModal();
}
//-------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(!ok) ShowMessage("!! 安装没有完成\n请重新运行安装程序");
Form1->Close();
}
//-------------------------------------------------------------------------
Unit2.cpp的内容如下,黑斜体为自己加入的代码,其它为C++Builder4自动生成的。
//---------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include "Unit1.h" //可以访问Form1
//-------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------void __fastcall TForm2::DriveComboBox1Change(TObject *Sender)
{
DirectoryListBox1->Drive=DriveComboBox1->Drive;
}
//---------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
char buf[20];
HWND myexe;
Form1->Edit1->Text=Label1->Caption;
myexe=GetModuleHandle(NULL);
LoadString(myexe,10000,buf,sizeof(buf));
if (*(Label1->Caption.c_str()+Label1->Caption.Length()-1)=='\\')
Form1->Edit1->Text=Form1->Edit1->Text+AnsiString(buf);
else
Form1->Edit1->Text=Form1->Edit1->Text+"\\"+AnsiString(buf);
Form1->Label2->Caption=Form1->FreeDiskSpace(DriveComboBox1->Drive);
Form1->Label2->Caption=Form1->Label2->Caption+" 字节";
Form2->Close();
}
//---------------------------------------------------------------------void __fastcall TForm2::Button2Click(TObject *Sender)
{
Form2->Close();
}
//---------------------------------------------------------------------
|