Click here to Skip to main content
15,115,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone

I have a VB2010 .Net program which maintains Supplier details. Each supplier has a certificate (PDF). The Supplier Maintenance program has a button which is intended to open a File dialog box, allow the user to select a PDF file and the program then stores the path and file name.

Private Sub but_Certificate_Path_Click(sender As System.Object, e As System.EventArgs) Handles but_Certificate_Path.Click
    Dim ofd As New OpenFileDialog

    ofd.Filter = "PDF Files(*.pdf)|*.pdf|All files (*.*)|*.*"

    ofd.FilterIndex = 1
    ofd.InitialDirectory = "\\xxx\BEE"

    If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then  '<--CRASH OCCURS HERE
        txt_Bee_Certificate_Path.Text = ofd.FileName
    End If

End Sub

When the 'Path' button is clicked the program crashes with :
Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.

I am not using the OpenFileDialog1 control.

The Call Stack info that I am able to find is :
SupplierMaintenance.exe!SupplierMaintenance.frm_Supplier_Maint.but_Certificate_Path_Click(Object sender, System.EventArgs e) Line 415 Basic
[Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]
[External Code]

Do I have a coding problem or a problem with user32.dll?

Does anyone know how I can resolve this - I am absolutely clueless.

Many thanks
Richard MacCutchan 4-Dec-13 6:48am
Are you sure that initial directory path is valid?
Darrell de Wet 4-Dec-13 7:24am
Yes. I just changed it to xxx for this post. xxx = File server name.
Richard MacCutchan 4-Dec-13 8:06am
All I can suggest is that you step through the code with your debugger and try to find out what is happening.
Fredrik Bornander 4-Dec-13 8:33am
Create a new WinForms application with a single button that has the payload of the above OnClick handler to verify that it is not an environment issue.
If that application does not crash then it's something that your application has done to it's memory before calling that method.

Are you PInvoking anything?
Are you using unsafe code?
Darrell de Wet 5-Dec-13 2:36am
Hi Fredrik - thanks for your response.
I hope that I am not using any unsafe code - what would unsafe code be?
I am relatively new to .Net so I can do with all the assistance I can get
Fredrik Bornander 5-Dec-13 9:33am
If you're using the 'unsafe' keyword, then you're using unsafe code.
cigwork 4-Dec-13 15:04pm
Does this happen on the very first attempt or only after some random number of attempts? If the latter have you tried calling ofd.Dispose() before exiting the click event handler?

Have tou tried specifying a local path that you know the process will have access to? If that works, check that the process has access to the server path. Perhaps the dlg is trying to do something clever if there's no access and failing?

' Can we see the server directory?
Dim dI As IO.DirectoryInfo = New IO.DirectoryInfo(ofd.InitialDirectory)
Dim fA As io.FileAttributes = dI.Attributes
Catch ex As Exception
' Might get caught. Might not if a security exception is thrown.
End Try
Darrell de Wet 5-Dec-13 2:34am
Morning cigwork.

Thanks for your assistance.
Part 2 of your reply : I tried as you suggested and there appears to be no problem seeing the server directory.

I have also included the ofd.dispose() statement and that seems to have changed tings a bit.

The form contains a ListView where all the suppliers are listed. If the user clicks on a supplier, the suppliers details are then placed in a group of controls (textboxes, checkedboxes etc) so that the user can change as required.

The 'Certificate Path' button is included with these.

If the user clicks on the listview and then on the button a blank dialog box is opened with only file name, files of Type combo boxes and Open, Cancel buttons visible - the rest completely blank.

If the first thing the user does is click on the Certificate button then the dialog boxes opens perfectly and thereafter they can click on the listview and Certificate button as often a required and it works perfectly.

Fredrik Bornader asked if I was using any unsafe code - I certainly hope not. Do you have any idea what might constitute unsafe code?

Any thoughts on this? Your assistance is truly appreciated.
cigwork 5-Dec-13 14:27pm
OK I'm stumped too. A quick search on teh interwebs shows you are not alone. This problem has been turning up for a couple of years now.

The link below although for C++/MFC is interesting because its 1st suggestion is to uninstall the acrobat PDF reader. What happens if you try to select files that aren't PDFs? There's also a link to a hotfix that may be of interest.

Another suggestion that seems to be made on a regular basis in the various articles is to get rid of any Windows Explorer shell extensions you may have loaded. It seems that bugs in these can cause this sort of behaviour.

The following links have a work-around which the authors freely admit are a bit smelly.

Unsafe code.
You can mark code as "unsafe" to allow the use of Windows APIs that don't/won't/can't play nicely in .Net's memory managed and garbage collected (or safe) world. AFAIK you can't use the "unsafe" keyword in VB, but if your project references a library DLL written in (say) C# that has unsafe methods then that could be a source of your woes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900