網(wǎng)站首頁 編程語言 正文
一、導(dǎo)入外部DLL函數(shù)
如[DllImport(“kernel32.dll”)]
這叫引入kernel32.dll這個(gè)動(dòng)態(tài)連接庫。這個(gè)動(dòng)態(tài)連接庫里面包含了很多WindowsAPI函數(shù),如果你想使用這面的函數(shù),就需要這么引入。舉個(gè)例子:
[DllImport(“kernel32.dll”)] private static extern void FunName(arg,[arg]);
extern 作用:標(biāo)識這個(gè)變量或者函數(shù)定義在其他文件 ,提示編譯器遇到此變量的時(shí),在其他模塊里尋找,這里是在提供的動(dòng)態(tài)庫里找
示列代碼:
using System; using System.Text; using System.Runtime.InteropServices; namespace Windows.Help { public partial class SystemInfo { [DllImport("kernel32")] public static extern void GetWindowsDirectory(StringBuilder WinDir, int count); [DllImport("kernel32")] public static extern void GetSystemDirectory(StringBuilder SysDir, int count); public static void Main () { const int nChars = 128; StringBuilder Buff = new StringBuilder(nChars); GetWindowsDirectory(Buff, nChars); String t = "Windows路徑:" + Buff.ToString(); System.Console.WriteLine(t); } } }
二、結(jié)構(gòu)體時(shí)表明屬性
如[StructLayout(LayoutKind.Sequential) ]
與[StructLayout(LayoutKind.Explicit)]
,首先介紹一下 結(jié)構(gòu)體和類的區(qū)別 :類是按引用傳遞 結(jié)構(gòu)體是按值傳遞
進(jìn)入正題:
結(jié)構(gòu)體是由若干成員組成的.布局有兩種
1.Sequential,順序布局,比如
struct S1{ int a; int b; }
那么默認(rèn)情況下在內(nèi)存里是先排a,再排b
也就是如果能取到a的地址,和b的地址,則相差一個(gè)int類型的長度,4字節(jié)
[StructLayout(LayoutKind.Sequential)] struct S1 { int a; int b; }
這樣和上一個(gè)是一樣的.因?yàn)槟J(rèn)的內(nèi)存排列就是Sequential,也就是按成員的先后順序排列.
2.Explicit,精確布局
需要用FieldOffset()設(shè)置每個(gè)成員的位置
這樣就可以實(shí)現(xiàn)類似c的公用體的功能
[StructLayout(LayoutKind.Explicit)] struct S1 { [FieldOffset(0)] int a; [FieldOffset(0)] int b; }
這樣a和b在內(nèi)存中地址相同
總結(jié)
原文鏈接:https://blog.csdn.net/qwq1503/article/details/128767923
相關(guān)推薦
- 2022-12-22 C語言中字母大小寫轉(zhuǎn)化簡單示例_C 語言
- 2022-07-07 go語言心跳超時(shí)的實(shí)現(xiàn)示例_Golang
- 2023-02-12 Golang如何構(gòu)造最佳隨機(jī)密碼詳解_Golang
- 2023-08-16 map()函數(shù)新數(shù)組不能遍歷
- 2022-08-17 python數(shù)據(jù)可視化matplotlib繪制折線圖示例_python
- 2022-07-23 .Net創(chuàng)建型設(shè)計(jì)模式之簡單工廠模式(Simple?Factory)_基礎(chǔ)應(yīng)用
- 2022-04-18 python?如何讀取列表中字典的value值_python
- 2022-11-20 C#?崩潰異常中研究頁堆布局的詳細(xì)過程_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支