Using add-type to show a file's property page

  • Follow


I am trying to use powershell 2.0 to show a file's property page by
using add-type.
The code below is the closest I have got so far but haven't got a clue
how to get it working
I can't pretend I know what I am doing with this, I have just tried to
hack it together from various sources. I certainly know nothing about
C#

$source=@"
public static void ShowFileProperties(string Filename) {
SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
info.lpVerb = "properties";
info.lpFile = Filename;
info.nShow = SW_SHOW;
info.fMask = SEE_MASK_INVOKEIDLIST;
ShellExecuteEx(ref info);
}

private const int SW_SHOW = 5;
private const uint SEE_MASK_INVOKEIDLIST = 12;

[DllImport("shell32.dll")]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

public struct SHELLEXECUTEINFO
{
public int cbSize;
public uint fMask;
public IntPtr hwnd;
public String lpVerb;
public String lpFile;
public String lpParameters;
public String lpDirectory;
public int nShow;
public int hInstApp;
public int lpIDList;
public String lpClass;
public int hkeyClass;
public uint dwHotKey;
public int hIcon;
public int hProcess;
}
"@

$test=add-type -memberdefinition $source -name myname `
-namespace mynamespace -passthru
$test[0]::ShowFileProperties("c:\boot.ini")

The error from this is a Windows error "Windows cannot find 'c'.  Make
sure you type the name correctly, and then..."
This looks like only the first character of the string "c:\boot.ini" is
being looked at.
but if even if I create the file c:\c and then run
$test[0]::ShowFileProperties("c:\boot.ini") from c:\ I get the same
error


-- 
Whisperer
0
Reply Whisperer 4/14/2010 2:31:33 PM

======================================================


-- 
Whisperer
0
Reply Whisperer 4/14/2010 2:52:02 PM


Sorted it myself.
Checked out the C# signature on
'pinvoke.net: shellexecuteex (shell32)'
(http://www.pinvoke.net/default.aspx/shell32.shellexecuteex)
and as a result changed the 'shell32.dll' line to the following and it
works!
[DllImport("shell32.dll", CharSet = CharSet.Auto)]


-- 
Whisperer
0
Reply Whisperer 4/16/2010 8:49:20 AM

2 Replies
290 Views

(page loaded in 6.425 seconds)


Reply: