vb.net 有專門的托盤圖標控件 在工具箱里直接添加一個就可以了 兩行代碼即可
創(chuàng)新互聯(lián)公司主營盂縣網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,app軟件開發(fā),盂縣h5微信小程序開發(fā)搭建,盂縣網(wǎng)站營銷推廣歡迎盂縣等地區(qū)企業(yè)咨詢
NotifyIcon1.Icon?=?Me.Icon
NotifyIcon1.Visible?=?True
這說明你調(diào)用 API 傳參存在問題。
首先有沒有設置結(jié)構(gòu)體內(nèi)存對齊?
[StructLayout(LayoutKind.Sequential)] 加了嗎?
COLORREF 不要用 Color 代替,用 int 代替
TCHAR cfFaceName[32] 定義為 Byte 數(shù)組也可以,帶在傳參前,必須初始化為 32 個長度
Imports System.Runtime.InteropServices ‘引入
’ pstream為32位內(nèi)存地址值
' nLenght為內(nèi)存長度
Dim ptr As New System.IntPtr(pstream) '定義一個地址為流基地址的非托管指針
Dim ReadArray(nLenght - 1) As Byte '定義數(shù)組
Marshal.Copy(ptr, ReadArray, 0, nLenght) '將數(shù)據(jù)從非托管內(nèi)存指針復制到托管 8 位無符號整數(shù)數(shù)組
你是想讀取游戲進程吧 這個問題剛開始也 讓我郁悶了很久 其實很多 游戲?qū)ψx取內(nèi)存做了特殊處理 常規(guī)的 調(diào)用api打開進程 讀取內(nèi)存 無效可以嘗試 用token 直接調(diào)用就ok了 返回true說明調(diào)用成功下面是詳細代碼 Public Class ToKen
#Region "常數(shù)及結(jié)構(gòu)聲明"
Private Const SE_PRIVILEGE_ENABLED As Int32 = 2
Private Const EWX_SHUTDOWN As Int32 = 1
Private Const EWX_REBOOT As Int32 = 2
Private Const EWX_LOGOFF As Int32 = 0
Private Structure LUID_AND_ATTRIBUTES
Public pLuid As LUID
Public Attributes As Integer
End Structure
Private Structure LUID
Dim LowPart As Int32
Dim HighPart As Int32
End Structure
Private Structure TOKEN_PRIVILEGES
Public PrivilegeCount As Integer
Public Privileges As LUID
Public Attributes As Int32
End Structure
#End Region#Region "API聲明"
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Int32
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Int32, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Int32, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Int32) As Int32
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Boolean
#End Region
#Region "獲取全部權(quán)限"
Public Function ToKenPrivileges() As Boolean
Dim hdlTokenHandle As Integer
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Integer
Dim currentProcess As Process = Process.GetCurrentProcess()
If OpenProcessToken(currentProcess.Handle, HF00FF, hdlTokenHandle) Then
LookupPrivilegeValue("", "SeDebugPrivilege", tmpLuid)
tkp.PrivilegeCount = 1
tkp.Privileges = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
Return AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
End If
End Function
#End Region
End Class
使用FileStream讀寫文件
文件頭:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
讀文件核心代碼:
byte[] byData = new byte[100];
char[] charData = new char[1000];
try
{
FileStream sFile = new FileStream("文件路徑",FileMode.Open);
sFile.Seek(55, SeekOrigin.Begin);
sFile.Read(byData, 0, 100); //第一個參數(shù)是被傳進來的字節(jié)數(shù)組,用以接受FileStream對象中的數(shù)據(jù),第2個參數(shù)是字節(jié)數(shù)組中開始寫入數(shù)據(jù)的位置,它通常是0,表示從數(shù)組的開端文件中向數(shù)組寫數(shù)據(jù),最后一個參數(shù)規(guī)定從文件讀多少字符.
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadLine();
return;
}
Decoder d = Encoding.UTF8.GetDecoder();
d.GetChars(byData, 0, byData.Length, charData, 0);
Console.WriteLine(charData);
Console.ReadLine();
寫文件核心代碼:
FileStream fs = new FileStream(文件路徑,FileMode.Create);
//獲得字節(jié)數(shù)組
byte [] data =new UTF8Encoding().GetBytes(String);
//開始寫入
fs.Write(data,0,data.Length);
//清空緩沖區(qū)、關(guān)閉流
fs.Flush();
fs.Close();
2、使用StreamReader和StreamWriter
文件頭:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
StreamReader讀取文件:
StreamReader objReader = new StreamReader(文件路徑);
string sLine="";
ArrayList LineList = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null!sLine.Equals(""))
LineList.Add(sLine);
}
objReader.Close();
return LineList;
StreamWriter寫文件:
FileStream fs = new FileStream(文件路徑, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//開始寫入
sw.Write(String);
//清空緩沖區(qū)
sw.Flush();
//關(guān)閉流
sw.Close();
fs.Close();
===================================================================================
方式一:用FileStream
//實例化一個保存文件對話框
SaveFileDialog sf = new SaveFileDialog();
//設置文件保存類型
sf.Filter = "txt文件|*.txt|所有文件|*.*";
//如果用戶沒有輸入擴展名,自動追加后綴
sf.AddExtension = true;
//設置標題
sf.Title = "寫文件";
//如果用戶點擊了保存按鈕
if(sf.ShowDialog()==DialogResult.OK)
{
//實例化一個文件流---與寫入文件相關(guān)聯(lián)
FileStream fs = new FileStream(sf.FileName,FileMode.Create);
//獲得字節(jié)數(shù)組
byte [] data =new UTF8Encoding().GetBytes(this.textBox1.Text);
//開始寫入
fs.Write(data,0,data.Length);
//清空緩沖區(qū)、關(guān)閉流
fs.Flush();
fs.Close();
}
方式二:用StreamWriter
//實例化一個保存文件對話框
SaveFileDialog sf = new SaveFileDialog();
//設置文件保存類型
sf.Filter = "txt文件|*.txt|所有文件|*.*";
//如果用戶沒有輸入擴展名,自動追加后綴
sf.AddExtension = true;
//設置標題
sf.Title = "寫文件";
//如果用戶點擊了保存按鈕
if (sf.ShowDialog() == DialogResult.OK)
{
//實例化一個文件流---與寫入文件相關(guān)聯(lián)
FileStream fs = new FileStream(sf.FileName, FileMode.Create);
//實例化一個StreamWriter--與fs相關(guān)聯(lián)
StreamWriter sw = new StreamWriter(fs);
//開始寫入
sw.Write(this.textBox1.Text);
//清空緩沖區(qū)
sw.Flush();
//關(guān)閉流
sw.Close();
fs.Close();
}
string FileName = Guid.NewGuid().ToString() + ".txt"; //GUID生成唯一文件名
StringBuilder ckpw = new StringBuilder("\"憑證輸出\", \"V800\", \"001\", \"東風隨州專用汽車有限公司\"," + "\"F89自由項16\", \"F90審核日期:\"");
if (!FileIO.IsFolderExists(Server.MapPath("pzsc")))
FileIO.CreaterFolder(Server.MapPath(""), "");
string filePath = Server.MapPath("pzsc") + "\\" + FileName;
System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, false, Encoding.GetEncoding("GB2312"));//創(chuàng)建的時候需要指定編碼格式,默認是UTF-8,中文顯示亂碼
sw.WriteLine(ckpw.ToString());
sw.Close();
方式三:用BinaryWriter
//實例化一個保存文件對話框
SaveFileDialog sf = new SaveFileDialog();
//設置文件保存類型
sf.Filter = "txt文件|*.txt|所有文件|*.*";
//如果用戶沒有輸入擴展名,自動追加后綴
sf.AddExtension = true;
//設置標題
sf.Title = "寫文件";
//如果用戶點擊了保存按鈕
if (sf.ShowDialog() == DialogResult.OK)
{
//實例化一個文件流---與寫入文件相關(guān)聯(lián)
FileStream fs = new FileStream(sf.FileName, FileMode.Create);
//實例化BinaryWriter
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(this.textBox1.Text);
//清空緩沖區(qū)
bw.Flush();
//關(guān)閉流
bw.Close();
fs.Close();
}
C#緩存流示例------用緩存流復制文件
C#文件處理操作必須先導入命名空間:using System.IO;
背景:使用VS2005、一個按鈕、一個窗體、C#緩存流、把D:\KuGoo\愛得太多.wma復制到D:\并更名為love.wma,即:D:\love.wma
在按鈕的Click事件中添加如下代碼:
private void button1_Click(object sender, EventArgs e)
{
//創(chuàng)建兩個文件流 一個是源文件相關(guān),另一個是要寫入的文件
FileStream fs = new FileStream(@"D:\KuGoo\愛得太多.wma",FileMode.Open);
FileStream fs2 = new FileStream(@"D:\love.wma",FileMode.Create);
//創(chuàng)建一個字節(jié)數(shù)組,作為兩者之間的媒介
//好比兩個人拿蘋果,這個字節(jié)數(shù)組就好比一個籃子,一個人作死的把蘋果送到籃子里面,
//而我就可以作死得拿蘋果,通過這個媒介我們互不干擾,
//不需要互相等待【她往籃子里面放了蘋果我才可以去拿】,提高了效率
byte[] data = new byte[1024];
//創(chuàng)建兩個緩沖流,與兩個文件流相關(guān)聯(lián)
BufferedStream bs = new BufferedStream(fs);
BufferedStream bs2= new BufferedStream(fs2);
//fs作死的讀,fs2作死的寫,直到fs沒有字節(jié)可讀fs2就不寫了
//好比,一個人作死的往籃子里面丟蘋果,另一個人作死得往籃子里面拿蘋果,直到籃子里面沒有蘋果拿了為止
//即--那個人沒有蘋果往籃子里面放了
while(fs.Read(data,0,data.Length)0)
{
fs2.Write(data,0,data.Length);
fs2.Flush();
}
//關(guān)閉流,好比兩個人累了,都要休息 呵呵o(∩_∩)o...
fs.Close();
fs2.Close();
}
C#內(nèi)存流示例-----用內(nèi)存流來讀取圖片
C#文件處理操作必須先導入命名空間:using System.IO;
背景:一個窗體、一個pictureBox、一個lable[沒有選擇圖片,lable的text為"圖片未選擇"],在pictureBox1的Click事件中添加如下代碼:
private void pictureBox1_Click(object sender, EventArgs e)
{
//實例化一個打開文件對話框
OpenFileDialog op = new OpenFileDialog();
//設置文件的類型
op.Filter = "JPG圖片|*.jpg|GIF圖片|*.gif";
//如果用戶點擊了打開按鈕、選擇了正確的圖片路徑則進行如下操作:
if(op.ShowDialog()==DialogResult.OK)
{
//清空文本
this.label1.Text = "";
//實例化一個文件流
FileStream fs = new FileStream(op.FileName, FileMode.Open);
//把文件讀取到字節(jié)數(shù)組
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
//實例化一個內(nèi)存流---把從文件流中讀取的內(nèi)容[字節(jié)數(shù)組]放到內(nèi)存流中去
MemoryStream ms = new MemoryStream(data);
//設置圖片框 pictureBox1中的圖片
this.pictureBox1.Image = Image.FromStream(ms);
}
}
Imports System.Runtime.InteropServices
Public Class MemoryEditor
Inherits WINAPI.NativeMethods
Private phwnd As IntPtr
Private Buffer As Byte()
Private BytesRead As IntPtr
Private BytesWrite As IntPtr
''' summary創(chuàng)建內(nèi)存編輯器/summary
''' param name="processHwnd"進程句柄/param
Sub New(processHwnd As IntPtr)
Me.phwnd = processHwnd
End Sub
''' summary根據(jù)指定偏移量讀取內(nèi)存基址/summary
''' param name="addr"內(nèi)存地址/param
''' param name="offsets"偏移量數(shù)組/param
Public Function ReadBaseAddress(addr As IntPtr, offsets() As Integer) As IntPtr
Dim address As IntPtr = ReadMemoryToInteger(addr)
For Each offset As Integer In offsets
address = address.ToInt32 + offset
address = ReadMemoryToInteger(address)
If address = IntPtr.Zero Then
Dim errInfo As String = "內(nèi)存偏移量[" Hex(offset) "]錯誤!"
Throw New Exception(errInfo)
End If
Next
Return address
End Function
''' summary讀取4字節(jié)內(nèi)存數(shù)值/summary
''' param name="addr"內(nèi)存地址/param
Public Function ReadMemoryToInteger(addr As IntPtr) As Integer
Buffer = New Byte(3) {}
ReadProcessMemory(phwnd, addr, Buffer, 4, BytesRead)
Return BitConverter.ToInt32(Buffer, 0)
End Function
''' summary讀取4字節(jié)內(nèi)存數(shù)組/summary
''' param name="addr"內(nèi)存地址/param
Public Function ReadMemoryToBytes(addr As IntPtr) As Byte()
Buffer = New Byte(3) {}
ReadProcessMemory(phwnd, addr, Buffer, 4, BytesRead)
Return Buffer
End Function
''' summary將內(nèi)存值數(shù)組寫入指定地址/summary
''' param name="addr"內(nèi)存地址/param
''' param name="buffer"內(nèi)存值數(shù)組/param
Public Function WriteMemoryByBytes(addr As IntPtr, buffer As Byte()) As Boolean
Return WriteProcessMemory(phwnd, addr, buffer, buffer.Length, BytesWrite)
End Function
End Class
Namespace WINAPI
Public MustInherit Class NativeMethods
DllImport("kernel32.dll", SetLastError:=True) _
Public Shared Function ReadProcessMemory(ByVal hProcess As IntPtr, _
ByVal lpBaseAddress As IntPtr, _
Out() ByVal lpBuffer() As Byte, _
ByVal dwSize As Integer, _
ByRef lpBytesRead As Integer) As Boolean
End Function
DllImport("kernel32.dll", SetLastError:=True) _
Public Shared Function WriteProcessMemory(ByVal hProcess As IntPtr, _
ByVal lpBaseAddress As IntPtr, _
ByVal lpBuffer As Byte(), _
ByVal nSize As Integer, _
Out() ByRef lpBytesWritten As IntPtr) As Boolean
End Function
End Class
End Namespace
是不是內(nèi)存讀寫代碼有問題!用上面的代碼試試,我以前寫的;
調(diào)用方法:
Dim mem As New MemoryEditor(進程句柄)
Dim offsets As Integer() = {H1, H2, H3} '{一級基址,二級基址,三級基址}
Dim baseaddr As IntPtr = mem.ReadBaseAddress(內(nèi)存地址, offsets)
Dim value As Integer = mem.ReadMemoryToInteger(baseaddr)
新聞標題:vb.net讀寫內(nèi)存 c#讀內(nèi)存
新聞來源:http://chinadenli.net/article48/hgdeep.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、品牌網(wǎng)站制作、電子商務、服務器托管、做網(wǎng)站、App設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)