RE: windows installer - vbs vs bat startup script

"JohnD" <[email protected]>
Newsgroups gmane.comp.gnu.octave.maintainers
Message-ID <[email protected]>
 

 

From: Ian McCallion [mailto:[email protected]] 
Sent: Friday, August 28, 2020 3:24 AM
To: JohnD
Cc: John W. Eaton; Nicholas Jankowski; octave-maintainers
Subject: Re: windows installer - vbs vs bat startup script

 

On Friday, 28 August 2020, JohnD <[email protected]> wrote:



> -----Original Message-----
> From: John W. Eaton [mailto:[email protected]]
> Sent: Thursday, August 27, 2020 11:49 AM
> To: Nicholas Jankowski; JohnD
> Cc: octave-maintainers
> Subject: Re: windows installer - vbs vs bat startup script
> 
> On 8/27/20 11:44 AM, Nicholas Jankowski wrote:
> >     I wonder if the issue is trying to run the .vbs script, or trying to
> >     run a program as hidden from another program ?____
> >
> >     __ __
> >
> >     If it just the general act of running a .vbs script , we could just
> >     a small program using activex and call almost the same commands
> >     from within it.
> >
> >
> > if the issue with vbs files is the various, potentially conflicting,
> > non-standard security protocols,  is activex any better? I wasn't aware
> > it was supported outside of a browser environment.
> 
> Does it need to be a script or could the same job be done with a
> separate .exe program file that sets up the environment appropriately
> and then execs the real Octave binary?
> 
> jwe


Heres a launcher exe code that looks like will work. See comments in file for compiling.

It probally needs a little more work to ensure things don’t buffer run, and doesn’t do much error checking

It assumes its in the same directory as the octave executable and works out the paths from that.

Its pretty close to what is being done in the.vbs scrip, but in a different order for parts

 

Looks good John. If I may suggest octave-launcher.exe is always installed in the root folder for the release, independently of where the main exes get installed. This will avoid a potential cause of breakage between releases for user code that launches octave.

 

Cheers... Ian

 

 

Ok modded to expect to be in the root of the install directory where octave.vbs currently would reside.
octave-launch.c (text/plain, 6.9 KB)
/*
 * Wrapper application to set octave env variables and then run octave
 * compile with gcc -o octave-launch octave-launch.c -Wl,--subsystem,windows -lshlwapi
 *
 * Copyright (C) 2020 John Donoghue <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http: *www.gnu.org/licenses/>.
*/

#include <windows.h>
#include <shlwapi.h>

int ParentDir(char *dir)
{
  int len = strlen(dir);
  while(len > 0 && dir[len] != '\\') len --;
  dir[len] = 0;
  return len;
}

char * FilePart(char *dir)
{
  int len = strlen(dir);
  while(len > 0 && dir[len-1] != '\\') len --;
  return &dir[len];
}

int main(int argc, char **argv)
{
  PROCESS_INFORMATION pi;
  char msyspath[1024];
  char rootpath[1024];
  char path[1024];
  char binpath[1024];
  int gui_mode = -1;
  int i;

  /* get path of us which we will assume is the root of the install */
  
  DWORD nSize = GetModuleFileName(NULL, path, sizeof(path)-1);
  if (nSize)
    {
      while(nSize > 0 && path[nSize] != '\\')
        nSize --;
      path[nSize] = '\0';
    }

  nSize = GetShortPathName(path, rootpath, sizeof(rootpath)-1);
  if (nSize == 0)
    {
      StrCpy(rootpath, path);
    }

  SetEnvironmentVariable("MXE_ROOT", rootpath); 


  /* get msys path and set env */

  StrCpy(msyspath, rootpath);

  StrCpy(path, rootpath);
  StrCat(path, "\\mingw64\\bin\\octave-cli.exe");
  if (PathFileExists(path))
    {
      SetEnvironmentVariable("MSYSTEM", "MINGW64"); 
      StrCat(msyspath, "\\usr");
      SetEnvironmentVariable("MSYSPATH", msyspath); 

      StrCat(msyspath, "\\bin");

      StrCpy(binpath, rootpath);
      StrCat(binpath, "\\mingw64\\bin");
    }
  else 
    {
      StrCpy(path, rootpath);
      StrCat(path, "\\mingw32\\bin\\octave-cli.exe");
      if (PathFileExists(path))
        {
          SetEnvironmentVariable("MSYSTEM", "MINGW32"); 
          StrCat(msyspath, "\\usr");
          SetEnvironmentVariable("MSYSPATH", msyspath); 

          StrCat(msyspath, "\\bin");

          StrCpy(binpath, rootpath);
          StrCat(binpath, "\\mingw32\\bin");
	}
      else
        {
          SetEnvironmentVariable("MSYSTEM", "MSYS"); 
          SetEnvironmentVariable("MSYSPATH", msyspath); 

          StrCat(msyspath, "\\bin");

          StrCpy(binpath, rootpath);
          StrCat(binpath, "\\bin");
        }
    }

  /* binpath is to the octave bin dir so get the parent */
  StrCpy(path, binpath);
  ParentDir(path);

 #ifdef SHOW_PATHS
  MessageBox(NULL, rootpath, "octroot", MB_OK);
  MessageBox(NULL, path, "octhome", MB_OK);
  MessageBox(NULL, binpath, "octbin", MB_OK);
  MessageBox(NULL, msyspath, "msysbin", MB_OK);
 #endif

  /* now at the octave home */
  SetEnvironmentVariable("OCTAVE_HOME", path); 

  /* qt paths 
   * either %OCT_HOME%\qt5\plugins
   * or %OCT_HOME\plugins
   */
  nSize = strlen(path);
  StrCat(path, "\\qt5\\plugins");
  if (! PathFileExists(path))
    {
      path[nSize] = '\0';
      StrCat(path, "\\plugins");
    }
  SetEnvironmentVariable("QT_PLUGIN_PATH", path); 

  path[nSize] = '\0';

  /* exe paths */
  {
    char pathbuffer[8096];
    char newpathbuffer[8096];

    nSize = GetEnvironmentVariable("PATH", pathbuffer, sizeof(pathbuffer)-1);
    pathbuffer[nSize] = '\0';

    /* set our paths first */
    StrCpy(newpathbuffer, binpath);
    StrCat(newpathbuffer, ";");
    StrCat(newpathbuffer, msyspath);
    StrCat(newpathbuffer, ";");
    StrCat(newpathbuffer, pathbuffer);
  
    SetEnvironmentVariable("PATH", newpathbuffer); 
  }

  /* other env */
  SetEnvironmentVariable("TERM", "cygwin"); 
  SetEnvironmentVariable("GNUTERM", "wxt"); 
  SetEnvironmentVariable("GS", "gs.exe"); 

  /* home set */
  nSize = GetEnvironmentVariable("HOME", path, sizeof(path)-1);
  if (nSize == 0 || path[0] == 0)
    {
      char newhome[1024];

      nSize = GetEnvironmentVariable("USERPROFILE", path, sizeof(path)-1);
      if (nSize == 0 || path[0] == 0)
        {
          /* build dir from drive and path */
          char tmpbuff[1024];
          nSize = GetEnvironmentVariable("HOMEDRIVE", tmpbuff, sizeof(tmpbuff)-1);
          if (nSize)
            StrCpy (path, tmpbuff);
          else
            path[0] = '\0';

          nSize = GetEnvironmentVariable("HOMEPATH", tmpbuff, sizeof(tmpbuff)-1);
          if(nSize) StrCat(path, tmpbuff);
        }

      nSize = GetShortPathName(path, newhome, sizeof(newhome)-1);
  
      if (nSize == 0)
        {
          StrCpy(newhome, path);
        }

      SetEnvironmentVariable("HOME", newhome);
    }

  /* check for gui mode */
  for (i=1; i<argc; i++)
    {
      if (StrCmp(argv[i], "--no-gui") == 0)
        gui_mode = 0;
      else if (StrCmp(argv[i], "--gui") == 0)
        gui_mode = 1;
      else if (StrCmp(argv[i], "--force-gui") == 0)
        gui_mode = 1;
    }

  /* set up process args and start it */
  {
    STARTUPINFO si;
    char argbuffer[4096];

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    StrCpy(path, binpath);

    if(gui_mode == -1)
      {
        /* no mode specified, so set gui */
        StrCpy(argbuffer, "octave-gui.exe --gui ");
        StrCat(path, "\\octave-gui.exe");
        si.dwFlags = STARTF_USESHOWWINDOW;
      }
    else if (gui_mode == 1)
      {
        StrCpy(argbuffer, "octave-gui.exe ");
        StrCat(path, "\\octave-gui.exe");
        si.dwFlags = STARTF_USESHOWWINDOW;
      }
    else
      {
        StrCpy(argbuffer, "octave-cli.exe ");
        StrCat(path, "\\octave-cli.exe");
      }

    /* quote and append each arg */
    for (i=1; i<argc; i++)
      {
        StrCat(argbuffer, "\"");
        StrCat(argbuffer, argv[i]);
        StrCat(argbuffer, "\" ");
      }

    /* Start the child process */
    if (! CreateProcess(path,// module name
        argbuffer,           // Command line
        NULL,                //  Process handle not inheritable
        NULL,                // Thread handle not inheritable
        FALSE,               //  Set handle inheritance to FALSE
        0,                   // No creation flags
        NULL,                // Use parent's environment block
        NULL,                // Use parent's starting directory 
        &si,                 // Pointer to STARTUPINFO structure
        &pi )                //Pointer to PROCESS_INFORMATION structure
    ) return 1;
  }

  /* Wait until child process exits. */
  WaitForSingleObject( pi.hProcess, INFINITE );

  /* Close process and thread handles */
  CloseHandle( pi.hProcess );
  CloseHandle( pi.hThread );

  return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.