I use SHGetFileInfo() or GetDisplayNameOf() to get the name of a special folder.
If a localized operating system to change the setting "Current language for non-Unicode programs", these functions return a value "??? ?????????".
This combination of settings encountered by users.
shell32 is not fully unicode compatible?
Shell32.STRRET STRRET;
STRRET.uType = (uint)Shell32.STRRET_TYPE.STRRET_WSTR;
if (Windows.S_OK != ishellfolder_parent.GetDisplayNameOf(ptr_pidllast, (uint)Shell32.SHGNO.SHGDN_NORMAL | (uint)Shell32.SHGNO.SHGDN_INFOLDER, out STRRET))
return null;
StringBuilder sbuilder = new StringBuilder(260);
Shell32.StrRetToBuf(ref STRRET, ptr_pidllast, sbuilder, (uint)sbuilder.Capacity);
what is wrong?
***added later
Another example to demonstrate my question:
public static partial class Program
{
const Int32 CSIDL_DESKTOP = (0x0000);
const uint SHGFI_DISPLAYNAME = 0x000000200; // get display name
const uint SHGFI_PIDL = 0x000000008; // pszPath is a pidl
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public static int NAMESIZE = 80;
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("shell32.dll")]
static extern IntPtr SHGetFileInfo(IntPtr pidl, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
[DllImport("shell32.dll")]
public static extern IntPtr SHCloneSpecialIDList(IntPtr hwnd, Int32 CSIDL, bool create);
[STAThread]
static void Main(string[] args)
{
IntPtr pidl = SHCloneSpecialIDList(IntPtr.Zero, CSIDL_DESKTOP, false);
SHFILEINFO shfi = new SHFILEINFO();
if (IntPtr.Zero != SHGetFileInfo(
pidl,
0,
ref shfi,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),
SHGFI_PIDL | SHGFI_DISPLAYNAME))
{
System.Windows.Forms.MessageBox.Show(shfi.szDisplayName);
}
This code not correct. Some cases wrong return values, described above.
Can anyone help me with an example of the correct code, fully compatible with Unicode and works with non-default system settings?
Thank you all! After some experiments, and found a solution. My error was here:
Shell32.StrRetToBuf(ref STRRET, ptr_pidllast, sbuilder, (uint)sbuilder.Capacity);
The signature should be:
[DllImport("shlwapi.dll", CharSet=CharSet.Unicode, EntryPoint="StrRetToBufW")]
public static extern Int32 StrRetToBufW( ...
Someone asked the same question. You can use the code example there.
How to get the actual (localized) folder names?
Related
I have implemented all required code to get file type name from file path and it is working successfully but it returns subtracted type name e.g. if file path of .pdb or .pdf file then it will return "obe Acrobat Document" instead of "Adobe Acrobat Document"
I have used shell32.dll. I am not getting what happens please, help me to get rid out of it.
Source code:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
internal struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
internal class Win32
{
public const uint FILE_ATTRIBUTE_NORMAL = 0x80;
public const uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
public const uint SHGFI_TYPENAME = 0x000000400;
public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
internal const uint SHGFI_SYSICONINDEX = 0x000004000;
internal const int ILD_TRANSPARENT = 0x1;
internal const uint SHGFI_ICON = 0x100;
internal const uint SHGFI_LARGEICON = 0x0;
internal const uint SHGFI_SMALLICON = 0x1;
[DllImport("shell32.dll", CharSet=CharSet.Unicode)]
internal static extern IntPtr SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbSizeFileInfo,
uint uFlags
);
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
internal static extern int ExtractIconEx
(
string stExeFileName,
int nIconIndex,
ref IntPtr phiconLarge,
ref IntPtr phiconSmall,
int nIcons
);
[DllImport("comctl32.dll", SetLastError = true)]
internal static extern IntPtr ImageList_GetIcon
(
IntPtr himl,
int i,
int flags
);
[DllImport("user32.dll")]
internal static extern bool DestroyIcon(IntPtr hIcon);
}
internal static string GetFileType(string filename)
{
SHFILEINFO shinfo = new SHFILEINFO();
Win32.SHGetFileInfo
(
filename,
Win32.FILE_ATTRIBUTE_NORMAL,
ref shinfo, (uint)Marshal.SizeOf(shinfo),
Win32.SHGFI_TYPENAME |
Win32.SHGFI_USEFILEATTRIBUTES
);
return shinfo.szTypeName; //It return "obe Acrobat Document" Instead of "Adobe Acrobat Document"
}
The iIcon field in the C++ struct has type int.So, I just have to set the type of iIcon int only, not IntPtr. IntPtr works as per system platform so. I just set int type to iIcon like,
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
internal struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
And It's working fine...
I am trying to set the icon of a shortcut pointing to a folder, but cannot find any resources on how to set a shortcut icon to a native icon from shell32.dll. I found This answer on msdn by Rykler, but the answer is outdated. Any help would be appreciated.
Code
SHFILEINFO shinfo = new SHFILEINFO();
Win32.SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)0x1000);
// icon index # and path
int IconIndex = shinfo.iIcon
string IconPath = shinfo.szDisplayName
shortcut.IconLocation = ???
SHFILEINFO struct: (Taken from This question)
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
class Win32
{
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint
dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}
So, after a little research, it seems not too complicate. At first, take the interface declarations from this question. Include the declarations in your project.
After that, you can use this code:
public class Q47602417
{
public static void MakeShortcut(string targetPath)
{
var shellLink = new ShellLink();
((IShellLinkW)shellLink).SetDescription("Sample Shortcut");
((IShellLinkW)shellLink).SetPath(targetPath);
((IShellLinkW)shellLink).SetIconLocation(Environment.SystemDirectory + "\\Shell32.dll", 12);
((IPersistFile)shellLink).Save(#"C:\temp\shortcut.lnk", false);
}
}
One thing to add: this sample contains no error checking! You don't know if the shortcut was created or not. So, change the called interface methods to return integers and add exception checking:
//...
int SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
int SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
int SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
//.. omitted other details ...
int Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName, [In, MarshalAs(UnmanagedType.Bool)] bool fRemember);
Now you can check if creating the shortcut acutally works.
public class Q47602417
{
public static void MakeShortcut(string targetPath)
{
ShellLink shellLink = new ShellLink();
int hr;
hr = ((IShellLinkW)shellLink).SetDescription("Sample Shortcut");
Marshal.ThrowExceptionForHR(hr);
hr = ((IShellLinkW)shellLink).SetPath(targetPath);
Marshal.ThrowExceptionForHR(hr);
hr = ((IShellLinkW)shellLink).SetIconLocation(Environment.SystemDirectory + "\\Shell32.dll", 12);
Marshal.ThrowExceptionForHR(hr);
hr = ((IPersistFile)shellLink).Save(#"C:\temp\shortcut.lnk", false);
Marshal.ThrowExceptionForHR(hr);
}
}
Example result:
For my program I need to get detailed information about the current displays. In my research I came across this post with talks about linking the System.Windows.Forms.Screen class and its EDID information. At first I tried copying and pasting the code found with using p/invoke to supply all the required native methods and structs, but it did not work and only gave me a string of ? for the InstanceID. So instead I tried to use the MSDN resources and again p/invoke to create the code myself. This is what I came up with:
private static void Foo()
{
Guid DisplayGUID = new Guid(Bar.GUID_DEVINTERFACE_MONITOR);
IntPtr DisplaysHandle = Bar.SetupDiGetClassDevs(ref DisplayGUID, null, IntPtr.Zero, (uint)(Win32.DIGCF_PRESENT | Win32.DIGCF_DEVICEINTERFACE));
Bar.SP_DEVICE_INTERFACE_DATA Data = new Bar.SP_DEVICE_INTERFACE_DATA();
Data.cbSize = Marshal.SizeOf(Data);
for (uint id = 0; Bar.SetupDiEnumDeviceInterfaces(DisplaysHandle, IntPtr.Zero, ref DisplayGUID, id, ref Data); id++)
{
Bar.SP_DEVINFO_DATA SPDID = new Bar.SP_DEVINFO_DATA();
SPDID.cbSize = (uint)Marshal.SizeOf(SPDID);
Bar.SP_DEVICE_INTERFACE_DETAIL_DATA NDIDD = new Bar.SP_DEVICE_INTERFACE_DETAIL_DATA();
if (IntPtr.Size == 8) //64 bit
NDIDD.cbSize = 8;
else //32 bit
NDIDD.cbSize = 4 + Marshal.SystemDefaultCharSize;
uint requiredsize = 0;
uint buffer = Bar.BUFFER_SIZE;
if (Bar.SetupDiGetDeviceInterfaceDetail(DisplaysHandle, ref Data, ref NDIDD, buffer, ref requiredsize, ref SPDID))
{
uint size = 0;
Bar.CM_Get_Device_ID_Size(out size, SPDID.DevInst);
IntPtr ptrInstanceBuf = Marshal.AllocHGlobal((int)size);
Bar.CM_Get_Device_ID(SPDID.DevInst, ref ptrInstanceBuf, size);
string InstanceID = Marshal.PtrToStringAuto(ptrInstanceBuf);
Console.WriteLine("InstanceID: {0}", InstanceID);
Marshal.FreeHGlobal(ptrInstanceBuf);
Console.WriteLine("DevicePath: {0}\n", NDIDD.DevicePath);
}
}
Bar.SetupDiDestroyDeviceInfoList(DisplaysHandle);
}
private class Bar
{
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, [MarshalAs(UnmanagedType.LPTStr)] string Enumerator, IntPtr hwndParent, uint Flags);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport(#"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
[DllImport(#"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr hDevInfo, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, UInt32 deviceInterfaceDetailDataSize, ref UInt32 requiredSize, ref SP_DEVINFO_DATA deviceInfoData);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern int CM_Get_Device_ID_Size(out uint pulLen, UInt32 dnDevInst, int flags = 0);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern int CM_Get_Device_ID(uint dnDevInst, ref IntPtr Buffer, uint BufferLen, int ulFlags = 0);
public const int BUFFER_SIZE = 168; //guess
public const string GUID_DEVINTERFACE_MONITOR = "{E6F07B5F-EE97-4a90-B076-33F57BF4EAA7}";
[Flags]
public enum DiGetClassFlags : uint
{
DIGCF_DEFAULT = 0x00000001, // only valid with DIGCF_DEVICEINTERFACE
DIGCF_PRESENT = 0x00000002,
DIGCF_ALLCLASSES = 0x00000004,
DIGCF_PROFILE = 0x00000008,
DIGCF_DEVICEINTERFACE = 0x00000010,
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
public Int32 cbSize;
public Guid interfaceClassGuid;
public Int32 flags;
private UIntPtr reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVINFO_DATA
{
public uint cbSize;
public Guid classGuid;
public uint DevInst;
public IntPtr reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string DevicePath;
}
}
My code compiles and runs, but it does not give me the output I am looking for.
The output that I am looking for is:
InstanceID: DISPLAY\DELA00B\5&786e6ca&0&UID1048832
DevicePath: \\?\display#dela00b#5&786e6ca&0&uid1048832#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}
But this is the output I am receiving:
InstanceID: l
DevicePath: \\?\display#dela00b#5&786e6ca&0&uid1048832#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}
My question comes in the form of what is the problem causing the InstanceID to not output correctly.
Turns out I was using the wrong p/invoke signature. CM_Get_Device_ID should look like this:
[DllImport("setupapi.dll", SetLastError = true)]
public static extern int CM_Get_Device_ID(uint dnDevInst, StringBuilder Buffer, int BufferLen, int ulFlags = 0);
Also Updated Usage Code:
StringBuilder IDBuffer = new StringBuilder((int)buffer);
Bar.CM_Get_Device_ID(SPDID.DevInst, IDBuffer, (int)buffer);
Console.WriteLine("InstanceID: {0}", IDBuffer.ToString());
Console.WriteLine("DevicePath: {0}\n", NDIDD.DevicePath);
How can I use the images within shell32.dll in my C# project?
You can extract icons from a DLL with this code:
public class IconExtractor
{
public static Icon Extract(string file, int number, bool largeIcon)
{
IntPtr large;
IntPtr small;
ExtractIconEx(file, number, out large, out small, 1);
try
{
return Icon.FromHandle(largeIcon ? large : small);
}
catch
{
return null;
}
}
[DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);
}
...
form.Icon = IconExtractor.Extract("shell32.dll", 42, true);
Of course you need to know the index of the image in the DLL...
This thread on the MSDN developer forums offers a solution:
The typical way to implement these in .NET is to use the graphics provided in the ZIP file located at C:\Program Files\Microsoft Visual Studio X\Common7\VS200XImageLibrary.
You don't state which version of Visual Studio you have installed but you'll need to replace the "200X" with your version number.
The code in the accepted answer leaks one icon handle each time it's called, as it always asks for two icon handles and only gives one back.
Here is a version that doesn't leak a handle:
public static Icon Extract(string filePath, int index, bool largeIcon = true)
{
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
IntPtr hIcon;
if (largeIcon)
{
ExtractIconEx(filePath, index, out hIcon, IntPtr.Zero, 1);
}
else
{
ExtractIconEx(filePath, index, IntPtr.Zero, out hIcon, 1);
}
return hIcon != IntPtr.Zero ? Icon.FromHandle(hIcon) : null;
}
[DllImport("shell32", CharSet = CharSet.Unicode)]
private static extern int ExtractIconEx(string lpszFile, int nIconIndex, out IntPtr phiconLarge, IntPtr phiconSmall, int nIcons);
[DllImport("shell32", CharSet = CharSet.Unicode)]
private static extern int ExtractIconEx(string lpszFile, int nIconIndex, IntPtr phiconLarge, out IntPtr phiconSmall, int nIcons);
Some of them are available in %Program Files%\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary - for others, you'd need to speak to Microsoft's lawyers about licensing them for redistribution in your application
See this code. It will be help
public class ExtractIcon
{
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo(
string pszPath, uint dwFileAttributes,
out SHFILEINFO psfi, uint cbfileInfo,
SHGFI uFlags);
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public string szDisplayName;
public string szTypeName;
};
private enum SHGFI
{
SmallIcon = 0x00000001,
OpenIcon = 0x00000002,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
LinkOverlay = 0x00008000,
UseFileAttributes = 0x00000010
}
public static Icon GetIcon(string strPath, bool bSmall, bool bOpen)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon | SHGFI.SmallIcon;
else
flags = SHGFI.Icon | SHGFI.LargeIcon;
if (bOpen) flags = flags | SHGFI.OpenIcon;
SHGetFileInfo(strPath, 0, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}
I have a problem retrieving the information of printer ports. I already use XcvData function to add, configure and delete ports, but I can not get the following to run as I expect it to:
public PrinterNativeMethods.PORT_DATA_1 GetPortData1FromPort(string serverName, string portName)
{
IntPtr printerHandle;
PrinterNativeMethods.PRINTER_DEFAULTS defaults = new PrinterNativeMethods.PRINTER_DEFAULTS
{
DesiredAccess = PrinterNativeMethods.PrinterAccess.ServerAdmin
};
string connection = string.Format(#"{0},XcvPort {1}", serverName, portName);
PrinterNativeMethods.OpenPrinter(connection, out printerHandle, ref defaults);
PrinterNativeMethods.CONFIG_INFO_DATA_1 configData = new PrinterNativeMethods.CONFIG_INFO_DATA_1
{
dwVersion = 1,
};
uint size = (uint)Marshal.SizeOf(configData);
IntPtr pointer = Marshal.AllocHGlobal((int)size);
Marshal.StructureToPtr(configData, pointer, true);
PrinterNativeMethods.PORT_DATA_1 portData = new PrinterNativeMethods.PORT_DATA_1();
uint portDataSize = (uint)Marshal.SizeOf(portData);
IntPtr portDataHandle = Marshal.AllocHGlobal((int)portDataSize);
try
{
uint outputNeeded;
uint status;
var retVal = PrinterNativeMethods.XcvData(printerHandle, "GetConfigInfo", pointer, size, out portDataHandle, portDataSize, out outputNeeded, out status);
//portDataHandle now points to a different location!? Unmarshalling will fail:
portData = (PrinterNativeMethods.PORT_DATA_1)Marshal.PtrToStructure(portDataHandle, typeof(PrinterNativeMethods.PORT_DATA_1));
}
finally
{
PrinterNativeMethods.ClosePrinter(printerHandle);
Marshal.FreeHGlobal(pointer);
Marshal.FreeHGlobal(portDataHandle);
}
return portData;
}
from PrinterNativeMethods:
[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int XcvData(
IntPtr handle,
string dataName,
IntPtr inputData,
uint inputDataSize,
out IntPtr outputData,
uint outputDataSize,
out uint outputNeededSize,
out uint status);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct PORT_DATA_1
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string sztPortName;
public uint dwVersion;
public uint dwProtocol;
public uint cbSize;
public uint dwReserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 49)]
public string sztHostAddress;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33h)]
public string sztSNMPCommunity;
public uint dwDoubleSpool;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
public string sztQueue;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string sztIPAddress;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 540)]
public byte[] Reserved;
public uint dwPortNumber;
public uint dwSNMPEnabled;
public uint dwSNMPDevIndex;
}
Additional comment: I cannot use WMI or prnadmin.dll as an alternative.
The problem you are having is in the definition of your XcvData definition. The outputData parameter by MS's definition is simply wanting a pointer to write data to, an IntPtr is a pointer, however by setting the parameter to out IntPtr you are making it a pointer to a pointer, this is why the address of your parameter appears to be changing. Changing the signature to the below will fix your problem.
[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int XcvData(
IntPtr handle,
string dataName,
IntPtr inputData,
uint inputDataSize,
IntPtr outputData,
uint outputDataSize,
out uint outputNeededSize,
out uint status);
You can also avoid some unnecessary allocation/deallocation by changing things around a bit.
Change your method signature for XcvData to
[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int XcvData(
IntPtr handle,
string dataName,
IntPtr inputData,
uint inputDataSize,
ref PORT_DATA_1 outputData,
uint outputDataSize,
out uint outputNeededSize,
out uint status);
Assuming that you will be using XcvData for more than just this single call you can make multiple references to it with slightly different signatures by setting the EntryPoint property on the DllImport Attribute.
[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint="XcvData")]
internal static extern int XcvDataGetPortData1(
IntPtr handle,
string dataName,
IntPtr inputData,
uint inputDataSize,
ref PORT_DATA_1 outputData,
uint outputDataSize,
out uint outputNeededSize,
out uint status);
I have given this a quick test on my machine and can confirm that this will fix your problem.