PowerShell-4.API调用以及DLL调用
生活随笔
收集整理的這篇文章主要介紹了
PowerShell-4.API调用以及DLL调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PowerShell可以直接調用API,So...這東西完全和cmd不是一回事了...
調用API的時候幾乎和C#一樣(注意堆棧平衡):
調用MessageBox:
?
$iii = Add-Type -memberDefinition @" [DllImport("User32")] public static extern int MessageBox ( long hWnd, string lpText, string lpCaption, int uType); "@ -passthru -name XXX$iii::MessageBox(0 ,'test' ,'tit' ,0)
PowerShell 調用我們自己的dll
(下面是網上粘貼的別人的,我本意是要調用C++的dll,但是按照C#的姿勢調用失敗了,現在采取的方案是通過rundll32來橋接PowerShell調用C++的dll。)
?
用C#寫一段代碼編譯為DLL文件
namespace Math{public class Methods {public Methods() {}public static int CompareI(int a, int b) {if (a>b) return a;else return b;}public int CompareII(int a, int b) {if (a>b) return a;else return b;}} }[void][reflection.assembly]::LoadFile("G:/Math2.dll") [Math.methods]::CompareI(10,2) $a=New-Object Math.Methods $a.CompareII(2,3) [void][reflection.assembly]::LoadFile("D:/VS2008/VC/Math2.dll") 我們必須以這種形式加載DLL庫 [Math.methods]::CompareI(10,2) 我們看到Powershell在調用靜態方法的時候必須使用方括號加上雙冒號“::”的形式來調用靜態方法。 $a=New-Object Math.Methods $a.CompareII(2,3) 而一般方法則不然,必須用New-Object來聲明一個對象引用。總結
以上是生活随笔為你收集整理的PowerShell-4.API调用以及DLL调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows核心编程 第十七章 -内存
- 下一篇: PowerShell-5.网络请求