子曾经曰过

  博客园  :: 首页  ::  ::  ::  :: 管理

工具 P/Invoke Interop Assistant 1.0

网站 http://www.pinvoke.net/default.aspx

简单代码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using System.Management; //需要在项目中添加DLL实体引用
using System.IO;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//button1.Text = serviceController1.ServiceName.ToString();
//try
//{
// dataGridView1.DataSource = Process.GetProcesses(); //调试运行可能运行过程中会出错,因此给运行程序以管理员身份运行

//}
//catch
//{ }

//Process.GetCurrentProcess().CloseMainWindow(); //通过进程关闭窗口
//Process.Start(new ProcessStartInfo(@"D:\wpftestmusic\1.wmv",null));
//Process.Start(new ProcessStartInfo(@"C:\Program Files (x86)\Thunder Network\Xmp\bin\Xmp.exe",@"D:\wpftestmusic\1.wmv"));
//dataGridView1.DataSource = System.ServiceProcess.ServiceController.GetServices();
//dataGridView1.DataSource = System.ServiceProcess.ServiceController.GetDevices();
test t = new test();
System.ServiceProcess.ServiceInstaller installer
= new System.ServiceProcess.ServiceInstaller();



}
public class test : System.ServiceProcess.ServiceBase
{

}

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public ScreenOrientation dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}

[DllImport(
"user32.dll")]
public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);

private void button2_Click(object sender, EventArgs e)
{
DEVMODE vDevMode
= new DEVMODE();
int i = 0;
while (EnumDisplaySettings(null, i, ref vDevMode))
{
richTextBox1.Text
+= vDevMode.dmDisplayFrequency.ToString() + "\n";

i
++;
}

}

private void button3_Click(object sender, EventArgs e)
{
//命名空间using System.Management中包含基础结构的系统、设备和应用程序的访问
//ManagementClass mc = new ManagementClass(); //
//ManagementObjectSearcher query = new ManagementObjectSearcher(new ObjectQuery("SELECT * FROM Win32_Printer")); //建立MOS查询
//ManagementObjectCollection collection; //建立一个集合
//collection = query.Get(); //将查询结果保存到建立的集合中
//foreach (var i in collection) //
//{
// //richTextBox1.Text += i.GetPropertyValue("Name").ToString()+i["Name"].ToString();
// if (string.Compare("fax",i["Name"].ToString(),true)==0) //找到我要设置的打印机
// {
// (i as ManagementObject).InvokeMethod("SetDefaultPrinter", null); //设置为默认打印机
// }
//}
//ConnectionOptions conn = new ConnectionOptions();
//conn.EnablePrivileges = true;
//conn.Username = "jsbc";
//conn.Password = "";
//ManagementScope ms = new ManagementScope(@"\\localhost\root\cimv2", conn);
ManagementScope ms = new ManagementScope();

ManagementObjectSearcher query
= new ManagementObjectSearcher(ms, new ObjectQuery("SELECT * FROM Win32_Environment")); //建立MOS查询
ManagementObjectCollection collection; //建立一个集合
collection = query.Get(); //将查询结果保存到建立的集合中
foreach (var i in collection) //
{
richTextBox1.Text
+= (i as ManagementObject)["VariableValue"] + "\n";
}

}

private const int SPI_SETDESKWALLPAPER = 20;
private const int SPIF_UPDATEINIFILE = 0x1;
private const int SPIF_SENDWININICHANGE = 0x2;

[DllImport(
"user32.dll", CharSet = CharSet.Auto, SetLastError = true)] //DllImport是DllImportAttribute缩写
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

[DllImportAttribute(
"user32.dll", CharSet = CharSet.Auto, SetLastError = true, EntryPoint = "SystemParametersInfoW")] //CharSet = CharSet.Auto,少了这句就不能正确执行,CharSet.Ansi对于函数名末尾加个A的入口点函数,CharSet.Unicode对应末尾加W。CharSet.Auto会自动选择.
public static extern bool SystemParametersInfoW(uint uiAction, uint uiParam, string pvParam, uint fWinIni);

private void button4_Click(object sender, EventArgs e)
{
#region
//unsafe
//{
// int a = 5;
// int* p = &a; //如何得到p就是a存放的内存首地址呢?
// int b = *(p + 1);
// MessageBox.Show(b.ToString());
//}
#endregion
string s = @"D:\wpftestmusic\2.jpg";
//SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, s, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

SystemParametersInfoW(
20, 0, s, 16);
}


[DllImport(
"user32.dll", EntryPoint = "MessageBoxW", CharSet = CharSet.Unicode)] //函数后带W的话CharSet需要设置为Auto或者Unicode
public static extern int MessageBoxW([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpText, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpCaption, uint uType);

private void button5_Click(object sender, EventArgs e)
{
MessageBoxW(
this.Handle, "Text", "Caption", 1);

}






}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using System.Management; //需要在项目中添加DLL实体引用
using System.IO;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//button1.Text = serviceController1.ServiceName.ToString();
//try
//{
// dataGridView1.DataSource = Process.GetProcesses(); //调试运行可能运行过程中会出错,因此给运行程序以管理员身份运行

//}
//catch
//{ }

