網站首頁 編程語言 正文
一、導入外部DLL函數
如[DllImport(“kernel32.dll”)]
這叫引入kernel32.dll這個動態連接庫。這個動態連接庫里面包含了很多WindowsAPI函數,如果你想使用這面的函數,就需要這么引入。舉個例子:
[DllImport(“kernel32.dll”)] private static extern void FunName(arg,[arg]);
extern 作用:標識這個變量或者函數定義在其他文件 ,提示編譯器遇到此變量的時,在其他模塊里尋找,這里是在提供的動態庫里找
示列代碼:
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); } } }
二、結構體時表明屬性
如[StructLayout(LayoutKind.Sequential) ]
與[StructLayout(LayoutKind.Explicit)]
,首先介紹一下 結構體和類的區別 :類是按引用傳遞 結構體是按值傳遞
進入正題:
結構體是由若干成員組成的.布局有兩種
1.Sequential,順序布局,比如
struct S1{ int a; int b; }
那么默認情況下在內存里是先排a,再排b
也就是如果能取到a的地址,和b的地址,則相差一個int類型的長度,4字節
[StructLayout(LayoutKind.Sequential)] struct S1 { int a; int b; }
這樣和上一個是一樣的.因為默認的內存排列就是Sequential,也就是按成員的先后順序排列.
2.Explicit,精確布局
需要用FieldOffset()設置每個成員的位置
這樣就可以實現類似c的公用體的功能
[StructLayout(LayoutKind.Explicit)] struct S1 { [FieldOffset(0)] int a; [FieldOffset(0)] int b; }
這樣a和b在內存中地址相同
原文鏈接:https://blog.csdn.net/qwq1503/article/details/128767923
相關推薦
- 2022-10-11 云服務器搭建redis主從復制以及哨兵模式(附踩坑記錄)
- 2022-07-16 css盒子模型和css邊框屬性
- 2022-04-10 Python?tkinter實現計算器功能_python
- 2022-10-19 React?Hook實現對話框組件_React
- 2022-06-25 python實現人機對戰的井字棋游戲_python
- 2022-04-22 element的el-drawer預留操作欄問題
- 2022-09-23 Go語言結構體Go?range的學習教程_Golang
- 2022-10-18 C/C++函數指針深入探究_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支