Extracting Windows File Properties (Custom Properties) C# - c#

In Word/Excel you have to possibility to add Custom properties. (See Image)
Custom Properties.
As you guys can see there is the field: "Properties:", you can add any information you want there.
When you save the file and you go to the file location in the folder, you can right click -> Properties and you have all the tabs: General/Security/Details/Previous Versions. with the feature you add the tab Custom.
Now I want to get this information through coding: Custom Properties information. and extract it later to notepad.
So far i used the Shell32 but then I only get the information that is in the Details tab. I did some research and saw some possibilities with DSOfile.dll. But I want to know if there is a possibility to do this without installing other DLL?
This is my code so far with the Shell32.
static void Main(string[] args)
{
//using (StreamWriter writer = new StreamWriter(#"filepathhere"))
//{
//Console.SetOut(writer);
ReadProperties();
//}
}
static void ReadProperties()
{
List<string> arrHeaders = new List<string>();
Shell shell = new Shell();
Folder objFolder = shell.NameSpace(#"filepathhere");
FolderItem objFolderItem = objFolder.ParseName("filehere.doc");
for (int i = 0; i < short.MaxValue; i++)
{
string header = objFolder.GetDetailsOf(objFolder, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
for ( int i = 0; i < arrHeaders.Count; i++)
{
Console.WriteLine("{0}\t{1}: {2}", i, arrHeaders[i], objFolder.GetDetailsOf(objFolderItem, i));
}
Console.ReadKey();
}
Thanks in advance!
Desu

Historically these properties were defined by the technology called "Structured Storage". The first Structured Storage implementation is the ancient (but still very alive) Compound File Format
After that, Microsoft added Structured Storage capabilities directly into NTFS. This allows you to define properties like author or title on any files (even .txt) files. Although the Explorer UI does not let you do this anymore for some reason, I think it still works programmatically.
And then, with Vista, Microsoft rebooted all that and introduced a superset of all this: the Windows Property System.
Unfortunately, there is no .NET API in the framework for all this. But Microsoft created an open source .NET library called the Windows API CodePack. So, the easiest way for you to extract properties is to add a reference to the WindowsAPICodePack Core NugetPackage and you can use it like this:
static void Main(string[] args)
{
foreach (var prop in new ShellPropertyCollection(#"mypath\myfile"))
{
Console.WriteLine(prop.CanonicalName + "=" + prop.ValueAsObject);
}
}
If you don't want to add extra DLLs, then you can extract the ShellPropertyCollection code from the WindowsAPICodePack source (Windows API Code Pack: Where is it?). It's quite a work but doable.
Another solution in your case, is to use the old Structure Storage native API. I've provided a sample that does this. Here is how you can use it:
static void Main(string[] args)
{
foreach (var prop in new StructuredStorage(#"mypath\myfile").Properties)
{
Console.WriteLine(prop.Name + "=" + prop.Value);
}
}
public sealed class StructuredStorage
{
public static readonly Guid SummaryInformationFormatId = new Guid("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}");
public static readonly Guid DocSummaryInformationFormatId = new Guid("{D5CDD502-2E9C-101B-9397-08002B2CF9AE}");
public static readonly Guid UserDefinedPropertiesId = new Guid("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
private List<StructuredProperty> _properties = new List<StructuredProperty>();
public StructuredStorage(string filePath)
{
if (filePath == null)
throw new ArgumentNullException("filePath");
FilePath = filePath;
IPropertySetStorage propertySetStorage;
int hr = StgOpenStorageEx(FilePath, STGM.STGM_READ | STGM.STGM_SHARE_DENY_NONE | STGM.STGM_DIRECT_SWMR, STGFMT.STGFMT_ANY, 0, IntPtr.Zero, IntPtr.Zero, typeof(IPropertySetStorage).GUID, out propertySetStorage);
if (hr == STG_E_FILENOTFOUND || hr == STG_E_PATHNOTFOUND)
throw new FileNotFoundException(null, FilePath);
if (hr != 0)
throw new Win32Exception(hr);
try
{
LoadPropertySet(propertySetStorage, SummaryInformationFormatId);
LoadPropertySet(propertySetStorage, DocSummaryInformationFormatId);
}
finally
{
Marshal.ReleaseComObject(propertySetStorage);
}
// for some reason we can't read this one on the same COM ref?
LoadProperties(UserDefinedPropertiesId);
}
public string FilePath { get; private set; }
public IReadOnlyList<StructuredProperty> Properties
{
get
{
return _properties;
}
}
private void LoadPropertySet(IPropertySetStorage propertySetStorage, Guid fmtid)
{
IPropertyStorage propertyStorage;
int hr = propertySetStorage.Open(fmtid, STGM.STGM_READ | STGM.STGM_SHARE_EXCLUSIVE, out propertyStorage);
if (hr == STG_E_FILENOTFOUND || hr == STG_E_ACCESSDENIED)
return;
if (hr != 0)
throw new Win32Exception(hr);
IEnumSTATPROPSTG es;
propertyStorage.Enum(out es);
if (es == null)
return;
try
{
var stg = new STATPROPSTG();
int fetched;
do
{
hr = es.Next(1, ref stg, out fetched);
if (hr != 0 && hr != 1)
throw new Win32Exception(hr);
if (fetched == 1)
{
string name = GetPropertyName(fmtid, propertyStorage, stg);
var propsec = new PROPSPEC[1];
propsec[0] = new PROPSPEC();
propsec[0].ulKind = stg.lpwstrName != null ? PRSPEC.PRSPEC_LPWSTR : PRSPEC.PRSPEC_PROPID;
IntPtr lpwstr = IntPtr.Zero;
if (stg.lpwstrName != null)
{
lpwstr = Marshal.StringToCoTaskMemUni(stg.lpwstrName);
propsec[0].union.lpwstr = lpwstr;
}
else
{
propsec[0].union.propid = stg.propid;
}
var vars = new PROPVARIANT[1];
vars[0] = new PROPVARIANT();
try
{
hr = propertyStorage.ReadMultiple(1, propsec, vars);
if (hr != 0)
throw new Win32Exception(hr);
}
finally
{
if (lpwstr != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(lpwstr);
}
}
object value;
try
{
switch (vars[0].vt)
{
case VARTYPE.VT_BOOL:
value = vars[0].union.boolVal != 0 ? true : false;
break;
case VARTYPE.VT_BSTR:
value = Marshal.PtrToStringUni(vars[0].union.bstrVal);
break;
case VARTYPE.VT_CY:
value = decimal.FromOACurrency(vars[0].union.cyVal);
break;
case VARTYPE.VT_DATE:
value = DateTime.FromOADate(vars[0].union.date);
break;
case VARTYPE.VT_DECIMAL:
IntPtr dec = IntPtr.Zero;
Marshal.StructureToPtr(vars[0], dec, false);
value = Marshal.PtrToStructure(dec, typeof(decimal));
break;
case VARTYPE.VT_DISPATCH:
value = Marshal.GetObjectForIUnknown(vars[0].union.pdispVal);
break;
case VARTYPE.VT_ERROR:
case VARTYPE.VT_HRESULT:
value = vars[0].union.scode;
break;
case VARTYPE.VT_FILETIME:
value = DateTime.FromFileTime(vars[0].union.filetime);
break;
case VARTYPE.VT_I1:
value = vars[0].union.cVal;
break;
case VARTYPE.VT_I2:
value = vars[0].union.iVal;
break;
case VARTYPE.VT_I4:
value = vars[0].union.lVal;
break;
case VARTYPE.VT_I8:
value = vars[0].union.hVal;
break;
case VARTYPE.VT_INT:
value = vars[0].union.intVal;
break;
case VARTYPE.VT_LPSTR:
value = Marshal.PtrToStringAnsi(vars[0].union.pszVal);
break;
case VARTYPE.VT_LPWSTR:
value = Marshal.PtrToStringUni(vars[0].union.pwszVal);
break;
case VARTYPE.VT_R4:
value = vars[0].union.fltVal;
break;
case VARTYPE.VT_R8:
value = vars[0].union.dblVal;
break;
case VARTYPE.VT_UI1:
value = vars[0].union.bVal;
break;
case VARTYPE.VT_UI2:
value = vars[0].union.uiVal;
break;
case VARTYPE.VT_UI4:
value = vars[0].union.ulVal;
break;
case VARTYPE.VT_UI8:
value = vars[0].union.uhVal;
break;
case VARTYPE.VT_UINT:
value = vars[0].union.uintVal;
break;
case VARTYPE.VT_UNKNOWN:
value = Marshal.GetObjectForIUnknown(vars[0].union.punkVal);
break;
default:
value = null;
break;
}
}
finally
{
PropVariantClear(ref vars[0]);
}
var property = new StructuredProperty(fmtid, name, stg.propid);
property.Value = value;
_properties.Add(property);
}
}
while (fetched == 1);
}
finally
{
Marshal.ReleaseComObject(es);
}
}
private static string GetPropertyName(Guid fmtid, IPropertyStorage propertyStorage, STATPROPSTG stg)
{
if (!string.IsNullOrEmpty(stg.lpwstrName))
return stg.lpwstrName;
var propids = new int[1];
propids[0] = stg.propid;
var names = new string[1];
names[0] = null;
int hr = propertyStorage.ReadPropertyNames(1, propids, names);
if (hr == 0)
return names[0];
return null;
}
public void LoadProperties(Guid formatId)
{
IPropertySetStorage propertySetStorage;
int hr = StgOpenStorageEx(FilePath, STGM.STGM_READ | STGM.STGM_SHARE_DENY_NONE | STGM.STGM_DIRECT_SWMR, STGFMT.STGFMT_ANY, 0, IntPtr.Zero, IntPtr.Zero, typeof(IPropertySetStorage).GUID, out propertySetStorage);
if (hr == STG_E_FILENOTFOUND || hr == STG_E_PATHNOTFOUND)
throw new FileNotFoundException(null, FilePath);
if (hr != 0)
throw new Win32Exception(hr);
try
{
LoadPropertySet(propertySetStorage, formatId);
}
finally
{
Marshal.ReleaseComObject(propertySetStorage);
}
}
private const int STG_E_FILENOTFOUND = unchecked((int)0x80030002);
private const int STG_E_PATHNOTFOUND = unchecked((int)0x80030003);
private const int STG_E_ACCESSDENIED = unchecked((int)0x80030005);
private enum PRSPEC
{
PRSPEC_LPWSTR = 0,
PRSPEC_PROPID = 1
}
private enum STGFMT
{
STGFMT_ANY = 4,
}
[Flags]
private enum STGM
{
STGM_READ = 0x00000000,
STGM_READWRITE = 0x00000002,
STGM_SHARE_DENY_NONE = 0x00000040,
STGM_SHARE_DENY_WRITE = 0x00000020,
STGM_SHARE_EXCLUSIVE = 0x00000010,
STGM_DIRECT_SWMR = 0x00400000
}
// we only define what we handle
private enum VARTYPE : short
{
VT_I2 = 2,
VT_I4 = 3,
VT_R4 = 4,
VT_R8 = 5,
VT_CY = 6,
VT_DATE = 7,
VT_BSTR = 8,
VT_DISPATCH = 9,
VT_ERROR = 10,
VT_BOOL = 11,
VT_UNKNOWN = 13,
VT_DECIMAL = 14,
VT_I1 = 16,
VT_UI1 = 17,
VT_UI2 = 18,
VT_UI4 = 19,
VT_I8 = 20,
VT_UI8 = 21,
VT_INT = 22,
VT_UINT = 23,
VT_HRESULT = 25,
VT_LPSTR = 30,
VT_LPWSTR = 31,
VT_FILETIME = 64,
}
[StructLayout(LayoutKind.Explicit)]
private struct PROPVARIANTunion
{
[FieldOffset(0)]
public sbyte cVal;
[FieldOffset(0)]
public byte bVal;
[FieldOffset(0)]
public short iVal;
[FieldOffset(0)]
public ushort uiVal;
[FieldOffset(0)]
public int lVal;
[FieldOffset(0)]
public uint ulVal;
[FieldOffset(0)]
public int intVal;
[FieldOffset(0)]
public uint uintVal;
[FieldOffset(0)]
public long hVal;
[FieldOffset(0)]
public ulong uhVal;
[FieldOffset(0)]
public float fltVal;
[FieldOffset(0)]
public double dblVal;
[FieldOffset(0)]
public short boolVal;
[FieldOffset(0)]
public int scode;
[FieldOffset(0)]
public long cyVal;
[FieldOffset(0)]
public double date;
[FieldOffset(0)]
public long filetime;
[FieldOffset(0)]
public IntPtr bstrVal;
[FieldOffset(0)]
public IntPtr pszVal;
[FieldOffset(0)]
public IntPtr pwszVal;
[FieldOffset(0)]
public IntPtr punkVal;
[FieldOffset(0)]
public IntPtr pdispVal;
}
[StructLayout(LayoutKind.Sequential)]
private struct PROPSPEC
{
public PRSPEC ulKind;
public PROPSPECunion union;
}
[StructLayout(LayoutKind.Explicit)]
private struct PROPSPECunion
{
[FieldOffset(0)]
public int propid;
[FieldOffset(0)]
public IntPtr lpwstr;
}
[StructLayout(LayoutKind.Sequential)]
private struct PROPVARIANT
{
public VARTYPE vt;
public ushort wReserved1;
public ushort wReserved2;
public ushort wReserved3;
public PROPVARIANTunion union;
}
[StructLayout(LayoutKind.Sequential)]
private struct STATPROPSTG
{
[MarshalAs(UnmanagedType.LPWStr)]
public string lpwstrName;
public int propid;
public VARTYPE vt;
}
[StructLayout(LayoutKind.Sequential)]
private struct STATPROPSETSTG
{
public Guid fmtid;
public Guid clsid;
public uint grfFlags;
public System.Runtime.InteropServices.ComTypes.FILETIME mtime;
public System.Runtime.InteropServices.ComTypes.FILETIME ctime;
public System.Runtime.InteropServices.ComTypes.FILETIME atime;
public uint dwOSVersion;
}
[DllImport("ole32.dll")]
private static extern int StgOpenStorageEx([MarshalAs(UnmanagedType.LPWStr)] string pwcsName, STGM grfMode, STGFMT stgfmt, int grfAttrs, IntPtr pStgOptions, IntPtr reserved2, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IPropertySetStorage ppObjectOpen);
[DllImport("ole32.dll")]
private static extern int PropVariantClear(ref PROPVARIANT pvar);
[Guid("0000013B-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IEnumSTATPROPSETSTG
{
[PreserveSig]
int Next(int celt, ref STATPROPSETSTG rgelt, out int pceltFetched);
// rest ommited
}
[Guid("00000139-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IEnumSTATPROPSTG
{
[PreserveSig]
int Next(int celt, ref STATPROPSTG rgelt, out int pceltFetched);
// rest ommited
}
[Guid("00000138-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IPropertyStorage
{
[PreserveSig]
int ReadMultiple(uint cpspec, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] PROPSPEC[] rgpspec, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] PROPVARIANT[] rgpropvar);
[PreserveSig]
int WriteMultiple(uint cpspec, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] PROPSPEC[] rgpspec, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] PROPVARIANT[] rgpropvar, uint propidNameFirst);
[PreserveSig]
int DeleteMultiple(uint cpspec, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] PROPSPEC[] rgpspec);
[PreserveSig]
int ReadPropertyNames(uint cpropid, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] int[] rgpropid, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 0)] string[] rglpwstrName);
[PreserveSig]
int NotDeclared1();
[PreserveSig]
int NotDeclared2();
[PreserveSig]
int Commit(uint grfCommitFlags);
[PreserveSig]
int NotDeclared3();
[PreserveSig]
int Enum(out IEnumSTATPROPSTG ppenum);
// rest ommited
}
[Guid("0000013A-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IPropertySetStorage
{
[PreserveSig]
int Create([MarshalAs(UnmanagedType.LPStruct)] Guid rfmtid, [MarshalAs(UnmanagedType.LPStruct)] Guid pclsid, uint grfFlags, STGM grfMode, out IPropertyStorage ppprstg);
[PreserveSig]
int Open([MarshalAs(UnmanagedType.LPStruct)] Guid rfmtid, STGM grfMode, out IPropertyStorage ppprstg);
[PreserveSig]
int NotDeclared3();
[PreserveSig]
int Enum(out IEnumSTATPROPSETSTG ppenum);
}
}
public sealed class StructuredProperty
{
public StructuredProperty(Guid formatId, string name, int id)
{
FormatId = formatId;
Name = name;
Id = id;
}
public Guid FormatId { get; private set; }
public string Name { get; private set; }
public int Id { get; private set; }
public object Value { get; set; }
public override string ToString()
{
return Name;
}
}

You can try NPOI engine to extract properties from different Office files (doc, xls, xlsx, docx, etc). This component doesn't have any third party dependencies and you don't need Office to use it.
However, this library is a bit tricky because you need to use different types of property extractors for different type of files. Good code sample can be found in official Git Hub repository TestHPSFPropertiesExtractor.
NuGet package can be found here.

There's a set of NuGet packages called NetOffice that you can use. There are packages for each of the Office applications, plus a few core assemblies. Get NetOffice.Word and NetOffice.Excel and install them in your solution. There's a little documentation at the Codeplex site, but I had to browse the source to really understand what was going on. Here's a sample program:
using NetOffice.OfficeApi;
using System;
namespace Office_Doc_Reader
{
class Program
{
static void Main(string[] args)
{
using (var wordApp = new NetOffice.WordApi.Application())
using (var excelApp = new NetOffice.ExcelApi.Application())
{
var doc = wordApp.Documents.Open("C:\\Users\\John\\Desktop\\test.docx");
var xls = excelApp.Workbooks.Open("C:\\Users\\John\\Desktop\\test.xlsx");
var customProperties = (DocumentProperties)doc.CustomDocumentProperties;
foreach (var property in customProperties)
{
Console.WriteLine(String.Format("Name: {0}, Value: {1}, Type: {2}", property.Name, property.Value, property.Type));
}
customProperties = (DocumentProperties)xls.CustomDocumentProperties;
foreach (var property in customProperties)
{
Console.WriteLine(String.Format("Name: {0}, Value: {1}, Type: {2}", property.Name, property.Value, property.Type));
}
}
Console.ReadKey();
}
}
}
That shows the following results:
Name: Custom prop 1, Value: Text Value, Type: msoPropertyTypeString
Name: Custom prop 2, Value: 2/21/2016 12:00:00 AM, Type: msoPropertyTypeDate
Name: Custom prop 3, Value: 42, Type: msoPropertyTypeNumber
Name: Custom prop 4, Value: True, Type: msoPropertyTypeBoolean
Name: Foo, Value: abc, Type: msoPropertyTypeString
Name: Bar, Value: 1/1/1970 12:00:00 AM, Type: msoPropertyTypeDate
Name: Baz, Value: 3.14159, Type: msoPropertyTypeFloat
Name: Qux, Value: False, Type: msoPropertyTypeBoolean
For a Word and Excel file with these properties:
I've not worked with these much at all, so I won't be able to go much deeper than this. Hope it helps.

You may use OpenXml SDK from Microsoft. I believe it is able to get any info from word/excel files.
Have a look at this https://msdn.microsoft.com/en-us/library/office/hh674468.aspx also this https://msdn.microsoft.com/en-us/library/office/bb448854.aspx
or
http://openxmldeveloper.org/

There could be a more generic approach.
If your source is a ...x file, the document can be extracted via XML queries.
If you unzip the file (eg.g with 7-Zip) you will find a file called custom.xml in the subfolder docProps.
The properties can easily be found within the XML file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="SAPDOKNR">
<vt:lpwstr>10002210058</vt:lpwstr>
</property>
...

Related

Are there updated UIAutomation DLLs? (creating custom pattern in c#)

In my WPF c# program, I'm trying to implement a custom pattern for Windows UIAutomation, which means I need to import something that knows about IUIAutomationPatternHandler, and the only way I can find to that is to add a COM reference to UIAutomationCore, and I'm not sure which DLL it's actually importing, but what gets included seems wrong.
For example, the IUIAutomationRegistrar.RegisterPattern method that gets imported looks like this:
void RegisterPattern([In] ref UIAutomationPatternInfo pattern,
out int pPatternId, out int pPatternAvailablePropertyId,
[In] uint propertyIdCount, out int pPropertyIds,
[In] uint eventIdCount, out int pEventIds);
but the C++ documentation says this:
HRESULT RegisterPattern(
[in] const UIAutomationPatternInfo *pattern,
[out] PATTERNID *pPatternId,
[out] PROPERTYID *pPatternAvailablePropertyId,
[in] UINT propertyIdCount,
[out] PROPERTYID *pPropertyIds,
[in] UINT eventIdCount,
[out] EVENTID *pEventIds
);
which is kind of standard COM stuff, where you allocate an array to receive the information, and then pass in the array itself (pPropertyIds) and the size of the array (propertyIdCount) which means the c# import where it thinks pPropertyIds is an "out int" is wrong.
Is adding a COM reference to UIAutomationCore the right way to do this? Or is there a different process that is recommended, to get the correct signature? Or do I just have to do my own manual importing so the parameters are correct?
EDIT
In order to call RegisterPattern, I need a CUIAutomationRegistrar object from the DLL. Normally you just call
var registrar = new UIA.CUIAutomationRegistrar();
and that works, but then in order to call my own version of RegisterPattern(), I need to call InvokeMethod (right?) and when I call
registrar.GetType().GetMethods()
or
registrar.GetType().GetMethod("RegisterPattern")
It claims there's no method named "RegisterPattern", probably because the method is marked
[MethodImpl(MethodImplOptions.InternalCall)]
so it's not actually a "real" method.
EDIT 2
So this works:
var registrar = new UIA.CUIAutomationRegistrar();
MyCopyPaste.IUIAutomationRegistrar myRegistar = (MyCopyPaste.IUIAutomationRegistrar)registrar;
I assume the compiler is actually doing the QueryInterface() behind the scenes, and since MyCopyPaste.IUIAutomationRegistrar has the same UUID as UIA.IUIAutomationRegistrar, the compiler is happy.
But then using 'myRegistrar' is tricky. I modified the RegisterPattern() method in MyCopyPaste.IUIAutomationRegstrar like so:
[MethodImpl(MethodImplOptions.InternalCall)]
void RegisterPattern([In] ref UIAutomationPatternInfo pattern,
out int pPatternId, out int pPatternAvailablePropertyId,
[In] uint propertyIdCount,
[MarshalAs(UnmanagedType.LPArray)] int[] pPropertyIds,
[In] uint eventIdCount, out int pEventIds);
which I call like this:
int[] propertyIds = new int[10];
myRegistar.RegisterPattern(ref patternInfo,
out int patternId, out int patternAvailablePropertyId,
10, propertyIds, 0, out int eventIds);
and it throws a "Value does not fall within the expected range." at runtime.
EDIT 3
Here's my entire copy/paste section:
namespace MyCopyPaste
{
// [TypeIdentifier("930299ce-9965-4dec-b0f4-a54848d4b667", "UIA.UIAutomationType")]
public enum UIAutomationType
{
UIAutomationType_Int = 1,
UIAutomationType_Bool = 2,
UIAutomationType_String = 3,
UIAutomationType_Double = 4,
UIAutomationType_Point = 5,
UIAutomationType_Rect = 6,
UIAutomationType_Element = 7,
UIAutomationType_Array = 65536,
UIAutomationType_Out = 131072,
UIAutomationType_IntArray = 65537,
UIAutomationType_BoolArray = 65538,
UIAutomationType_StringArray = 65539,
UIAutomationType_DoubleArray = 65540,
UIAutomationType_PointArray = 65541,
UIAutomationType_RectArray = 65542,
UIAutomationType_ElementArray = 65543,
UIAutomationType_OutInt = 131073,
UIAutomationType_OutBool = 131074,
UIAutomationType_OutString = 131075,
UIAutomationType_OutDouble = 131076,
UIAutomationType_OutPoint = 131077,
UIAutomationType_OutRect = 131078,
UIAutomationType_OutElement = 131079,
UIAutomationType_OutIntArray = 196609,
UIAutomationType_OutBoolArray = 196610,
UIAutomationType_OutStringArray = 196611,
UIAutomationType_OutDoubleArray = 196612,
UIAutomationType_OutPointArray = 196613,
UIAutomationType_OutRectArray = 196614,
UIAutomationType_OutElementArray = 196615
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
// [TypeIdentifier("930299ce-9965-4dec-b0f4-a54848d4b667", "UIA.UIAutomationParameter")]
public struct UIAutomationParameter
{
public UIAutomationType type;
public IntPtr pData;
}
[Guid("C03A7FE4-9431-409F-BED8-AE7C2299BC8D")]
[InterfaceType(1)]
// [TypeIdentifier]
public interface IUIAutomationPatternInstance
{
}
[Guid("D97022F3-A947-465E-8B2A-AC4315FA54E8")]
[InterfaceType(1)]
// [TypeIdentifier]
public interface IUIAutomationPatternHandler
{
[MethodImpl(MethodImplOptions.InternalCall)]
void CreateClientWrapper([In][MarshalAs(UnmanagedType.Interface)] IUIAutomationPatternInstance pPatternInstance, [MarshalAs(UnmanagedType.IUnknown)] out object pClientWrapper);
[MethodImpl(MethodImplOptions.InternalCall)]
void Dispatch([In][MarshalAs(UnmanagedType.IUnknown)] object pTarget, [In] uint index, [In] ref UIAutomationParameter pParams, [In] uint cParams);
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
// [TypeIdentifier("930299ce-9965-4dec-b0f4-a54848d4b667", "UIA.UIAutomationPatternInfo")]
public struct UIAutomationPatternInfo
{
public Guid guid;
[MarshalAs(UnmanagedType.LPWStr)]
public string pProgrammaticName;
public Guid providerInterfaceId;
public Guid clientInterfaceId;
public uint cProperties;
public IntPtr pProperties;
public uint cMethods;
public IntPtr pMethods;
public uint cEvents;
public IntPtr pEvents;
[MarshalAs(UnmanagedType.Interface)]
public IUIAutomationPatternHandler pPatternHandler;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
// [TypeIdentifier("930299ce-9965-4dec-b0f4-a54848d4b667", "UIA.UIAutomationPropertyInfo")]
public struct UIAutomationPropertyInfo
{
public Guid guid;
[MarshalAs(UnmanagedType.LPWStr)]
public string pProgrammaticName;
public UIAutomationType type;
}
[InterfaceType(1)]
[Guid("8609C4EC-4A1A-4D88-A357-5A66E060E1CF")]
// [TypeIdentifier]
[ComImport]
public interface IUIAutomationRegistrar
{
[MethodImpl(MethodImplOptions.InternalCall)]
void RegisterProperty([In] ref UIAutomationPropertyInfo property, out int propertyId);
void _VtblGap1_1();
[MethodImpl(MethodImplOptions.InternalCall)]
void RegisterPattern([In] ref UIAutomationPatternInfo pattern,
out int pPatternId, out int pPatternAvailablePropertyId,
[In] uint propertyIdCount,
[MarshalAs(UnmanagedType.LPArray)] int[] pPropertyIds,
[In] uint eventIdCount, out int pEventIds);
}
[Guid("8609C4EC-4A1A-4D88-A357-5A66E060E1CF")]
// [TypeIdentifier]
public interface CUIAutomationRegistrar : IUIAutomationRegistrar
{
}
}
and here's how I'm using it:
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("BFEF93B9-0CAD-4B0F-8FB7-51CFF6891BCD")] // GUID of IValidatingPattern
public interface IValidatingPattern
{
static readonly Guid PatternGUID = new(0x66e36682, 0x42f6, 0x4266, 0xaf, 0x27, 0xf, 0x34, 0xfa, 0xe6, 0x58, 0x3e);
static readonly string PatternName = "Validating";
static readonly Guid IsValidGUID = new(0x5dbe95c5, 0xb18c, 0x4904, 0xa3, 0xae, 0x57, 0xcb, 0xe4, 0xc2, 0x41, 0x5e);
public bool IsValid { get; }
}
internal class ValidatingPattern : IValidatingPattern
{
public bool IsValid
{
get
{
Debug.WriteLine("Not implemented (yet)");
throw new NotImplementedException();
}
}
}
UIAutomationPatternInfo patternInfo = new()
{
guid = IValidatingPattern.PatternGUID,
pProgrammaticName = IValidatingPattern.PatternName,
providerInterfaceId = typeof(IValidatingPattern).GUID,
cProperties = 0, // (uint)properties.Length,
// struct UIAutomationPropertyInfo *pProperties;
cMethods = 0,
// struct UIAutomationMethodInfo *pMethods;
cEvents = 0,
// struct UIAutomationEventInfo *pEvents;
pPatternHandler = Handler
};
var registrar = new UIA.CUIAutomationRegistrar();
IUIAutomationRegistrar myRegistar = (IUIAutomationRegistrar)registrar;
int[] propertyIds = new int[10];
myRegistar.RegisterPattern(ref patternInfo,
out int patternId, out int patternAvailablePropertyId,
10/*(uint)properties.Length*/, propertyIds, 0, out int eventIds);
Note that if I change the 'propertyIdCount' to zero, the call works perfectly and returns patternId=50000 and patternAvailablePropertyId=500001:
myRegistar.RegisterPattern(ref patternInfo,
out int patternId, out int patternAvailablePropertyId,
0/* <-- WORKS WHEN ZERO*/, propertyIds, 0, out int eventIds);
EDIT 4
I also tried defining a single method:
UIAutomationType[] methodOutParamTypes = { UIAutomationType.UIAutomationType_Bool };
GCHandle methodOutParamTypesHandle = GCHandle.Alloc(methodOutParamTypes);
string[] methodOutParamNames = { "IsValid" };
GCHandle methodOutParamNamesHandle = GCHandle.Alloc(methodOutParamNames);
UIAutomationMethodInfo getIsValidMethod = new()
{
pProgrammaticName = "get_IsValid",
doSetFocus = 0, // int
cInParameters = 0,
cOutParameters = 1,
pParameterTypes = (IntPtr)methodOutParamTypesHandle,
pParameterNames = (IntPtr)methodOutParamNamesHandle
};
UIAutomationMethodInfo[] methods = { getIsValidMethod };
GCHandle methodsHandle = GCHandle.Alloc(methods);
UIAutomationPatternInfo patternInfo = new()
{
guid = IValidatingPattern.PatternGUID,
pProgrammaticName = IValidatingPattern.PatternName,
providerInterfaceId = typeof(IValidatingPattern).GUID,
cProperties = 0, // (uint)properties.Length,
// struct UIAutomationPropertyInfo *pProperties;
cMethods = 1,
pMethods = (IntPtr)methodsHandle, // struct UIAutomationMethodInfo *pMethods;
cEvents = 0,
// struct UIAutomationEventInfo *pEvents;
pPatternHandler = Handler
};
var registrar = new UIA.CUIAutomationRegistrar();
MyCopyPaste.IUIAutomationRegistrar myRegistar = (MyCopyPaste.IUIAutomationRegistrar)registrar;
int[] propertyIds = new int[10];
myRegistar.RegisterPattern(ref patternInfo,
out int patternId, out int patternAvailablePropertyId,
0/*(uint)properties.Length*/, propertyIds,
0, out int eventIds);
That throws a NullReferenceException.

Unexpected results while marshaling list of struct

I could not find out what I'm doing wrong. I think I'm doing everything by the numbers, but it looks like I'm missing something.
On the managed side:
public override List<TimerPair> GetNativeTimers()
{
var listPtr = HgrDll.GetNativeTimers(out var size, out var ptrShift);
if (listPtr == IntPtr.Zero)
throw new NullReferenceException("sounds like the list"
+ " pointer was not given");
var timerPairList = new List<TimerPair>(size);
var structSize = Marshal.SizeOf(typeof(TimerPairStruct2));
for (var i = 0; i < size; i++)
{
var o = Marshal.PtrToStructure<TimerPairStruct2>(listPtr).ToTimerPair();
timerPairList.Add(o);
listPtr += structSize;
}
return timerPairList;
}
The target struct (used for intermediate compute) and class:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct TimerPairStruct2 : ITimerPairStruct
{
public long Time;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string Descr;
public TimerPair ToTimerPair() => new TimerPair(Descr, Time);
}
public class TimerPair
{
public string Descr { get; set; }
public TimeSpan Time { get; set; }
public TimerPair(string descr, long time)
{
Descr = descr;
Time = new TimeSpan(0,0,0,0, (int)((double)time/1000000));
}
}
For the Pinvoke part:
[DllImport("DwarfHomograph.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr GetNativeTimers(out int size, out int ptrShift);
and now on the native side:
DwarfHgr* DwarfAsset = nullptr;
extern "C" __declspec(dllexport) list<TimerPair>* GetNativeTimers(
int* size,
int* ptrShift)
{
return DwarfAsset->GetNativeTimers(size, ptrShift);
}
the used method from the DwarfHgr class:
list<TimerPair>* DwarfHgr::GetNativeTimers(int* size, int* ptrShift)
{
TimerPairsList = Sw.AsTimerPairs();
*size = static_cast<int>(TimerPairsList.size());
*ptrShift = sizeof(TimerPair);
return &TimerPairsList;
}
and the member is list<TimerPair> TimerPairsList;
The struct I'm marshaling is:
//#pragma pack(push,8)
struct __declspec(dllexport) TimerPair final
{
long long Time{}; // ns
char Descr[64]{};
};
//#pragma pack(pop)
I'm having unit tests in both native and managed sides; the expected result looks like (native):
ImageA processing: 22.0704ms
ImageB processing: 22.0448ms
MatFullA.copyTo: 1.87452ms
MatToSparse: 1.93482ms
calcOpticalFlowFarneback: 1115.48ms
create flowSparse image: 10.1416ms
But I have this (managed):
°óŠqÉ: 1,964,675.000000ms
q$Fý: 140,725,780.000000ms
xr$Fý: 140,725,780.000000ms
: 549,755.000000ms
à¡CUÉ: 1,964,675.000000ms
: 0.000000ms

EnumDisplayDevices not returning anything

I am having an issue working with EnumDisplayDevices in C#. I used the code posted here and it worked seamlessly. However recently I switched to coding the application on my desktop computer. The code no longer works, not giving me anything.
If it helps, my monitor is an LG W2753VC running on an AMD Radeon HD 5770.
Here is my current code:
var device = new DISPLAY_DEVICE();
device.cb = Marshal.SizeOf(device);
try {
for (uint id = 0; EnumDisplayDevices(null, id, ref device, 0); id++) {
device.cb = Marshal.SizeOf(device);
EnumDisplayDevices(device.DeviceName, 0, ref device, 0);
device.cb = Marshal.SizeOf(device);
Console.WriteLine("id={0}, name={1}, devicestring={2}", id, device.DeviceName, device.DeviceString);
if (device.DeviceName == null || device.DeviceName == "") continue;
}
} catch (Exception ex) { }
What I'm getting on my laptop:
id=0, name=\\.\DISPLAY1\Monitor0, devicestring=Generic PnP Monitor
(That should be "Mobile PC Display", but it's not the problem here)
What I'm getting on my desktop:
id=0, name=, devicestring=
id=1, name=, devicestring=
Am I doing this wrong, and if not, is there any other way to get all connected displays? I have already tried the WMI and registry approach.
Hard to tell without you posting all your p/invoke code. Here's code that works on my workstation:
public class EnumDisplayDevicesTest
{
[DllImport("user32.dll")]
static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);
[Flags()]
public enum DisplayDeviceStateFlags : int
{
/// <summary>The device is part of the desktop.</summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>The device is part of the desktop.</summary>
PrimaryDevice = 0x4,
/// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
MirroringDriver = 0x8,
/// <summary>The device is VGA compatible.</summary>
VGACompatible = 0x10,
/// <summary>The device is removable; it cannot be the primary display.</summary>
Removable = 0x20,
/// <summary>The device has more display modes than its output devices support.</summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DISPLAY_DEVICE
{
[MarshalAs(UnmanagedType.U4)]
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
[MarshalAs(UnmanagedType.U4)]
public DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}
public void Display()
{
DISPLAY_DEVICE d = new DISPLAY_DEVICE();
d.cb = Marshal.SizeOf(d);
try
{
for (uint id = 0; EnumDisplayDevices(null, id, ref d, 0); id++)
{
if (d.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop))
{
Console.WriteLine(
String.Format("{0}, {1}, {2}, {3}, {4}, {5}",
id,
d.DeviceName,
d.DeviceString,
d.StateFlags,
d.DeviceID,
d.DeviceKey
)
);
d.cb = Marshal.SizeOf(d);
EnumDisplayDevices(d.DeviceName, 0, ref d, 0);
Console.WriteLine(
String.Format("{0}, {1}",
d.DeviceName,
d.DeviceString
)
);
}
d.cb = Marshal.SizeOf(d);
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format("{0}", ex.ToString()));
}
}
}
The above code works as the OP desires on my machines, not sure why it won't work on his laptop.
An alternative method to getting the info is listed below, taken from here:
foreach (var display in DisplayDetails.GetMonitorDetails())
{
Console.WriteLine(display.Model);
}
public class DisplayDetails
{
public string PnPID { get; set; }
public string SerialNumber { get; set; }
public string Model { get; set; }
public string MonitorID { get; set; }
/// <summary>
/// The Constructor to create a new instances of the DisplayDetails class...
/// </summary>
public DisplayDetails(string sPnPID, string sSerialNumber, string sModel, string sMonitorID)
{
PnPID = sPnPID;
SerialNumber = sSerialNumber;
Model = sModel;
MonitorID = sMonitorID;
}
/// <summary>
/// This Function returns all Monitor Details
/// </summary>
/// <returns></returns>
static public IEnumerable<DisplayDetails> GetMonitorDetails()
{
//Open the Display Reg-Key
RegistryKey Display = Registry.LocalMachine;
Boolean bFailed = false;
try
{
Display = Registry.LocalMachine.OpenSubKey(#"SYSTEM\CurrentControlSet\Enum\DISPLAY");
}
catch
{
bFailed = true;
}
if (!bFailed & (Display != null))
{
//Get all MonitorIDss
foreach (string sMonitorID in Display.GetSubKeyNames())
{
RegistryKey MonitorID = Display.OpenSubKey(sMonitorID);
if (MonitorID != null)
{
//Get all Plug&Play ID's
foreach (string sPNPID in MonitorID.GetSubKeyNames())
{
RegistryKey PnPID = MonitorID.OpenSubKey(sPNPID);
if (PnPID != null)
{
string[] sSubkeys = PnPID.GetSubKeyNames();
//Check if Monitor is active
if (sSubkeys.Contains("Control"))
{
if (sSubkeys.Contains("Device Parameters"))
{
RegistryKey DevParam = PnPID.OpenSubKey("Device Parameters");
string sSerial = "";
string sModel = "";
//Define Search Keys
string sSerFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xff });
string sModFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xfc });
//Get the EDID code
byte[] bObj = DevParam.GetValue("EDID", null) as byte[];
if (bObj != null)
{
//Get the 4 Vesa descriptor blocks
string[] sDescriptor = new string[4];
sDescriptor[0] = Encoding.Default.GetString(bObj, 0x36, 18);
sDescriptor[1] = Encoding.Default.GetString(bObj, 0x48, 18);
sDescriptor[2] = Encoding.Default.GetString(bObj, 0x5A, 18);
sDescriptor[3] = Encoding.Default.GetString(bObj, 0x6C, 18);
//Search the Keys
foreach (string sDesc in sDescriptor)
{
if (sDesc.Contains(sSerFind))
{
sSerial = sDesc.Substring(4).Replace("\0", "").Trim();
}
if (sDesc.Contains(sModFind))
{
sModel = sDesc.Substring(4).Replace("\0", "").Trim();
}
}
}
if (!string.IsNullOrEmpty(sPNPID + sSerFind + sModel + sMonitorID))
{
yield return new DisplayDetails(sPNPID, sSerial, sModel, sMonitorID);
}
}
}
}
}
}
}
}
}
}
I had exactly this behavior when device.cb wasn't equal to one of 840 or 224 (sizeof DISPLAY_DEVICEW and DISPLAY_DEVICEA respectively). In my case I just mistakenly wrote device.cb = sizeof(device.cb);
Check value of Marshal.SizeOf(device) on both your computers.
I had the exact same problem and I found out that I was using the following to import EnumDisplayDevices:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
Rather than
[DllImport("user32.dll")]
The code example from GalacticJello is working for me. I forced the build of the assemblies for x86.
In other cases maybe try:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DISPLAY_DEVICE`
but this is not working for x86!

PInvoke PFCreateInterface C#

I am attempting to pinvoke PFCreateInterface in C# for a packet filtering app. However, the example on pinvoke.net seems inadequately documented. For starters, once StartPacketFilter() in main is called, how can i remove the packet filter programatically without closing the app? Also, i'm also quite confused about the format of the packet string.
I.e hostsToBlock[0] = "67.77.87.97,255.255.255.255,0"; //blocks all traffic on any port to/from 67.77.87.97
How would i go about blocking a remote port of 6980 across all ips?
The pinvoke codes are as follows :
/// <summary>
/// IP packet filter management wrapper for Iphlpapi.dll (Win 2000/XP)
/// </summary>
///
public class IpPacketFilter
{
[DllImport("iphlpapi.dll", EntryPoint = "PfBindInterfaceToIPAddress")]
public static extern int PfBindInterfaceToIPAddress(
IntPtr Interface_handle,
PFADDRESSTYPE pfatType,
ref int ip_address
);
[DllImport("iphlpapi.dll", EntryPoint = "PfCreateInterface")]
public static extern int PfCreateInterface(
int dwName,
PFFORWARD_ACTION inAction,
PFFORWARD_ACTION outAction,
bool UseLog,
bool MustBeUnique,
ref IntPtr ppInterface
);
//////
////
[DllImport("iphlpapi.dll", EntryPoint = "PfAddFiltersToInterface")]
public static extern int PfAddFiltersToInterface(
IntPtr interface_handle,
int cInFilters,
[MarshalAsAttribute(UnmanagedType.Struct)]
ref PPF_FILTER_DESCRIPTOR pfiltIn,
int cOutFilters,
[MarshalAsAttribute(UnmanagedType.Struct)]
ref PPF_FILTER_DESCRIPTOR pfiltOut,
[MarshalAsAttribute(UnmanagedType.Struct)]
ref PPF_FILTER_DESCRIPTOR pfHandle
);
}
public unsafe struct PPF_FILTER_DESCRIPTOR
{
public FILTER_FLAGS dwFilterFlags;
public int dwRule;
public PFADDRESSTYPE pfatType;
public int* SrcAddr;
public int* SrcMask;
public int* DstAddr;
public int* DstMask;
public PROTOCOL dwProtocol;
public int fLateBound;
public int wSrcPort;
public int wDstPort;
public int wSrcPortHighRange;
public int wDstPortHighRange;
}
public enum PFFORWARD_ACTION : int
{
PF_ACTION_FORWARD = 0,
PF_ACTION_DROP
}
public enum PFADDRESSTYPE : int
{
PF_IPV4,
PF_IPV6
}
public enum PROTOCOL : int
{
ANY = 0x00,
ICMP = 0x01,
TCP = 0x06,
UDP = 0x11
}
public enum FILTER_FLAGS : int
{
FD_FLAGS = 0x1
}
class Program
{
internal const int FALSE = 0;
internal const int TRUE = 1;
static void Main(string[] args)
{
string[] hostsToBlock = new string[2];
hostsToBlock[0] = "67.77.87.97,255.255.255.255,0"; //blocks all traffic on any port to/from 67.77.87.97
hostsToBlock[1] = "0.0.0.0,0.0.0.0,29000"; //blocks all traffic on port 29000, in and out
StartPacketFilter(hostsToBlock);
}
internal static int lIpFromString(string sIpAddress)
{
int lIp = 0;
try
{
string[] octets = sIpAddress.Split(new string[] { "." }, StringSplitOptions.None);
if (octets.Length != 4)
return 0;
for (int i = 0; i < 4; i++)
lIp |= (int.Parse(octets[i]) << (i * 8));
}
catch { }
return lIp;
}
internal static string[] GetLocalIpAddresses()
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
string[] localIpAddresses = new string[host.AddressList.Length];
for (int i = 0; i < host.AddressList.Length; i++)
{
localIpAddresses[i] = host.AddressList[i].ToString();
}
return localIpAddresses;
}
internal static bool StartPacketFilter(string[] hosts)
{
string[] localIpAddresses = GetLocalIpAddresses();
if (localIpAddresses == null)
return false;
foreach (string localAddress in localIpAddresses)
{
int result;
IntPtr interfaceHandle = new IntPtr();
//convert the string IP to an unsigned int for p/invoke
int lLocalIp = lIpFromString(localAddress);
//create a filter interface in the tcp/ip stack
result = IpPacketFilter.PfCreateInterface(0, PFFORWARD_ACTION.PF_ACTION_FORWARD, PFFORWARD_ACTION.PF_ACTION_FORWARD, false, true, ref interfaceHandle);
if (result != 0)
return false;
//bind interface to an ip address
result = IpPacketFilter.PfBindInterfaceToIPAddress(interfaceHandle, PFADDRESSTYPE.PF_IPV4, ref lLocalIp);
if (result != 0)
return false;
foreach (string targetHost in hosts)
{
IntPtr filterHandle = new IntPtr();
string[] hostDetail = targetHost.Split(new string[] { "," }, StringSplitOptions.None);
if (hostDetail != null && hostDetail.Length == 3)
{
//build the filter structure
PPF_FILTER_DESCRIPTOR filter = new PPF_FILTER_DESCRIPTOR();
filter.dwFilterFlags = FILTER_FLAGS.FD_FLAGS;
filter.dwRule = FALSE;
filter.pfatType = PFADDRESSTYPE.PF_IPV4;
filter.dwProtocol = PROTOCOL.TCP;
int iSrcAddr = lLocalIp;
int iSrcMask = lIpFromString("255.255.255.255");
filter.wSrcPort = 0;
filter.wSrcPortHighRange = 0;
int iDstAddr = lIpFromString(hostDetail[0]);
int iDstMask = lIpFromString(hostDetail[1]);
filter.wDstPort = int.Parse(hostDetail[2]);
filter.wDstPortHighRange = int.Parse(hostDetail[2]);
unsafe
{
filter.SrcAddr = &iSrcAddr;
filter.DstAddr = &iDstAddr;
filter.SrcMask = &iSrcMask;
filter.DstMask = &iDstMask;
}
// add filter to interface (both inbound and outbound)
result = IpPacketFilter.PfAddFiltersToInterface(interfaceHandle, 1, ref filter, 1, ref filter, ref filter);
if (result != 0)
return false;
}
}
}
return true;
}
}
After a quick search, I found this: PfRemoveFiltersFromInterface. This seems to do exactly what your asking for.

Setting IE Proxy by C#

Hello i want to set IE proxy using a C# program as WebProxy class have get proxy method.But there is no method to set it!
Here are some alternatives found by Googling:
1- GlobalProxySelection
This is from http://www.hccp.org/csharp-http-proxy.html
Sample Code:
System.Net.Uri proxyURI = new System.Net.Uri("http://64.202.165.130:3128");
System.Net.GlobalProxySelection.Select = new System.Net.WebProxy(proxyURI);
2- Another discussion on StackOverflow: Programmatically Set Browser Proxy Settings in C#
Read it if you want the proxy only for your app.
For global change, it suggests looking at: http://msdn.microsoft.com/en-us/library/aa384113.aspx
Sample Code:
WINHTTP_PROXY_INFO proxyInfo;
// Allocate memory for string members.
proxyInfo.lpszProxy = new WCHAR[25];
proxyInfo.lpszProxyBypass = new WCHAR[25];
// Set the members of the proxy info structure.
proxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
swprintf_s(proxyInfo.lpszProxy, 25, L"proxy_server");
swprintf_s(proxyInfo.lpszProxyBypass, 25, L"<local>");
// Set the default proxy configuration.
if (WinHttpSetDefaultProxyConfiguration( &proxyInfo ))
printf("Proxy Configuration Set.\n");
// Free memory allocated to the strings.
delete [] proxyInfo.lpszProxy;
delete [] proxyInfo.lpszProxyBypass;
3- Using Native Code
This is from http://huddledmasses.org/setting-windows-internet-connection-proxy-from-c/
Sample Code:
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace PoshHttp
{
public class Proxies
{
public static bool UnsetProxy()
{
return SetProxy(null, null);
}
public static bool SetProxy(string strProxy)
{
return SetProxy(strProxy, null);
}
public static bool SetProxy(string strProxy, string exceptions)
{
InternetPerConnOptionList list = new InternetPerConnOptionList();
int optionCount = string.IsNullOrEmpty(strProxy) ? 1 : (string.IsNullOrEmpty(exceptions) ? 2 : 3);
InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
// USE a proxy server ...
options[0].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;
options[0].m_Value.m_Int = (int)((optionCount < 2) ? PerConnFlags.PROXY_TYPE_DIRECT : (PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY));
// use THIS proxy server
if (optionCount > 1)
{
options[1].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVER;
options[1].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(strProxy);
// except for these addresses ...
if (optionCount > 2)
{
options[2].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS;
options[2].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(exceptions);
}
}
// default stuff
list.dwSize = Marshal.SizeOf(list);
list.szConnection = IntPtr.Zero;
list.dwOptionCount = options.Length;
list.dwOptionError = 0;
int optSize = Marshal.SizeOf(typeof(InternetConnectionOption));
// make a pointer out of all that ...
IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length);
// copy the array over into that spot in memory ...
for (int i = 0; i < options.Length; ++i)
{
IntPtr opt = new IntPtr(optionsPtr.ToInt32() + (i * optSize));
Marshal.StructureToPtr(options[i], opt, false);
}
list.options = optionsPtr;
// and then make a pointer out of the whole list
IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)list.dwSize);
Marshal.StructureToPtr(list, ipcoListPtr, false);
// and finally, call the API method!
int returnvalue = NativeMethods.InternetSetOption(IntPtr.Zero,
InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION,
ipcoListPtr, list.dwSize) ? -1 : 0;
if (returnvalue == 0)
{ // get the error codes, they might be helpful
returnvalue = Marshal.GetLastWin32Error();
}
// FREE the data ASAP
Marshal.FreeCoTaskMem(optionsPtr);
Marshal.FreeCoTaskMem(ipcoListPtr);
if (returnvalue > 0)
{ // throw the error codes, they might be helpful
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return (returnvalue < 0);
}
}
#region WinInet structures
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct InternetPerConnOptionList
{
public int dwSize; // size of the INTERNET_PER_CONN_OPTION_LIST struct
public IntPtr szConnection; // connection name to set/query options
public int dwOptionCount; // number of options to set/query
public int dwOptionError; // on error, which option failed
//[MarshalAs(UnmanagedType.)]
public IntPtr options;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct InternetConnectionOption
{
static readonly int Size;
public PerConnOption m_Option;
public InternetConnectionOptionValue m_Value;
static InternetConnectionOption()
{
InternetConnectionOption.Size = Marshal.SizeOf(typeof(InternetConnectionOption));
}
// Nested Types
[StructLayout(LayoutKind.Explicit)]
public struct InternetConnectionOptionValue
{
// Fields
[FieldOffset(0)]
public System.Runtime.InteropServices.ComTypes.FILETIME m_FileTime;
[FieldOffset(0)]
public int m_Int;
[FieldOffset(0)]
public IntPtr m_StringPtr;
}
}
#endregion
#region WinInet enums
//
// options manifests for Internet{Query|Set}Option
//
public enum InternetOption : uint
{
INTERNET_OPTION_PER_CONNECTION_OPTION = 75
}
//
// Options used in INTERNET_PER_CONN_OPTON struct
//
public enum PerConnOption
{
INTERNET_PER_CONN_FLAGS = 1, // Sets or retrieves the connection type. The Value member will contain one or more of the values from PerConnFlags
INTERNET_PER_CONN_PROXY_SERVER = 2, // Sets or retrieves a string containing the proxy servers.
INTERNET_PER_CONN_PROXY_BYPASS = 3, // Sets or retrieves a string containing the URLs that do not use the proxy server.
INTERNET_PER_CONN_AUTOCONFIG_URL = 4//, // Sets or retrieves a string containing the URL to the automatic configuration script.
}
//
// PER_CONN_FLAGS
//
[Flags]
public enum PerConnFlags
{
PROXY_TYPE_DIRECT = 0x00000001, // direct to net
PROXY_TYPE_PROXY = 0x00000002, // via named proxy
PROXY_TYPE_AUTO_PROXY_URL = 0x00000004, // autoproxy URL
PROXY_TYPE_AUTO_DETECT = 0x00000008 // use autoproxy detection
}
#endregion
internal static class NativeMethods
{
[DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, int dwBufferLength);
}
}
Please check the links themselves for details and complete parts of the solutions.

Categories