//Process.GetCurrentProcess().CloseMainWindow(); //通过进程关闭窗口
//Process.Start(new ProcessStartInfo(@"D:\wpftestmusic\1.wmv",null));
//Process.Start(new ProcessStartInfo(@"C:\Program Files (x86)\Thunder Network\Xmp\bin\Xmp.exe",@"D:\wpftestmusic\1.wmv"));
//dataGridView1.DataSource = System.ServiceProcess.ServiceController.GetServices();
//dataGridView1.DataSource = System.ServiceProcess.ServiceController.GetDevices();
test t = new test();
System.ServiceProcess.ServiceInstaller installer
= new System.ServiceProcess.ServiceInstaller();



}
public class test : System.ServiceProcess.ServiceBase
{

}

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public ScreenOrientation dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}

[DllImport(
"user32.dll")]
public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);

private void button2_Click(object sender, EventArgs e)
{
DEVMODE vDevMode
= new DEVMODE();
int i = 0;
while (EnumDisplaySettings(null, i, ref vDevMode))
{
richTextBox1.Text
+= vDevMode.dmDisplayFrequency.ToString() + "\n";

i
++;
}

}

private void button3_Click(object sender, EventArgs e)
{
//命名空间using System.Management中包含基础结构的系统、设备和应用程序的访问
//ManagementClass mc = new ManagementClass(); //
//ManagementObjectSearcher query = new ManagementObjectSearcher(new ObjectQuery("SELECT * FROM Win32_Printer")); //建立MOS查询
//ManagementObjectCollection collection; //建立一个集合
//collection = query.Get(); //将查询结果保存到建立的集合中
//foreach (var i in collection) //
//{
// //richTextBox1.Text += i.GetPropertyValue("Name").ToString()+i["Name"].ToString();
// if (string.Compare("fax",i["Name"].ToString(),true)==0) //找到我要设置的打印机
// {
// (i as ManagementObject).InvokeMethod("SetDefaultPrinter", null); //设置为默认打印机
// }
//}
//ConnectionOptions conn = new ConnectionOptions();
//conn.EnablePrivileges = true;
//conn.Username = "jsbc";
//conn.Password = "";
//ManagementScope ms = new ManagementScope(@"\\localhost\root\cimv2", conn);
ManagementScope ms = new ManagementScope();

ManagementObjectSearcher query
= new ManagementObjectSearcher(ms, new ObjectQuery("SELECT * FROM Win32_Environment")); //建立MOS查询
ManagementObjectCollection collection; //建立一个集合
collection = query.Get(); //将查询结果保存到建立的集合中
foreach (var i in collection) //
{
richTextBox1.Text
+= (i as ManagementObject)["VariableValue"] + "\n";
}

}

private const int SPI_SETDESKWALLPAPER = 20;
private const int SPIF_UPDATEINIFILE = 0x1;
private const int SPIF_SENDWININICHANGE = 0x2;

[DllImport(
"user32.dll", CharSet = CharSet.Auto, SetLastError = true)] //DllImport是DllImportAttribute缩写
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

[DllImportAttribute(
"user32.dll", CharSet = CharSet.Auto, SetLastError = true, EntryPoint = "SystemParametersInfoW")] //CharSet = CharSet.Auto,少了这句就不能正确执行,CharSet.Ansi对于函数名末尾加个A的入口点函数,CharSet.Unicode对应末尾加W。CharSet.Auto会自动选择.
public static extern bool SystemParametersInfoW(uint uiAction, uint uiParam, string pvParam, uint fWinIni);

private void button4_Click(object sender, EventArgs e)
{
#region
//unsafe
//{
// int a = 5;
// int* p = &a; //如何得到p就是a存放的内存首地址呢?
// int b = *(p + 1);
// MessageBox.Show(b.ToString());
//}
#endregion
string s = @"D:\wpftestmusic\2.jpg";
//SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, s, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

SystemParametersInfoW(
20, 0, s, 16);
}


[DllImport(
"user32.dll", EntryPoint = "MessageBoxW", CharSet = CharSet.Unicode)] //函数后带W的话CharSet需要设置为Auto或者Unicode
public static extern int MessageBoxW([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpText, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpCaption, uint uType);

[System.Runtime.InteropServices.DllImportAttribute(
"kernel32.dll", EntryPoint = "GetLocaleInfoW")]
public static extern int GetLocaleInfoW(uint Locale, uint LCType, [System.Runtime.InteropServices.OutAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] System.Text.StringBuilder lpLCData, int cchData);

[DllImport(
"kernel32.dll", SetLastError = true)]
static extern int GetLocaleInfo(uint Locale, uint LCType,
[Out] StringBuilder lpLCData,
int cchData); //这儿为什么[Out]有没有都会有返回值呢?] [Out]表示是可选的,不是必需的



private void button5_Click(object sender, EventArgs e)
{

//MessageBoxW(this.Handle, "Text", "Caption", 1);
StringBuilder sb = new StringBuilder();
GetLocaleInfo(
2048, 4, sb, 255);
//LOCALE_SLANGUAGE 的值为2;SYSTEM_DEFAULT 的值为2048; LOCALE_USER_DEFAULT 的值为1024;LOCALE_SNATIVELANGNAME的值为4;

MessageBox.Show(sb.ToString());
//返回中文简体

}







}
}
posted on 2011-05-25 18:25  人的本质是什么?  阅读(597)  评论(0编辑  收藏  举报