January 15, 2021, 06:34:26 PM

Author Topic: [bmx] CreateShortcut() by Canardian [ 1+ years ago ]  (Read 614 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] CreateShortcut() by Canardian [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : CreateShortcut()
Author : Canardian
Posted : 1+ years ago

Description : Creates a Windows Explorer Shortcut file.

In addition to the BlitzMax source, a C++ source is also needed, which is here:
xsystem.cpp:
Code: [Select]
#include "windows.h"

extern "C" {

int xsystem( const char *args ){
int res;
STARTUPINFO si={sizeof(si)};
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

PROCESS_INFORMATION pi={0};

if( !CreateProcess( 0,(char*)args,0,0,1,0,0,0,&si,&pi ) ) return -1;

WaitForSingleObject( pi.hProcess,INFINITE );

res=GetExitCodeProcess( pi.hProcess,(DWORD*)&res ) ? res : -1;

CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

return res;
}

}


Code :
Code: BlitzMax
  1. Strict
  2. Import "xsystem.cpp"
  3. CreateShortcut(GetEnv("USERPROFILE")+"Start MenuMy Program.lnk","c:windows
  4. otepad.exe","My Program","CTRL+ALT+SHIFT+X","notepad.exe, 0","","c:")
  5. End
  6.  
  7.  
  8.  
  9. Extern "C"
  10.         Function xsystem(command:Byte Ptr)
  11. EndExtern
  12.  
  13. Function CreateShortcut(linkfile$,exefile$,title$,hotkey$,icon$,exeargs$,workdir$)
  14.         Local filename$=GetEnv("TEMP")+"creascut.vbs"
  15.         Local f:TStream
  16.         f=WriteStream(filename)
  17.         WriteLine(f,"set objWSHShell = CreateObject(~qWScript.Shell~q)")
  18.         WriteLine(f,"set objSC = objWSHShell.CreateShortcut(~q"+linkfile+"~q)")
  19.         WriteLine(f,"objSC.Description = ~q"+title+"~q")
  20.         WriteLine(f,"objSC.HotKey = ~q"+hotkey+"~q")
  21.         WriteLine(f,"objSC.IconLocation = ~q"+icon$+"~q")  ' 0 is the index
  22.         WriteLine(f,"objSC.TargetPath = ~q"+exefile+"~q")
  23.         WriteLine(f,"objSC.Arguments = ~q"+exeargs+"~q")
  24.         WriteLine(f,"objSC.WindowStyle = 1")   ' 1 = normal; 3 = maximize window; 7 = minimize
  25.         WriteLine(f,"objSC.WorkingDirectory = ~q"+workdir+"~q")
  26.         WriteLine(f,"objSC.Save")
  27.         CloseStream(f)
  28.         xsystem("cscript.exe ~q"+filename+"~q")
  29.         DeleteFile filename
  30. EndFunction
  31.  
  32.  
  33.  
  34. Extern "Win32"
  35.         Function GetEnvironmentVariable(lpName$z, lpBuffer:Byte Ptr, nSize) = "GetEnvironmentVariableA@12"
  36. End Extern
  37.  
  38. Function GetEnv$(envVar$)
  39.         Local buff@[64]
  40.         Local rtn = GetEnvironmentVariable(envVar$, buff@, buff.length)
  41.         If rtn > buff.length
  42.                 buff@ = buff@[..rtn]
  43.                 rtn = GetEnvironmentVariable(envVar$, buff@, buff.length)
  44.         EndIf
  45.         Return String.FromBytes(buff@, rtn)
  46. End Function


Comments :


Jesse(Posted 1+ years ago)

 thanks for sharing. I like it. Curious, Does it work on Vista?


Canardian(Posted 1+ years ago)

 Yes, it has been tested on Vista too, and it works fine there.


xlsior(Posted 1+ years ago)

 Of course it does appear to depend on the system being able to run straight .VBS scripts, which isn't necesarily always the case... (I've seen more than my fair share of systems where for one way or another .VBS files wouldn't run at all)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal