Access Violation in DsBrowseForContainer on Windows Vista

I have used the example from SDK for browsing Active Directory (can be found here) and in Vista the method DsBrowseForContainer throws AccessViolationException in .NET.

While testing, I have created a new MFC (C++) project and added a code for calling to DsBrowseForContainerW (similar to above). That worked perfectly, which means the problem is not in Vista, rather in .NET or marshalling in .NET. Checking the structure returned from the function call, I have found that previously NULL "pszRoot" member is now filled in! Means DsBrowseForContainerW has allocated a memory for it and passed back to me. Looks like marshaller has problem with this.

What I did in C# later, is changed "pszRoot" member to IntPtr (leaving it null) and used Marshal.PtrToStringAuto method to get its value afterwards. This works just fine on Vista, XP and 2003. Now have to figure out if I should free the returned string in pszRoot and how to do that (which method to use I mean)

Related MSDN forums link: AccessViolation error when use DsBrowseContainer in Vista

Update Oct. 6, 2008: I have posted code and example for this issue here.

Technorati Tags: ,

Loading WPF TreeView dynamically

In WinForms, I used to catch BeforeExpand event of tree view and add additional items on the fly, from database (or any other source of course). So was looking to do the same thing in WPF TreeView and somehow missed the Expanded event of TreeViewItem. You can subscribe to this event in XAML like this:

<TreeView TreeViewItem.Expanded="TreeView_Expanded"></TreeView>

The rest of code is pretty simple, but if you want to see a full example, look at this post in MSDN forums: Loading TreeView nodes on demand.

Technorati Tags: ,