zoukankan      html  css  js  c++  java
  • 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来声明一个对象引用。


  • 相关阅读:
    Codeforces 1132D
    Codeforces 670F
    Codeforces 670E
    Codeforces 670E
    Codeforces 670E
    Codeforces 670
    Codeforces 1138
    Codeforces 1114E
    力扣21.合并两个有序链表
    力扣538.把二叉树转换为累加树
  • 原文地址:https://www.cnblogs.com/csnd/p/12062118.html
Copyright © 2011-2022 走看看