I'm converting a function from Visual Basic 6.0 as:
Declare Function RequestOperation Lib "archivedll" (ByVal dth As Long, ByVal searchRequestBuf As String, ByVal buflen As Long, ByVal FieldNum As Long, ByVal OP As Long, ByVal value As String) As Long
In C#, I'm declare the function as:
[DllImport("archivedll")]
public static extern int RequestOperation(int dth ,StringBuilder searchRequestBuf, int bufferLen, int fieldNum, int op, string value);
When call RequestOperation from C#, it throws an exception:
[System.AccessViolationException] = {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}
I have successful in calling many other functions like this, but only this function throws the 开发者_C百科exception.
This function is clearly not throwing an AccessViolationException
- instead, it is generating an access violation fault by "attempting to read or write protected memory". .NET is translating that fault into an AccessViolationException
.
You'll have to go figure out why it is "attempting to read or write protected memory". In particular, did you initialize the StringBuilder
you are passing to it? Please post the code you use to call this method.
I think the StringBuilder
in the function declaration has something to do with it. You should use just plain String
instead.
/// Return Type: int
///dth: int
///searchRequestBuf: BSTR->OLECHAR*
///buflen: int
///FieldNum: int
///OP: int
///value: BSTR->OLECHAR*
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="RequestOperation")]
public static extern int RequestOperation(int dth, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.BStr)] string searchRequestBuf, int buflen, int FieldNum, int OP, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.BStr)] string value) ;
精彩评论