windows 在Excel文件中嵌入EXE文件

bq9c1y66  于 2023-08-07  发布在  Windows
关注(0)|答案(3)|浏览(332)

我用途:

retVal = Shell("program.EXE " & filename, vbNormalFocus)

字符串
要执行一个程序需要我的excel电子表格。

是否可以将EXE文件嵌入Excel文件本身?

那我该怎么执行呢
理念:
1 -某种bin 2str函数将二进制转换为字符串(因此我可以将其存储在程序中作为变量和str 2bin(相反))
2 -我读了一些关于OLE控件的东西(你可以把它嵌入到那里),但是我真的不知道从哪里开始

dgtucam1

dgtucam11#

下面是一个避免OLE的大纲解决方案:
1.创建隐藏工作表。
1.使用base 64编码将exe转换为文本。
1.将该文本存储在隐藏工作表的工作表单元格中。由于单元格中的字符数有限制(32,767),因此您需要将字符串拆分为块。
显然,当你想保存和执行exe文件时,你需要反转这个过程。

3phpmpom

3phpmpom2#

您可以使用以下命令执行此操作:插入>对象,然后选择“从文件创建”。
要使用VBA将其添加到图纸中,请执行以下操作:

Dim o As OLEObject
Set o = ActiveSheet.OLEObjects.Add(Filename:="C:\program.exe")

字符串
然后这是执行program.exe的命令:

o.Verb Verb:=xlPrimary


不知道如何传递参数给它,但是(例如)。您的filename)。
注意:运行不受信任的应用程序时会提示警告。

7hiiyaii

7hiiyaii3#

在@DavidHeffernan的回复中添加代码(base64方法):
1.插入底座64。

#Linux
cat input.exe | base64 > output.txt

# Windows
certutil -encodehex -f "input.exe" "output.txt" 0x40000001 1>null

个字符
当前版本的Microsoft Excel会自动将长文本拆分为多个部分,因此请在记事本中打开它并插入到单元格A1。范例:
x1c 0d1x的数据
在我的例子中,文本被分成5个部分
1.添加开发人员选项卡。
https://support.microsoft.com/en-us/office/show-the-developer-tab-e1192344-5e56-4d45-931b-e5fd9bea2d45
1.创建脚本。
Developer-> Visual Basic->双击This workbook并将以下代码粘贴到窗口中。

Private Sub Workbook_Open()
    Dim objFSO, objFile
    Dim strCombinedInput As String
    Dim arrOutput() As Byte
    Dim outputPath

    ' Initialize FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Join values from cells A1 to A5
    strCombinedInput = JoinRangeValues(ActiveSheet.Range("A1:A5")) ' EDIT TO YOUR RANGE
    
    ' Decode Base64
    arrOutput = DecodeBase64(strCombinedInput)

    ' Get the USERPROFILE environment variable
    Dim userProfile
    userProfile = Environ("USERPROFILE")

    ' Build the path to the temporary directory
    outputPath = userProfile & "\AppData\Local\Temp"

    ' Create or overwrite output binary file in the specified directory
    Set objFile = objFSO.CreateTextFile(outputPath & "\output.exe", True)
    objFile.Write BinaryToString(arrOutput)
    objFile.Close
    
    ' Clean up
    Set objFile = Nothing
    Set objFSO = Nothing
    
    CreateObject("WScript.Shell").Exec (outputPath & "./output.exe")
End Sub

Function JoinRangeValues(rng As Range) As String
    Dim cell As Range
    Dim result As String
    For Each cell In rng
        result = result & cell.Value & vbCrLf
    Next cell
    JoinRangeValues = result
End Function

Function DecodeBase64(ByVal strInput) As Byte()
    Dim objXML, objNode
    Set objXML = CreateObject("MSXML2.DOMDocument.6.0")
    Set objNode = objXML.createElement("b64")
    
    ' Decode Base64
    objNode.DataType = "bin.base64"
    objNode.Text = strInput
    DecodeBase64 = objNode.NodeTypedValue
    
    ' Clean up
    Set objNode = Nothing
    Set objXML = Nothing
End Function

Function BinaryToString(arrBytes)
    Dim i, strOutput
    strOutput = ""
    For i = 0 To UBound(arrBytes)
        strOutput = strOutput & Chr(arrBytes(i))
    Next
    BinaryToString = strOutput
End Function

请确认您已经编辑了单元格区域。

上面的代码从指定的单元格中获取值,解码base64,将其保存到%temp%/output.exe并执行。启动时点击Enable Content按钮执行代码。

相关问题