给你贴上我写的一个单元,包括 Swf<->EXE。。。
unit FlashConv;
interface
uses
Windows, Classes, SysUtils, Forms;
type
TFileType=(ftSelf,ftExe,ftOtherExe,ftOldVerExe,ftSwf,ftSelfPlayer,ftPlayer,ftOtherPlayer,ftOldVerPlayer,ftOther);
TCovState=(csSucc,csFailed,csOldVersion,
csSourceFileNotFound,csDestFileNotFound,csLinkerFileNotFound,
csInvalidPlayer,csInvalidSwf,csInvalidExe);
TFileInfo = packed record
CommpanyName: string;
FileDescription: string;
FileVersion: string;
InternalName: string;
LegalCopyright: string;
LegalTrademarks: string;
OriginalFileName: string;
ProductName: string;
ProductVersion: string;
Comments: string;
VsFixedFileInfo:VS_FIXEDFILEINFO;
UserDefineValue:string;
end;
function isSwfExe(FileName:String):TFileType;
function ExeToSwf(Src,Dest:string;Ignore:Boolean=True):TCovState;
function SwfToExe(Src,Dest:String;Linker:string=''):TCovState;
function ExeToPlayer(Src:String;var Dest:String;Ignore:Boolean=True):TCovState;
procedure GetConvErrMsg(csState: TCovState);
function GetFileVerInfo(const FileName: string;
var info: TFileInfo;UserDefine:string=''):boolean;
var
TempFile: String;
DestFile: String;
theFileInfo: String;
Version: String;
HelpString: String;
CurrentVersion: Integer;
implementation
function isSwfExe(FileName:String):TFileType;
var
FileStream: TFileStream;
Flag: Integer;
FileInfo: TFileInfo;
FlashPlayFlag: Array[0..13] of Char;
begin
Result:=ftOther;
FileStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
try
with FileStreamdo
begin
Seek(0,soFrombegin
ning);
ReadBuffer(Flag,SizeOf(Integer));
Flag:=Flag and $00FFFFFF;
if Flag=$00535746 then
//.swf文件开始部分标志
begin
Result:=ftSwf;
//是 .swf 文件
exit;
end;
GetFileVerInfo(FileName,FileInfo,'播放器标志');
StrLCopy(FlashPlayFlag,PChar(FileInfo.FileDescription+' '),12);
if (UpperCase(ExtractFileExt(FileInf
riginalFileName))<>'.OCX') and (UpperCase(FlashPlayFlag)='FLASH PLAYER') then
Result:=ftPlayer
else
if FileInfo.UserDefineValue='Flash 影院' then
begin
GetFileVerInfo(FileName,FileInfo,'支持版本');
if CurrentVersion<StrToInt(FileInfo.UserDefineValue) then
Result:=ftOldVerPlayer
else
Result:=ftSelfPlayer;
end
else
if UpperCase(ExtractFileExt(FileName))='.EXE' then
Result:=ftOtherPlayer;
{ else
begin
Result:=ftOther;
exit;
end;
}
Seek(-2*Sizeof(Integer),soFromEnd);//移至文件末
ReadBuffer(Flag,Sizeof(Integer));//并读取文件标志
if Flag=Integer($FA123456) then
//如果读到的文件标志与FA123456相同
begin
case Result of
ftPlayer: Result:=ftExe;
ftSelfPlayer: Result:=ftSelf;
ftOtherPlayer:Result:=ftOtherExe;
ftOldVerPlayer: Result:=ftOldVerExe;
else
Result:=ftOther;
end;
end;
end;
finally
FileStream.Free;
end;
end;
function ExeToSwf(Src,Dest:string;Ignore:Boolean=True):TCovState;
var
SrcStream,DestStream: TFileStream;
SwfFileSize: Cardinal;
tFT: TFileType;
begin
Result:=csFailed;
if not FileExists(Src) then
begin
Result:=csSourceFileNotFound;
exit;
end;
if not Ignore then
begin
tFT:=isSwfExe(Src);
if not (tFt in [ftSelf,ftExe,ftOtherExe]) then
begin
Result:=csInvalidExe;
exit;
end
else
if tFT=ftOldVerExe then
begin
Result:=csOldVersion;
exit;
end;
end;
SrcStream:=TFileStream.Create(Src,fmOpenRead or fmShareDenyWrite);
try
with SrcStreamdo
begin
Seek(-2*SizeOf(Integer),soFromEnd);
ReadBuffer(SwfFileSize,SizeOf(Integer));
if SwfFileSize=$FA123456 then
begin
ReadBuffer(SwfFileSize,SizeOf(SwfFileSize));
Seek(-SwfFileSize-2*SizeOf(Integer),sofromend);
DestStream:=TFileStream.Create(Dest,fmCreate);
try
DestStream.CopyFrom(SrcStream,SwfFileSize);
Result:=csSucc;
finally
DestStream.Free;
end;
end;
end;
finally
SrcStream.Free;
end;
end;
function SwfToExe(Src,Dest:String;Linker:string=''):TCovState;
var
SrcStream,DestStream,LinkStream: TFileStream;
Flag: Cardinal;
SwfFileSize: Integer;
LinkFile: String;
tState: TCovState;
tFT: TFileType;
begin
Result:=csFailed;
if not FileExists(Src) then
begin
Result:=csSourceFileNotFound;
exit;
end;
if isSwfExe(Src)<>ftSwf then
begin
Result:=csInvalidSwf;
exit;
end;
DestStream:=TFileStream.Create(Dest,fmCreate);
try
if (Linker='') or (Linker=Application.ExeName) then
begin
tState:=ExetoPlayer(Application.ExeName,DestFile);
if tState=csSucc then
LinkFile:=DestFile
else
begin
Result:=tState;
exit;
end;
end
else
begin
tFT:=isSwfExe(Linker);
case tFT of
ftSelf,ftExe,ftOtherExe:
begin
tState:=ExetoPlayer(Application.ExeName,DestFile);
if tState=csSucc then
LinkFile:=DestFile
else
begin
Result:=tState;
exit;
end
end;
ftOldVerExe:
begin
Result:=csOldVersion;
exit;
end;
ftSelfPlayer,ftPlayer,ftOldVerPlayer:
LinkFile:=Linker;
ftOtherPlayer:
begin
if MessageBox(Application.Handle,PChar('未知类型的可执行文件,您确认是一个 Flash 播放器吗?'+#13#13'按 “确定”继续转换,按“取消”放弃。'),'提示',MB_OKCANCEL+MB_ICONINFORMATION+MB_SYSTEMMODAL)=ID_CANCEL then
exit
else
LinkFile:=Linker;
end;
else
begin
Result:=csInvalidPlayer;
exit;
end;
end;
end;
if not FileExists(LinkFile) then
begin
Result:=csLinkerFileNotFound;
exit;
end;
Linkstream:=TFileStream.Create(LinkFile,fmOpenRead or fmShareDenyWrite);
try
DestStream.CopyFrom(Linkstream,0);
finally
LinkStream.Free;
end;
SrcStream:= TFilestream.Create(Src,fmOpenRead or fmShareDenyWrite);
try
DestStream.CopyFrom(SrcStream,0);
Flag:=$FA123456;
DestStream.WriteBuffer(Flag,SizeOf(Integer));
SwfFileSize:=SrcStream.Size;
DestStream.WriteBuffer(SwfFileSize,SizeOf(Integer));
Result:=csSucc;
finally
SrcStream.Free;
end;
finally
DestStream.Free;
end;
end;
function ExeToPlayer(Src:String;var Dest:String;Ignore:Boolean=True):TCovState;
var
SrcStream,DestStream: TFileStream;
SwfFileSize: Cardinal;
tFT: TFileType;
begin
Result:=csFailed;
if not FileExists(Src) then
begin
Result:=csSourceFileNotFound;
exit;
end;
if not Ignore then
begin
tFT:=isSwfExe(Src);
case tFT of
ftSelfPlayer,ftPlayer,ftOldVerPlayer,ftOtherPlayer:
begin
Result:=csSucc;
exit;
end;
ftOther,ftSwf:
begin
Result:=csInvalidPlayer;
exit;
end;
ftOldVerExe:
begin
Result:=csOldVersion;
exit;
end;
end;
end;
SrcStream:=TFileStream.Create(Src,fmOpenRead or fmShareDenyWrite);
try
with SrcStreamdo
begin
Seek(-1*SizeOf(Integer),soFromEnd);
ReadBuffer(SwfFileSize,SizeOf(Integer));
Seek(0,soFrombegin
ning);
SwfFileSize:=Size-LongInt(SwfFileSize)-2*SizeOf(Integer);
DestStream:=TFileStream.Create(Dest,fmCreate);
try
DestStream.CopyFrom(SrcStream,SwfFileSize);
Result:=csSucc;
finally
DestStream.Free;
end;
end;
finally
SrcStream.Free;
end;
end;
procedure GetConvErrMsg(csState: TCovState);
var
tmpStr:String;
begin
case csState of
csSucc: tmpStr:='转换成功!';
csFailed: tmpStr:='转换失败!';
csSourceFileNotFound: tmpStr:='源文件未找到!';
csDestFileNotFound: tmpStr:='目标文件未找到!';
csLinkerFileNotFound: tmpStr:='播放器文件未找到!';
csInvalidPlayer: tmpStr:='无效的播放器文件!';
csInvalidSwf: tmpStr:='无效的 SWF 文件!';
csInvalidExe: tmpStr:='无效的 Flash EXE 文件!';
csOldVersion: tmpStr:='不支持的高版本 Flash 影院 文件!';
end;
MessageBox(Application.Handle,PChar(tmpStr),'结果',mb_OK+mb_ICONINFORMATION+mb_SYSTEMMODAL);
end;
function GetFileVerInfo(const FileName: string;
var info: TFileInfo;UserDefine:string=''):boolean;
const
SFInfo= '/StringFileInfo/';
var
VersionInfo: Pointer;
InfoSize: DWORD;
InfoPointer: Pointer;
Translation: Pointer;
VersionValue: string;
unused: DWORD;
begin
unused := 0;
Result := False;
InfoSize := GetFileVersionInfoSize(pchar(FileName), unused);
if InfoSize > 0 then
begin
GetMem(VersionInfo, InfoSize);
Result := GetFileVersionInfo(pchar(FileName), 0, InfoSize, VersionInfo);
if Result then
begin
VerQueryValue(VersionInfo, '/VarFileInfo/Translation', Translation, InfoSize);
VersionValue := SFInfo + IntToHex(LoWord(Longint(Translation^)), 4) + IntToHex(HiWord(Longint(Translation^)), 4) + '/';
VerQueryValue(VersionInfo, pchar(VersionValue + 'CompanyName'), InfoPointer, InfoSize);
info.CommpanyName := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'FileDescription'), InfoPointer, InfoSize);
info.FileDescription := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'FileVersion'), InfoPointer, InfoSize);
info.FileVersion := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'InternalName'), InfoPointer, InfoSize);
info.InternalName := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'LegalCopyright'), InfoPointer, InfoSize);
info.LegalCopyright := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'LegalTrademarks'), InfoPointer, InfoSize);
info.LegalTrademarks := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'OriginalFileName'), InfoPointer, InfoSize);
inf
riginalFileName := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'ProductName'), InfoPointer, InfoSize);
info.ProductName := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'ProductVersion'), InfoPointer, InfoSize);
info.ProductVersion := string(pchar(InfoPointer));
VerQueryValue(VersionInfo, pchar(VersionValue + 'Comments'), InfoPointer, InfoSize);
info.Comments := string(pchar(InfoPointer));
if VerQueryValue(VersionInfo, '/', InfoPointer, InfoSize) then
info.VsFixedFileInfo := TVSFixedFileInfo(InfoPointer^);
if UserDefine<>'' then
begin
if VerQueryValue(VersionInfo,pchar(VersionValue+UserDefine),InfoPointer,InfoSize) then
info.UserDefineValue:=string(pchar(InfoPointer));
end;
end;
FreeMem(VersionInfo);
end;
end;
end.