Skip to content

Instantly share code, notes, and snippets.

View sandrinodimattia's full-sized avatar
🏠
Working from home

Sandrino Di Mattia sandrinodimattia

🏠
Working from home
View GitHub Profile
@sandrinodimattia
sandrinodimattia / gist:3160833
Created July 22, 2012 19:38
DbProviderFactoryRepository
public class DbProviderFactoryRepository
{
/// <summary>
/// The table containing all the data.
/// </summary>
private DataTable dbProviderFactoryTable;
/// <summary>
/// Name of the configuration element.
/// </summary>
@sandrinodimattia
sandrinodimattia / gist:3160835
Created July 22, 2012 19:38
Using the DbProviderFactoryRepository
// Initialize the repository.
var repository = new DbProviderFactoryRepository();
// Create a description manually and add it to the repository.
var manualDescription = new DbProviderFactoryDescription();
manualDescription.Description = ".NET Framework Data Provider for Microsoft SQL Server Compact";
manualDescription.Invariant = "System.Data.SqlServerCe.3.5";
manualDescription.Name = "Microsoft SQL Server Compact Data Provider";
manualDescription.Type = "System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe";
@sandrinodimattia
sandrinodimattia / gist:3162091
Created July 23, 2012 05:40
Windows Firewall (VBScript)
' Create the firewall manager object.
Dim fwMgr
Set fwMgr = CreateObject("HNetCfg.FwMgr")
' Get the current profile for the local firewall policy.
Dim profile
Set profile = fwMgr.LocalPolicy.CurrentProfile
@sandrinodimattia
sandrinodimattia / gist:3162092
Created July 23, 2012 05:40
Windows Firewall (C++)
HRESULT hr = S_OK;
INetFwMgr* fwMgr = NULL;
INetFwPolicy* fwPolicy = NULL;
_ASSERT(fwProfile != NULL);
*fwProfile = NULL;
// Create an instance of the firewall settings manager.
hr = CoCreateInstance(
@sandrinodimattia
sandrinodimattia / gist:3162093
Created July 23, 2012 05:41
Windows Firewall (COM)
[ComImport, ComVisible(false), Guid("F7898AF5-CAC4-4632-A2EC-DA06E5111AF2"), System.Runtime.InteropServices.InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface INetFwMgr
{
INetFwPolicy LocalPolicy { get; }
FirewallProfileType CurrentProfileType { get; }
void RestoreDefaults();
void IsPortAllowed(string imageFileName, IPVersion ipVersion, long portNumber, string localAddress, IPProtocol ipProtocol, [Out] out bool allowed, [Out] out bool restricted);
void IsIcmpTypeAllowed(IPVersion ipVersion, string localAddress, byte type, [Out] out bool allowed, [Out] out bool restricted);
}
@sandrinodimattia
sandrinodimattia / gist:3162100
Created July 23, 2012 05:41
Windows Firewall (dynamic)
Type fwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr");
dynamic fwMgr = Activator.CreateInstance(fwMgrType);
bool isFwEnabled = fwMgr.LocalPolicy.CurrentProfile.FirewallEnabled;
/// <summary>
/// Msdn reference: http://msdn.microsoft.com/en-us/library/aa366282(v=VS.85).aspx
/// </summary>
public enum NetFwAction
{
Block = 0,
Allow = 1,
Max = 2
}
@sandrinodimattia
sandrinodimattia / gist:3162116
Created July 23, 2012 05:42
NetFwAuthorizedApplication
/// <summary>
/// Msdn reference: http://msdn.microsoft.com/en-us/library/aa365100(v=VS.85).aspx
/// </summary>
public class NetFwAuthorizedApplication
{
...
/// <summary>
/// Initialize the application object.
/// </summary>
@sandrinodimattia
sandrinodimattia / gist:3162117
Created July 23, 2012 05:42
DynamicComMapper
public class DynamicComMapper
{
/// <summary>
/// Map all the properties of the object to the properties of the COM object.
/// </summary>
/// <param name="comObj"></param>
protected void Map(dynamic comObj)
{
foreach (PropertyInfo property in this.GetType().GetProperties())
{
/// <summary>
/// Release a COM object so that it can be cleaned up.
/// </summary>
/// <param name="comObj"></param>
protected void ReleaseCom(dynamic comObj)
{
Marshal.ReleaseComObject(comObj);
}