Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this function in a DLL:

C++
void VD_GetHomeDirectory( TCHAR *ADir )


But I cannot work with it in VB.NET here is my .NET code:

VB
<DllImport("VADAV.DLL", EntryPoint:="VD_GetHomeDirectory", SetLastError:=True, CharSet:=CharSet.Auto, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Sub VD_GetHomeDirectory(ByVal Dir As String)
    End Sub
Private Sub HomeButton_Click(sender As System.Object, e As System.EventArgs) Handles HomeButton.Click
       Dim Dir As String = Nothing
       VD_GetHomeDirectory(Dir)
       HomeTextBox.Text = Dir
End Sub

This error appear when run the code:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Any one has a idea about this. What is my mistake.
Posted

1 solution

You are passing Nothing (a null value) to a function which expects a pointer to a memory area in which it can safely insert the data.
You need to pass it some available, writable, memory - and since a String is immutable, you want to assign sufficient space into a StringBuilder and pass that.
 
Share this answer
 
Comments
mehdishahgholi 21-Oct-14 3:28am    
I tried but does not work. Could you please explain more or write the code
OriginalGriff 21-Oct-14 3:44am    
Show us exactly what you tried.
OriginalGriff 21-Oct-14 3:45am    
Oh, and explain exactly what did or didn't happen: "does not work" is not a helpful error description!
mehdishahgholi 21-Oct-14 7:45am    
:) sure, Here is the code:


<DllImport("VADAV.DLL", EntryPoint:="VD_GetHomeDirectory", SetLastError:=True, CharSet:=CharSet.Auto, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Sub VD_GetHomeDirectory(ByRef Dir As StringBuilder)
End Sub
Private Sub HomeButton_Click(sender As System.Object, e As System.EventArgs) Handles HomeButton.Click
Dim Dir As New StringBuilder(100)
VD_GetHomeDirectory(Dir)
End Sub

AccessViolationException error happen and this is the message:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I worked with two other function of this DLL with no problem as below:


<DllImport("VADAV.DLL", EntryPoint:="VD_GetImage", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Private Shared Function VD_GetImage(ByRef W As IntPtr) As IntPtr

End Function

<DllImport("VADAV.DLL", EntryPoint:="VDACQ_GetFrameDim", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Sub VDACQ_GetFrameDim(ByRef W As IntPtr, ByRef H As IntPtr)
End Sub

Collapse | Copy Code
Private Sub CheckDetectorButton_Click(sender As System.Object, e As System.EventArgs) Handles CheckDetectorButton.Click
Dim W As IntPtr
Dim H As IntPtr
VDACQ_GetFrameDim(W, H)
DimentionTextBox.Text = W.ToString + ";" + H.ToString

Me.Cursor = Cursors.WaitCursor
If VD_ConnectRestore() Then
CheckDetectorButton.BackColor = Color.Green
Else
CheckDetectorButton.BackColor = Color.Red
End If
Me.Cursor = Cursors.Default
End Sub

But with VD_GetHomeDirectory I get into hot water. Any idea please :(

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