2 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
以下都是調(diào)用API的C#例子
33.讀取ini文件屬性
//using System.Runtime.InteropServices;
//[DllImport("kernel32")]//返回取得字符串緩沖區(qū)的長(zhǎng)度
//private static extern long GetPrivateProfileString(string section,string key, string def,StringBuilder retVal,int size,string filePath);
string Section=%%1;
string Key=%%2;
string NoText=%%3;
string iniFilePath="Setup.ini";
string %%4=String.Empty;
if(File.Exists(iniFilePath)){
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);
%%4=temp.ToString();
}
35.寫入ini文件屬性
//using System.Runtime.InteropServices;
//[DllImport("kernel32")]//返回0表示失敗,非0為成功
//private static extern long WritePrivateProfileString(string section,string key, string val,string filePath);
string Section=%%1;
string Key=%%2;
string Value=%%3;
string iniFilePath="Setup.ini";
bool %%4=false;
if(File.Exists(iniFilePath))
{
long OpStation = WritePrivateProfileString(Section,Key,Value,iniFilePath);
if(OpStation == 0)
{
%%4=false;
}
else
{
%%4=true;
}
}
83.注冊(cè)全局熱鍵
注冊(cè)全局熱鍵要用到Windows的API方法RegisterHotKey和UnregisterHotKey。
一、聲明注冊(cè)熱鍵方法 [DllImport("user32.dll")]
private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern int UnregisterHotKey(IntPtr hwnd, int id);
int Space = 32; //熱鍵ID
private const int WM_HOTKEY = 0x312; //窗口消息-熱鍵
private const int WM_CREATE = 0x1; //窗口消息-創(chuàng)建
private const int WM_DESTROY = 0x2; //窗口消息-銷毀
private const int MOD_ALT = 0x1; //ALT
private const int MOD_CONTROL = 0x2; //CTRL
private const int MOD_SHIFT = 0x4; //SHIFT
private const int VK_SPACE = 0x20; //SPACE
二、注冊(cè)熱鍵方法 /// <summary>
/// 注冊(cè)熱鍵
/// </summary>
/// <param name="hwnd">窗口句柄</param>
/// <param name="hotKey_id">熱鍵ID</param>
/// <param name="fsModifiers">組合鍵</param>
/// <param name="vk">熱鍵</param>
private void RegKey(IntPtr hwnd, int hotKey_id, int fsModifiers, int vk)
{
bool result;
if (RegisterHotKey(hwnd,hotKey_id,fsModifiers,vk) == 0)
{
result = false;
}
else
{
result = true;
}
if (!result)
{
MessageBox.Show("注冊(cè)熱鍵失??!");
}
}
/// <summary>
/// 注銷熱鍵
/// </summary>
/// <param name="hwnd">窗口句柄</param>
/// <param name="hotKey_id">熱鍵ID</param>
private void UnRegKey(IntPtr hwnd, int hotKey_id)
{
UnregisterHotKey(hwnd,hotKey_id);
}
三、重寫WndProc方法,實(shí)現(xiàn)注冊(cè) protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch(m.Msg)
{
case WM_HOTKEY: //窗口消息-熱鍵
switch(m.WParam.ToInt32())
{
case 32: //熱鍵ID
MessageBox.Show("Hot Key : Ctrl + Alt + Shift + Space");
break;
default:
break;
}
break;
case WM_CREATE: //窗口消息-創(chuàng)建
RegKey(Handle,Space,MOD_ALT | MOD_CONTROL | MOD_SHIFT,VK_SPACE); //注冊(cè)熱鍵
break;
case WM_DESTROY: //窗口消息-銷毀
UnRegKey(Handle,Space); //銷毀熱鍵
break;
default:
break;
}
}
84.菜單勾選/取消完成后關(guān)閉計(jì)算機(jī)
/*
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int flg, int rea);
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_FORCEIFHUNG = 0x00000010;
*/
int flg=EWX_FORCE | EWX_POWEROFF;
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(flg, 0);
85.菜單勾選/取消完成后重新啟動(dòng)計(jì)算機(jī)
/*
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int flg, int rea);
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_FORCEIFHUNG = 0x00000010;
*/
int flg=EWX_FORCE | EWX_REBOOT;
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(flg, 0);
86.菜單勾選/取消完成后注銷計(jì)算機(jī)
/*
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int flg, int rea);
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_FORCEIFHUNG = 0x00000010;
*/
int flg=EWX_FORCE | EWX_LOGOFF;
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(flg, 0);
87.菜單勾選/取消開機(jī)自啟動(dòng)程序
public void RunWhenStart(bool Started)
{
string name=%%1;
string path=Application.ExecutablePath;
RegistryKey HKLM = Registry.LocalMachine;
RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\");
if (Started == true)
{
try
{
Run.SetValue(name, path);
HKLM.Close();
}
catch (Exception)
{
MessageBox.Show("注冊(cè)表修改錯(cuò)誤(開機(jī)自啟未實(shí)現(xiàn))");
}
}
else
{
try
{
if (Run.GetValue(name) != null)
{
Run.DeleteValue(name);
HKLM.Close();
}
else
return;
}
catch (Exception e)
{
//ExceptionTransact.WriteErrLog(base.GetType().Name, e.Message);
MessageBox(e.Message);
}
}
}
89.模擬鍵盤輸入字符串
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 KenZhang.Free.VirtualInput;
using System.Runtime.InteropServices;
namespace VirtualInputDemo
{
public partial class Form1 : Form
{
public const int INPUT_KEYBOARD = 1;
public const int KEYEVENTF_KEYUP = 0x0002;
[DllImport("user32.dll")]
public static extern UInt32 SendInput(UInt32 nInputs, ref INPUT pInputs, int cbSize);
[StructLayout(LayoutKind.Explicit)]
public struct INPUT
{
[FieldOffset(0)]
public Int32 type;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT
{
public Int32 dx;
public Int32 dy;
public Int32 mouseData;
public Int32 dwFlags;
public Int32 time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
public Int16 wVk;
public Int16 wScan;
public Int32 dwFlags;
public Int32 time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct HARDWAREINPUT
{
public Int32 uMsg;
public Int16 wParamL;
public Int16 wParamH;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
INPUT inDown = new INPUT();
inDown.type = INPUT_KEYBOARD;
inDown.ki.wVk = (int)Keys.A;
//INPUT inUp = new INPUT();
//inUp.type = INPUT_KEYBOARD;
//inUp.ki.wVk = (int)Keys.A;
//inUp.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, ref inDown, Marshal.SizeOf(inDown));
//SendInput(1, ref inUp, Marshal.SizeOf(inUp));
}
}
}

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果查看所有進(jìn)程的DLL文件,運(yùn)行CMD:
tasklist /m
運(yùn)行后顯示所有進(jìn)程加載的DLL文件.
假如我們查看進(jìn)程iexplore.exe調(diào)用或加載的模塊文件,運(yùn)行:
tasklist /m iexplore.exe
運(yùn)行后顯示所有該應(yīng)用程序的DLL文件.
查看DLL:
一個(gè)很簡(jiǎn)單的方法就是用.NET自帶的命令行工具
1.首先啟動(dòng)vs的命令行工具
2.定位到你的DLL的那個(gè)目錄
3.然后輸入" ildasm a.dll;注意:a.dll是你的dll的名字 就可以了
不知你問的是不是這個(gè)
希望能幫上你
- 2 回答
- 0 關(guān)注
- 101 瀏覽
添加回答
舉報(bào)