JCARPENTER
Programmer
I have a class that allows me to browse folders. It uses unmanaged code in shell32.dll. Everything works, but I can't figure out how to call IMalloc::Free() from C#. I've included the class below. The commented section is the equivalent C++ code that's the problem. Can anybody help?
Thanks,
Jim
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace BrowseFolder
{
[StructLayout(LayoutKind.Sequential)]
public struct BROWSEINFOW
{
public int hwndOwner;
public IntPtr pidlRoot;
public string lpszDisplayName;
public string lpszTitle;
public uint ulFlags;
public IntPtr lpfn;
public long lParam;
public int iImage;
}
public class MyBrowse
{
[DllImport("Shell32"]
static public extern IntPtr
SHBrowseForFolder(ref BROWSEINFOW BrowseInfo);
[DllImport("Shell32"]
static public extern int
SHGetPathFromIDList(IntPtr pidl,
StringBuilder FolderName);
[DllImport("Shell32"]
static public extern int
SHGetMalloc(ref IntPtr ppMalloc);
static public string BrowseFolders(string title)
{
BROWSEINFOW bi = new BROWSEINFOW();
bi.lpszTitle = title;
IntPtr pItemList = SHBrowseForFolder(ref bi);
if ( pItemList == IntPtr.Zero )
return null;
StringBuilder folder = new StringBuilder(260);
int BOOL = SHGetPathFromIDList(pItemList,folder);
/* This is what I can't figure out
LPMALLOC ppMalloc;
SHGetMalloc(&ppMalloc);
ppMalloc->Free(pItemList);
*/
return folder.ToString();
}
}
}
Thanks,
Jim
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace BrowseFolder
{
[StructLayout(LayoutKind.Sequential)]
public struct BROWSEINFOW
{
public int hwndOwner;
public IntPtr pidlRoot;
public string lpszDisplayName;
public string lpszTitle;
public uint ulFlags;
public IntPtr lpfn;
public long lParam;
public int iImage;
}
public class MyBrowse
{
[DllImport("Shell32"]
static public extern IntPtr
SHBrowseForFolder(ref BROWSEINFOW BrowseInfo);
[DllImport("Shell32"]
static public extern int
SHGetPathFromIDList(IntPtr pidl,
StringBuilder FolderName);
[DllImport("Shell32"]
static public extern int
SHGetMalloc(ref IntPtr ppMalloc);
static public string BrowseFolders(string title)
{
BROWSEINFOW bi = new BROWSEINFOW();
bi.lpszTitle = title;
IntPtr pItemList = SHBrowseForFolder(ref bi);
if ( pItemList == IntPtr.Zero )
return null;
StringBuilder folder = new StringBuilder(260);
int BOOL = SHGetPathFromIDList(pItemList,folder);
/* This is what I can't figure out
LPMALLOC ppMalloc;
SHGetMalloc(&ppMalloc);
ppMalloc->Free(pItemList);
*/
return folder.ToString();
}
}
}