Callback functions in c++ with managed extensions

P

Paul W

Hi everybody,

This may seem like a Shell question, but I believe it belongs here.

I'm trying to make a wrapper class for the SHBrowseForFolder function. This
function provides for a callback to your code for certain situations. I've
done this in c# but can't seem to get it to work here.

This is the delegate:
public __delegate int BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM
lParam, LPARAM lpData);


This is the callback function (incomplete):
int FolderBrowserCallBack(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM
lpData)
{
return 0;
} //FolderBrowserCallBack

And here's the function that sets it up (somewhat incomplete):
DialogResult ShowDialog(Form* Owner)
{
DialogResult retVal = DialogResult::Cancel;
BROWSEINFO bi;
BrowseCallbackProc* pBrowseCallbackProc = new BrowseCallbackProc(this,
&FolderBrowser::FolderBrowserCallBack);
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = (HWND)Owner->Handle.ToPointer();
bi.ulFlags = (UINT)m_flags;
bi.pidlRoot = (LPITEMIDLIST)m_root;
bi.pszDisplayName = (LPWSTR)Marshal::StringToCoTaskMemUni(new String('\0',
MAX_PATH)).ToPointer();
bi.lpszTitle = (LPWSTR)Marshal::StringToCoTaskMemUni(m_title).ToPointer();
bi.lpfn = (BFFCALLBACK)pBrowseCallbackProc; // This assignment doesn't
seem to work
m_selectedPidl = SHBrowseForFolder(&bi); //
NullReferenceException occurs here
if (m_selectedPidl)
{
retVal = DialogResult::OK;
} //if (m_selectedPidl)
return retVal;
} //ShowDialog


When I run the code I get a NullReferenceException when calling
SHBrowseForFolder. When I step through, I find that even though
pBrowseCallbackProc appears to hold a valid address, bi.lpfn still shows
<undefined> after stepping through the assignment. This doesn't seem likely
to really be true because if you intentionally set this member to NULL, the
function works fine, albeit without the callback feature.

Thanks in advance for any help you can give me with this.

Paul
 
B

Ben Voigt

Thanks in advance for any help you can give me with this.

First piece of advice: migrate to C++/CLI. Managed Extensions for C++ is no
longer maintained. There are a lot of documented bugs that MS has no
intention of fixing, and probably many more known only to Microsoft.
 
T

Tamas Demjen

Paul said:
I'm trying to make a wrapper class for the SHBrowseForFolder function.

There is already one: FolderBrowserDialog, introduced in .NET 1.1.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx

That being said, is there any reason why you can't use the Microsoft
provided dialog?

Note, however, that there is (was?) a bug that caused
FolderBrowserDialog to crash frequently with NullReferenceException:

http://support.microsoft.com/kb/830920

It appears to be a .NET 1.1 problem, for which there's a hot fix.

Unfortunately I have the same problem in VS 2005 (frequent and random
crashes with FolderBrowserDialog::ShowDialog). I'm trying to install the
SP1 right now, and see if it makes a difference.

Tom
 
P

Paul W

Hi Tom,

The FolderBrowser in .NET doesn't expose all the functionality of the
dialog. I also want it to return the PIDL for the selected folder so I can
test another control I'm writing.
 
P

Paul W

Hi Ben.

I've been out of the loop here for awhile. Am I to assume that C++/CLI is
what comes with VS 2005? Or can VS 2003 be upgraded to use this? Upgrading
to VS 2005 isn't an option for me right now.
 
D

David Lowndes

I've been out of the loop here for awhile. Am I to assume that C++/CLI is
what comes with VS 2005?
Yes.

Or can VS 2003 be upgraded to use this?

No.

Dave
 
B

Bruno van Dooren [MVP VC++]

I've been out of the loop here for awhile. Am I to assume that C++/CLI is
what comes with VS 2005? Or can VS 2003 be upgraded to use this?
Upgrading
to VS 2005 isn't an option for me right now.


C++/CLI is the C++ binding to .NET.

MC++ was the first attempt to support .NET in C++. It was horrible and ugly.
C++/CLI was invented to replace MC++.

C++/CLI is here to stay, so learning it is a reasonable investment of your
time.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
T

Tamas Demjen

Paul said:
The FolderBrowser in .NET doesn't expose all the functionality of the
dialog. I also want it to return the PIDL for the selected folder so I can
test another control I'm writing.

I know that this is C# code, but it may still give you an idea, or you
can compile it to an assembly and use it from C++/CLI:

http://support.microsoft.com/kb/306285

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top