Code archives/File Utilities/CreateShortcut()

This code has been declared by its author to be Public Domain code.

Download source code

CreateShortcut() by Canardian2008
Creates a Windows Explorer Shortcut file.

In addition to the BlitzMax source, a C++ source is also needed, which is here:
xsystem.cpp:
#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;
}

}
Strict
Import "xsystem.cpp"
CreateShortcut(GetEnv("USERPROFILE")+"\Start Menu\My Program.lnk","c:\windows\notepad.exe","My Program","CTRL+ALT+SHIFT+X","notepad.exe, 0","","c:\")
End



Extern "C"
	Function xsystem(command:Byte Ptr)
EndExtern

Function CreateShortcut(linkfile$,exefile$,title$,hotkey$,icon$,exeargs$,workdir$)
	Local filename$=GetEnv("TEMP")+"\creascut.vbs"
	Local f:TStream
	f=WriteStream(filename)
	WriteLine(f,"set objWSHShell = CreateObject(~qWScript.Shell~q)")
	WriteLine(f,"set objSC = objWSHShell.CreateShortcut(~q"+linkfile+"~q)")
	WriteLine(f,"objSC.Description = ~q"+title+"~q")
	WriteLine(f,"objSC.HotKey = ~q"+hotkey+"~q")
	WriteLine(f,"objSC.IconLocation = ~q"+icon$+"~q")  ' 0 is the index
	WriteLine(f,"objSC.TargetPath = ~q"+exefile+"~q")
	WriteLine(f,"objSC.Arguments = ~q"+exeargs+"~q")
	WriteLine(f,"objSC.WindowStyle = 1")   ' 1 = normal; 3 = maximize window; 7 = minimize
	WriteLine(f,"objSC.WorkingDirectory = ~q"+workdir+"~q")
	WriteLine(f,"objSC.Save")
	CloseStream(f)
	xsystem("cscript.exe ~q"+filename+"~q")
	DeleteFile filename
EndFunction



Extern "Win32"
	Function GetEnvironmentVariable(lpName$z, lpBuffer:Byte Ptr, nSize) = "GetEnvironmentVariableA@12"
End Extern

Function GetEnv$(envVar$)
	Local buff@[64]
	Local rtn = GetEnvironmentVariable(envVar$, buff@, buff.length)
	If rtn > buff.length
		buff@ = buff@[..rtn]
		rtn = GetEnvironmentVariable(envVar$, buff@, buff.length)
	EndIf
	Return String.FromBytes(buff@, rtn)
End Function

Comments

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


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


xlsior2008
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)


Code Archives Forum