【问题标题】:how to pinvoke SLGetWindowsInformation from c#如何从 c# pinvoke SLGetWindowsInformation
【发布时间】:2015-09-19 17:45:39
【问题描述】:

我知道如何调用,但是这个函数中给出的数据结构给我带来的麻烦比我自己想出来的要多

函数名称是 SLGetWindowsInformation 存在于 slc.dll

    HRESULT WINAPI SLGetWindowsInformation(
  _In_      PCWSTR     pwszValueName,
  _Out_opt_ SLDATATYPE *peDataType,
  _Out_     UINT       *pcbValue,
  _Out_     PBYTE      *ppbValue
);

完整参考here

提前感谢,祝你有美好的一天

【问题讨论】:

    标签: c# c++ types pinvoke type-conversion


    【解决方案1】:

    像这样:

    enum SLDATATYPE 
    {
        SL_DATA_NONE      = REG_NONE,
        SL_DATA_SZ        = REG_SZ,
        SL_DATA_DWORD     = REG_DWORD,
        SL_DATA_BINARY    = REG_BINARY,
        SL_DATA_MULTI_SZ  = REG_MULTI_SZ,
        SL_DATA_SUM       = 100
    };
    // you can look up the values of the REG_XXX constants from the windows header files    
    
    [DllImport("Slc.dll", CharSet = CharSet.Unicode)]
    static extern uint SLGetWindowsInformation(
        string ValueName,
        out SLDATATYPE DataType,
        out uint cbValue,
        out IntPtr Value
    );
    

    这样调用函数:

    SLDATATYPE DataType;
    uint cbValue;
    IntPtr ValuePtr;
    uint res = SLGetWindowsInformation(ValueName, out DataType, out cbValue, out ValuePtr);
    // check that res indicates success before proceeding
    byte[] Value = new byte[cbValue];
    Marshal.Copy(ValuePtr, Value, 0, Value.Length);
    Marshal.FreeHGlobal(ValuePtr);
    

    请注意,这可能看起来有点混乱,但Marshal.FreeHGlobal 实际上调用了LocalFree,因此释放此缓冲区的正确方法是。

    【讨论】:

    • 3分钟内接受答案,干得好,感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2010-12-16
    相关资源
    最近更新 更多