protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{ if (keyData == Keys.Control) { MessageBox.Show("The A key was pressed");return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{ if (keyData == Keys.Control) { MessageBox.Show("The A key was pressed");return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
public Image byteArrayToImage(byte[] byteArrayIn)
{ MemoryStream ms = new MemoryStream(byteArrayIn);Image returnImage = Image.FromStream(ms);
return returnImage;}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public enum IconSize
{Small,
Large
}
public class IconExtractor
{private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0;
private const uint SHGFI_SMALLICON = 0x1;
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{ public IntPtr hIcon; public IntPtr iIcon;public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("shell32.dll")]private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
public IconExtractor() {}
public System.Drawing.Icon Extract(string File, IconSize Size)
{IntPtr hIcon;
SHFILEINFO shinfo = new SHFILEINFO(); if (Size == IconSize.Large) {hIcon = SHGetFileInfo(File, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
}
else {hIcon = SHGetFileInfo(File, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
}
return System.Drawing.Icon.FromHandle(shinfo.hIcon);}
public System.Drawing.Icon Extract(string File)
{return this.Extract(File, IconSize.Small);
}
}
public List GetComputersOnNetwork(){ List list = new List();using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
{foreach (DirectoryEntry computers in root.Children)
{foreach (DirectoryEntry computer in computers.Children)
{if ((computer.Name != "Schema"))
{list.Add(computer.Name);
comboBox1.Items.Add(computer.Name );
}
}
}
}
return list;}
using System;using System.Runtime.InteropServices;using System.Security;using System.Collections;using System.Windows.Forms;namespace Mesoft.Network{public sealed class NetworkBrowser
{ #region Dll Imports //declare the the Netapi32 : NetServerEnum method import[DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute]
/// <summary> /// Netapi32.dll : The NetServerEnum function lists all servers /// of the specified type that are visible in a domain. For example, an /// application can call NetServerEnum to list all domain controllers only /// or all SQL servers only. /// You can combine bit masks to list several types. For example, a value /// of 0x00000003 combines the bit masks for SV_TYPE_WORKSTATION /// (0x00000001) and SV_TYPE_SERVER (0x00000002) /// </summary>public static extern int NetServerEnum(
string ServerNane, // must be null
int dwLevel, ref IntPtr pBuf, int dwPrefMaxLen,out int dwEntriesRead,
out int dwTotalEntries,
int dwServerType,string domain, // null for login domain
out int dwResumeHandle
);
//declare the Netapi32 : NetApiBufferFree method import[DllImport("Netapi32", SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute]
/// <summary> /// Netapi32.dll : The NetApiBufferFree function frees /// the memory that the NetApiBufferAllocate function allocates. /// Call NetApiBufferFree to free the memory that other network /// management functions return. /// </summary>public static extern int NetApiBufferFree(
IntPtr pBuf);
//create a _SERVER_INFO_100 STRUCTURE[StructLayout(LayoutKind.Sequential)]
public struct _SERVER_INFO_100
{internal int sv100_platform_id;
[MarshalAs(UnmanagedType.LPWStr)]
internal string sv100_name;
}
#endregion #region Public Constructor public NetworkBrowser() {}
#endregion #region Public Methods /// <summary> /// Uses the DllImport : NetServerEnum with all its required parameters /// (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp /// for full details or method signature) to retrieve a list of domain SV_TYPE_WORKSTATION /// and SV_TYPE_SERVER PC's /// </summary> /// <returns>Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER /// PC's in the Domain</returns> public ArrayList getNetworkComputers() { //local fields ArrayList networkComputers = new ArrayList();const int MAX_PREFERRED_LENGTH = -1;
int SV_TYPE_WORKSTATION = 1; int SV_TYPE_SERVER = 2;IntPtr buffer = IntPtr.Zero;
IntPtr tmpBuffer = IntPtr.Zero;
int entriesRead = 0; int totalEntries = 0; int resHandle = 0;int sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
try { //call the DllImport : NetServerEnum with all its required parameters //see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp //for full details of method signatureint ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH,
out entriesRead,out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out
resHandle);
//if the returned with a NERR_Success (C++ term), =0 for C# if (ret == 0) { //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC'sfor (int i = 0; i < totalEntries; i++)
{ //get pointer to, Pointer to the buffer that received the data from //the call to NetServerEnum. Must ensure to use correct size of //STRUCTURE to ensure correct location in memory is pointed totmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
//Have now got a pointer to the list of SV_TYPE_WORKSTATION and //SV_TYPE_SERVER PC's, which is unmanaged memory //Needs to Marshal data from an unmanaged block of memory to a //managed object, again using STRUCTURE to ensure the correct data //is marshalled _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100)); //add the PC names to the ArrayListnetworkComputers.Add(svrInfo.sv100_name);
}
}
}
catch (Exception ex) {MessageBox.Show("Problem with acessing network computers in NetworkBrowser " + "\r\n\r\n\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
finally { //The NetApiBufferFree function frees //the memory that the NetApiBufferAllocate function allocatesNetApiBufferFree(buffer);
}
//return entries found return networkComputers;}
#endregion}
}
private void frmMAIN_Load(object sender, EventArgs e)
{ try {timer1.Start();
Flag = 0;
frmBG ObjBg = new frmBG();ObjBg.MdiParent = ObjMainTemp;
ObjBg.Show();
DataTable dt = ObjGn.FillData("SELECT * FROM MENU ORDER BY SR");for (int i = 0; i < objmenu =" new" forecolor =" Color.White;" dropmenu =" ObjGn.FillData(" menu="'" j =" 0;" objsubmenu =" new" name =" DropMenu.Rows[j][" image =" Image.FromFile(DropMenu.Rows[j]["> 2)
{Flag = 0;
}
Flag++;
if (Flag == 2) {DataTable dt = ObjGn.FillData("SELECT * FROM MENUITEM WHERE ITEM='" + e.ClickedItem.Name + "'");
CSharpCodeProvider c = new CSharpCodeProvider();ICodeCompiler icc = c.CreateCompiler();
CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("system.dll"); cp.ReferencedAssemblies.Add("system.xml.dll"); cp.ReferencedAssemblies.Add("system.data.dll"); cp.ReferencedAssemblies.Add("system.windows.forms.dll"); cp.ReferencedAssemblies.Add("system.drawing.dll"); cp.ReferencedAssemblies.Add("WindowApplicationMenuTest.exe"); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true;StringBuilder sb = new StringBuilder("");
sb.Append("using System;\r\n"); sb.Append("using System.Xml;\r\n"); sb.Append("using System.Data;\r\n"); sb.Append("using System.Data.SqlClient;\r\n"); sb.Append("using System.Windows.Forms;\r\n"); sb.Append("using System.Drawing;\r\n"); sb.Append("using WindowApplicationMenuTest; \r\n"); sb.Append("namespace WindowApplicationMenuTest{ \r\n"); sb.Append("public class CSCodeEvaler{ \r\n"); sb.Append("public object EvalCode(){ \r\n");sb.Append(dt.Rows[0]["FORM"].ToString() + " ObjTemp = new " + dt.Rows[0]["FORM"].ToString() + "();");
sb.Append("ObjTemp.MdiParent = frmMAIN.ObjMainTemp; \r\n"); sb.Append("ObjTemp.Show();\r\n"); sb.Append("ObjTemp.WindowState = FormWindowState.Maximized;\r\n");sb.Append("ObjTemp.Title.Text = \"" + dt.Rows[0]["ITEMPROMPT"].ToString() + "\";\r\n");
sb.Append(" try{");sb.Append("ObjTemp.TempPic.Image = Image.FromFile(\"" + dt.Rows[0]["ICONPATH"].ToString() + "\");");
sb.Append("return ObjTemp; \r\n}"); sb.Append("catch { return null; }"); sb.Append("} \r\n"); sb.Append("} \r\n"); sb.Append("} \r\n");CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
if (cr.Errors.Count > 0) {MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText, " Error evaluating cs code", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
System.Reflection.Assembly a = cr.CompiledAssembly;
object o = a.CreateInstance("WindowApplicationMenuTest.CSCodeEvaler");
Type t = o.GetType();
MethodInfo mi = t.GetMethod("EvalCode");object s = mi.Invoke(o, null);
Flag = 0;
}
}catch(Exception ex){}}