Problem with SHELLEXECUTEEX ( Null argument exception on marshal.copy)

B

Basha

NullArgument Exception is happening on marshal.copy statement
execution. Plz help, following is the code


Private Structure SHELLEXECUTEEX
Public cbSize As UInt32
Public fMask As UInt32
Public hwnd As IntPtr
Public lpVerb As IntPtr
Public lpFile As IntPtr
Public lpParameters As IntPtr
Public lpDirectory As IntPtr

Public nShow As Integer
Public hInstApp As IntPtr
' Optional members
Public lpIDList As IntPtr
Public lpClass As IntPtr
Public hkeyClass As IntPtr
Public dwHotKey As UInt32
Public hIcon As IntPtr
Public hProcess As IntPtr
End Structure 'SHELLEXECUTEEX

Private Class APIClass
<DllImport("coredll")> _
Public Shared Function ShellExecuteEx(ByRef ex As
ShellExecuteEx) As Integer

End Function
'<DllImport("coredll")> _
Public Shared Function LocalAlloc(ByVal flags As Integer,
ByVal size As Integer) As IntPtr

End Function

<DllImport("coredll")> _
Public Shared Sub LocalFree(ByVal ptr As IntPtr)

End Sub
End Class




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim docname As String = clsAppSettings.AppDirectory & "\" &
clsAppSettings.LU_CabFileName
Dim fInfo As New FileInfo(docname)

Dim nSize As Integer = docname.Length * 2 + 2
Dim pData As IntPtr = APIClass.LocalAlloc(&H40, nSize)
Marshal.Copy(Encoding.Unicode.GetBytes(fInfo.Name), 0, pData,
nSize - 2)

Dim see As New SHELLEXECUTEEX
see.cbSize = System.Convert.ToUInt32(60)
see.dwHotKey = System.Convert.ToUInt32(0)
see.fMask = System.Convert.ToUInt32(0)
see.hIcon = IntPtr.Zero
see.hInstApp = IntPtr.Zero
see.hProcess = IntPtr.Zero
see.lpClass = IntPtr.Zero
see.lpDirectory = IntPtr.Zero
see.lpIDList = IntPtr.Zero
see.lpParameters = IntPtr.Zero
see.lpVerb = IntPtr.Zero
see.nShow = 0
see.lpFile = pData

APIClass.ShellExecuteEx(see)

APIClass.LocalFree(pData)
End Sub
 
A

Alex Feinman [MVP]

You are getting NullReferenceException because you have commented out
<DllImpot("coredll")>

'<DllImport("coredll")> _
Public Shared Function LocalAlloc(ByVal flags As Integer, ByVal size As
Integer) As IntPtr
End Function

This makes your function return null pointer as it is considered local. This
of course would not have happened in C#

There is another problem. nSize is the size of the full path to the cab
file, but in the Marshal.Copy you use GetBytes(fInfo.Name). fInfo.Name is a
part of the full path and trying to copy nSize-2 bytes from it will throw
OutOfRange exception
 

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