Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从c获取pinvoke SLGetWindowsInformation#_C#_C++_Types_Pinvoke_Type Conversion - Fatal编程技术网

C# 如何从c获取pinvoke SLGetWindowsInformation#

C# 如何从c获取pinvoke SLGetWindowsInformation#,c#,c++,types,pinvoke,type-conversion,C#,C++,Types,Pinvoke,Type Conversion,我知道如何使用pinvoke,但是这个函数中给出的数据结构给我带来的麻烦比我自己能解决的还要多 函数名为SLGetWindowsInformation存在于slc.dll中 HRESULT WINAPI SLGetWindowsInformation( _In_ PCWSTR pwszValueName, _Out_opt_ SLDATATYPE *peDataType, _Out_ UINT *pcbValue, _Out_

我知道如何使用pinvoke,但是这个函数中给出的数据结构给我带来的麻烦比我自己能解决的还要多

函数名为SLGetWindowsInformation存在于slc.dll中

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

提前感谢,祝你度过美好的一天,就像这样:

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分钟内接受答案,干得好,谢谢您的帮助