Skip to content

Commit

Permalink
Windows 8 Default Program Fix
Browse files Browse the repository at this point in the history
This is more of a hack than a fix, needs more testing and for some reason rundll32 wont work on my machine (8.1) with the image viewer
  • Loading branch information
AlphaDelta committed Aug 19, 2015
1 parent 7326b06 commit 34ccf9a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
2 changes: 2 additions & 0 deletions SecureDesktop-GUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public Main()
{
IntPtr hProc = IntPtr.Zero;



WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
WinAPI.CreateProcess(null, String.Format("\"{0}\" \"{1}\"", Program.location + "\\SecureDesktop.exe", fileloc), IntPtr.Zero, IntPtr.Zero, false, WinAPI.CREATE_NO_WINDOW, IntPtr.Zero, null, ref si, out pi);
Expand Down
48 changes: 46 additions & 2 deletions SecureDesktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ static void Main(string[] args)
}

string procline = String.Join(" ", args);
string ext = Path.GetExtension(args[0]);

if (Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2)
{
int i = 0;
for (; i < 10; i++)
{
if (ext == ".dll")
{
procline = String.Format("{0} {1}", @"rundll32", procline);
break;
}
else if (ext != ".exe")
{
string file = "";
if (!ResolveExtension(ext, ref file)) break;
procline = String.Format("\"{0}\" {1}", file, procline);
ext = Path.GetExtension(file);
}
else break;
}
if (i == 10)
{
Console.WriteLine("Could not locate default program");
return;
}
}

Taskbar tb = new Taskbar(); //Get this first so that if we crash we wont be stuck in desktop limbo!

Expand All @@ -50,6 +77,7 @@ static void Main(string[] args)
WinAPI.SetThreadDesktop(hNewDesktop);
try
{

WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
si.lpDesktop = "securedesktop";
si.dwFlags |= 0x00000020;
Expand All @@ -65,9 +93,7 @@ static void Main(string[] args)
ERROR = sf.ERROR;
}
else
{
ERROR = 4;
}
}
catch (Exception e) { ERROR = 2; da_ex = e; }
finally { workdone = true; }
Expand Down Expand Up @@ -107,5 +133,23 @@ static void Main(string[] args)
break;
}
}

static bool ResolveExtension(string ext, ref string def)
{

uint length = 0;
uint ret = WinAPI.AssocQueryString(WinAPI.AssocF.None, WinAPI.AssocStr.Executable, ext, null, null, ref length);
if (ret == WinAPI.S_FALSE)
{
StringBuilder sb = new StringBuilder((int)length); // (length-1) will probably work too as the marshaller adds null termination
ret = WinAPI.AssocQueryString(WinAPI.AssocF.None, WinAPI.AssocStr.Executable, ext, null, sb, ref length);
if (ret == WinAPI.S_OK)
{
def = sb.ToString();
return true;
}
}
return false;
}
}
}
50 changes: 50 additions & 0 deletions SecureDesktop/WinAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,55 @@ public static extern IntPtr ShellExecute(
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);

[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, ref uint pcchOut);

[Flags]
public enum AssocF : uint
{
None = 0,
Init_NoRemapCLSID = 0x1,
Init_ByExeName = 0x2,
Open_ByExeName = 0x2,
Init_DefaultToStar = 0x4,
Init_DefaultToFolder = 0x8,
NoUserSettings = 0x10,
NoTruncate = 0x20,
Verify = 0x40,
RemapRunDll = 0x80,
NoFixUps = 0x100,
IgnoreBaseClass = 0x200,
Init_IgnoreUnknown = 0x400,
Init_FixedProgId = 0x800,
IsProtocol = 0x1000,
InitForFile = 0x2000,
}

public enum AssocStr
{
Command = 1,
Executable,
FriendlyDocName,
FriendlyAppName,
NoOpen,
ShellNewValue,
DDECommand,
DDEIfExec,
DDEApplication,
DDETopic,
InfoTip,
QuickTip,
TileInfo,
ContentType,
DefaultIcon,
ShellExtension,
DropTarget,
DelegateExecute,
SupportedUriProtocols,
Max,
}

public const int S_OK = 0, S_FALSE = 1;
}
}

0 comments on commit 34ccf9a

Please sign in to comment.