Unity中嵌入.exe文件

1,在Unity中嵌入另一个软件,主要方法是调用Win32系统Api的方法

  1)、用Process.Start启动一个进程,把该进程存为process

  2)、用win32方法FindWindow寻找process窗口句柄,SetParent把当前窗口设为process窗口的父窗口,SetWindowLong去掉process窗口的标题栏,SetWindowPos设置process窗口的位置

using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;

public class TestEmbedExe : MonoBehaviour 
{
    #region Win32 Api
    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, int hWndInserAfter, int x, int y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll")]
    static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, long dwNewLong);

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string className, string windowName);

    [DllImport("user32.dll")]
    private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    #endregion

    OpenFileName fileInfo;
    Process process;

    private void Start() {
        Screen.SetResolution(1600, 900, false);
    }

    private void OnGUI() {
        if (GUILayout.Button("Open")) {
            fileInfo = FileOperate.GetOpenFile();
            if(fileInfo != null) {
                if(process != null) {
                    if (!process.HasExited)
                        process.Kill();
                    process = null;
                }
                ProcessStartInfo info = new ProcessStartInfo(fileInfo.file);
                process = Process.Start(info);
                process.WaitForInputIdle();
                StartCoroutine(Wait());
                //IntPtr win = FindWindow(null, process.ProcessName);
                //SetWindowLong(win, -16, 1);
                //SetParent(win, GetForegroundWindow());
                //SetWindowPos(win, 0, 0, 0, 300, 200, 0x0040);
            }
        }
        if (GUILayout.Button("去边框")) {
                IntPtr win = FindWindow(null, process.ProcessName);
                SetWindowLong(win, -16, 1); 
        }
    }

    IEnumerator Wait() {
        yield return 0;
        IntPtr win = FindWindow(null, process.ProcessName);
        SetParent(win, GetForegroundWindow());
        yield return new WaitForSeconds(0.5f);
        SetWindowLong(win, -16, 0x00800000L);
        SetWindowPos(win, 0, 20, 20, 300, 200, 0x0040);
    }

    private void OnDestroy() {
        if (process != null) {
            if (!process.HasExited)
                process.Kill();
            process = null;
        }
    }
}

2,本例中用到调用windows资源管理器功能,代码如下

using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;

public class FileOperate {

    private void OnGUI() {
        if (GUILayout.Button("OpenDialog")) {
            OpenFileName ofn = new OpenFileName();
            ofn.structSize = Marshal.SizeOf(ofn);
            ofn.filter = "All Files(.json)\0*.json\0\0";
            ofn.file = new string(new char[256]);
            ofn.maxFile = ofn.file.Length;
            ofn.fileTitle = new string(new char[64]);
            ofn.maxFileTitle = ofn.fileTitle.Length;
            ofn.initialDir = Application.dataPath;
            ofn.title = "open";
            ofn.defExt = "jpg";
            ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
            if (GetOpenFileName1(ofn)) {
                Debug.Log(ofn.file);
            }
        }
    }

    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out]OpenFileName ofn);

    public static bool GetOpenFileName1([In, Out]OpenFileName ofn) {
        return GetOpenFileName(ofn);
    }

    public static OpenFileName GetOpenFile() {
        OpenFileName ofn = new OpenFileName();
        ofn.structSize = System.Runtime.InteropServices.Marshal.SizeOf(ofn);
        ofn.filter = "All Files()\0*.*\0\0";
        ofn.file = new string(new char[256]);
        ofn.maxFile = ofn.file.Length;
        ofn.fileTitle = new string(new char[64]);
        ofn.maxFileTitle = ofn.fileTitle.Length;
        ofn.initialDir = Application.streamingAssetsPath;
        ofn.title = "open";
        ofn.defExt = "jpg";
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        GetOpenFileName1(ofn);
        return ofn;
    }
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName {
    public int structSize = 0;
    public IntPtr dlgOwner = IntPtr.Zero;
    public IntPtr instance = IntPtr.Zero;
    public string filter = null;
    public string custonFilter = null;
    public int maxCustFilter = 0;
    public int filterIndex = 0;
    public string file = null;
    public int maxFile = 0;
    public string fileTitle = null;
    public int maxFileTitle = 0;
    public string initialDir = null;
    public string title = null;
    public int flags = 0;
    public short fileOffect = 0;
    public short fileExtension = 0;
    public string defExt = null;
    public IntPtr custData = IntPtr.Zero;
    public IntPtr hook = IntPtr.Zero;
    public string templateName = null;
    public IntPtr reservedPtr = IntPtr.Zero;
    public int reservedInt = 0;
    public int flagsEx = 0;
}

 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值