473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Last Stumbling Block on Subclassing OpenFileDialog - AddressOf problem

I've struggled manfully and have finally managed to subclass the Common
Dialog class to add my own controls.
My last problem is how to set the lpfnHook attribute of the OPENFILENAME
structure.
This needs to be declared as an integer (IntPtr / delegate class refuses to
work).

So, how can I cast (AddressOf MyHookFunction) into an integer?

Thanks
Nov 20 '05 #1
7 1809
This needs to be declared as an integer (IntPtr / delegate class refuses to
work).


Using a delegate should work. Perhaps you should try to figure out why
it fails.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #2
* "Simon" <si***@ssm.com> scripsit:
I've struggled manfully and have finally managed to subclass the Common
Dialog class to add my own controls.
My last problem is how to set the lpfnHook attribute of the OPENFILENAME
structure.
This needs to be declared as an integer (IntPtr / delegate class refuses to
work).


Can you post some code?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #3
Here is my code:

Public Structure OpenFilename
Dim lStructSize As Integer
Dim hwndOwner As Integer
Dim hInstance As Integer
Dim lpstrFilter As String
Dim lpstrCustomFilter As String
Dim nMaxCustFilter As Integer
Dim nFilterIndex As Integer
Dim lpstrFile As String
Dim nMaxFile As Integer
Dim lpstrFileTitle As String
Dim nMaxFileTitle As Integer
Dim lpstrInitialDir As String
Dim lpstrTitle As String
Dim flags As Integer
Dim nFileOffset As Int16
Dim nFileExtension As Int16
Dim lpstrDefExt As String
Dim lCustData As Integer
Dim lpfnHook As SubclassCallback 'Normally declared as an
integer
Dim lpTemplateName As Integer
Dim pvReserved As Integer
Dim dwReserved As Integer
Dim FlagsEx As Integer
End Structure

