Search Unity

Passing parentHWND to UnityMain function

Discussion in 'Windows' started by benjicoh, Feb 15, 2018.

  1. benjicoh

    benjicoh

    Joined:
    Dec 21, 2014
    Posts:
    5
    Hi guys,

    I'm trying to do the following in windows standalone build (2017.3) :

    1) Launch unity from my process as explained in here
    https://docs.unity3d.com/Manual/WindowsStandaloneBinaries.html

    2) pass the UnityMain function -parentHWND with my handle

    I don't get it to work, it simply ignores the -parentHWND parameter

    Any ideas ?

    Thanks, Benny
     
  2. benjicoh

    benjicoh

    Joined:
    Dec 21, 2014
    Posts:
    5
    Ok, figured it out in case anyone else has the same problem

    1) get a pointer to the command line string by using GetCommandLine()
    2) modify the string (same pointer address) to use the "-parentHWND <your handle>"
    3) call UnityMain(0,0,L"", 10) // 10 is for normal window state

    And a snippet
    Code (csharp):
    1.  
    2. #include "stdafx.h"
    3. #include <string>
    4.  
    5. //Make sure you call this process with command line arguments "-parentHWND 0000000000"
    6.  
    7. typedef int (*unityFN)(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd);
    8. void LaunchUnity(HWND hwnd)
    9. {
    10.     const int hwndParamsSize = 10;
    11.     const int showCmd = 10;
    12.     auto dllHandle = LoadLibrary(L"UnityPlayer.dll");
    13.     auto funcHandle = GetProcAddress(dllHandle, "UnityMain");
    14.     unityFN unityFunc = (unityFN)funcHandle;
    15.  
    16.     auto cmd = GetCommandLine();
    17.     auto hwndStr = std::to_wstring((int)hwnd);
    18.     auto cmdStr = std::wstring(cmd);
    19.     int i = cmdStr.size() - hwndParamsSize;
    20.     for (int j = 0; i < cmdStr.size() - hwndParamsSize + hwndStr.size(); i++, j++)
    21.     {
    22.         cmd[i] = hwndStr[j];
    23.     }
    24.     cmd[i] = 0;
    25.  
    26.     unityFunc(0, 0, L"", showCmd);
    27. }
    28.  
    Hope it'll help someone, Benny
     
    Last edited: Feb 18, 2018
    OliverJackman likes this.