Public Delegate Function SubclassCallback(ByVal hWnd As Integer,
ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As
Integer
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias
"GetOpenFileNameA" (ByRef pOpenfilename As OpenFilename) As Integer
Public Declare Function LoadLibrary Lib "kernel32" Alias
"LoadLibraryA" (ByVal lpLibFileName As String) As Integer

Public Function GetOpenFile(ByVal OwnerHWnd As Integer) As String
Dim CommonDialogStruct As OpenFilename
Dim RetVal As Integer
Dim myCallback As SubclassCallback = New
SubclassCallback(AddressOf SaveAsProc)

With CommonDialogStruct
.lStructSize = Len(CommonDialogStruct)
.hwndOwner = OwnerHWnd
.lpfnHook = myCallback
.lpTemplateName = IDD_DIALOG1
.lpstrFilter = Replace(subFilter, "|", Chr(0)) & Chr(0)
.nFilterIndex = 1
.lpstrFile = subFileName & Chr(0) & Space(255 -
Len(subFileName))
.nMaxFile = Len(.lpstrFile) - 1
.lpstrTitle = subTitle
.lpstrInitialDir = subInitialDirectory
.flags = OFN_FILEMUSTEXIST Or OFN_SHOWHELP Or OFN_EXPLORER
Or OFN_ENABLEHOOK Or OFN_ENABLETEMPLATE
.hInstance = LoadLibrary(myAppPath & "DlgRes_VB.dll") 'This
works. It's the file that defines my extra controls. It loads when I don't
try to change the lpfHook from an integer
.FlagsEx = 0
End With

Try
RetVal = GetOpenFileName(CommonDialogStruct) 'Does nothing.
Doesn't even throw an exception
Catch ex As Exception
MsgBox(ex.Message)
End Try

GetOpenFile = ""
If RetVal = 0 Then
GetOpenFile = ""
ElseIf RetVal = 1 Then
GetOpenFile = (Trim(CommonDialogStruct.lpstrFile))
Else
'error
MsgBox(CommDlgExtendedError)
End If
FreeLibrary(CommonDialogStruct.hInstance)
End Function

Public Function SaveAsProc(ByVal hwnd As Integer, ByVal uMsg As
Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Dim NMHStruct As NMHDR
Dim lContext As Integer
Dim lTemp As Integer
Dim lNotificationCode As Integer
Dim lControlID As Integer
Dim hCtrl As Integer
Dim lRetVal As Integer

SaveAsProc = 0
Select Case uMsg
'.....
end Select
end Function
Hope it makes sense!
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
* "Simon" <si***@ssm.com> scripsit:
I've struggled manfully and have finally managed to subclass the Common
Dialog class to add my own controls.
My last problem is how to set the lpfnHook attribute of the OPENFILENAME
structure.
This needs to be declared as an integer (IntPtr / delegate class refuses to work).


Can you post some code?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #4
* "Simon" <si***@ssm.com> scripsit:
Hope it makes sense!


I forgot to ask: What doesn't work when declaring the structure's
member as the delegate of the callback? Error message?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message news:<2n************@uni-berlin.de>...
* "Simon" <si***@ssm.com> scripsit:
Hope it makes sense!


I forgot to ask: What doesn't work when declaring the structure's
member as the delegate of the callback? Error message?


No. No error message at all. It processes the GetOpenFileName call and
moves straight onto the section following the try..end try block. It
doesn't display the dialog, it doesn't throw an exception. It just
returns a result of 0.

Simon
Nov 20 '05 #6
Well. I sorted it. It was the Len(CommonDialogStruct) line that caused the
problem.
Defining lpfnHook as a delegate gives it a length of 0! So you have to add 4
back on to get the real length.

Surely I can't be the only person to have come across this in VB.NET?

"Simon" <st*****@solidsolutions.co.uk> wrote in message
news:f1**************************@posting.google.c om...
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message

news:<2n************@uni-berlin.de>...
* "Simon" <si***@ssm.com> scripsit:
Hope it makes sense!


I forgot to ask: What doesn't work when declaring the structure's
member as the delegate of the callback? Error message?


No. No error message at all. It processes the GetOpenFileName call and
moves straight onto the section following the try..end try block. It
doesn't display the dialog, it doesn't throw an exception. It just
returns a result of 0.

Simon

Nov 20 '05 #7
It was the Len(CommonDialogStruct) line that caused the
problem.
Defining lpfnHook as a delegate gives it a length of 0! So you have to add 4
back on to get the real length.


For interop use it's usually best to call Marshal.SizeOf() to retrieve
the object size.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Cliff Roman | last post by:
I have been trying to find the answer on the postnuke forums and I have even tried Sitepoint but I am unable to find an answer. So now I am asking here in hopes that someone may have an idea ...
4
by: Rajendra KASHI | last post by:
Hi, I am currently supporting one of the .Net application build with C# language. The application uses lots of delegates, in using the delegates the ShowDialog of the OpenDialogControl hangs the...
1
by: | last post by:
After finally block executes, the control goes back to the caller..I was expecting the control to go to the next statement after the finally block. Thanks for your input.
0
by: Johannes H?drich | last post by:
Hello folks, i've got a tough problem with configuration the CMAB in my asp.net application. Following scenario is given: root directory (ASP.NET Application) |- web.config (access from CMAB...
3
RobH
by: RobH | last post by:
I have a couple of tables. tbl-SID - This Holds An ID for the different States/Country. (currently 20 ID's exist) ,, 0,Al,Al - This is aimed to be a single Item to depict all locations....
1
by: JOHNDOE | last post by:
All right, I've been trying to wrap my head around this by reviewing what is out on the web but still seem to be struggling with this.... Here is the calling code with the AddressOf Error: ...
2
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i know how to work with the owner draw mode in the ListView-Class, but how do i draw the last three dots in a column if the text exceeds the columns width? To make it clear, i am talking...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.