C DLL Callback to VB.NET 2005

B

Bob Simoneau

I am calling an c dll function which has a callback. The callback gets
called once and my grid gets populated. The grid should have 4 rows, but as
soon as the first row gets populated the programs halts with an error:
Attempted to read or write protected memory. This is often an indication
that other memory is corrupt. Calling the function from Delphi works great
and returns 4 rows. It uses cdelc for calling convention, I tried using the
line below:

<DllImport("remotapi.dll", CallingConvention:=CallingConvention.Cdecl)> _
Sub rlist(ByRef INRLIST_PARMS As TRLIST_PARMS, ByRef INRLIST_RESP As
TRLIST_RESP, ByVal incb As ListDelegate)
End Sub

I end up with the same error. Any ideas would be appreciated. Thanks


Form1

Private Sub RLIST_POPULATE(ByRef RLIST_DETAIL As TRLIST_DETAIL)
Try
Dim source As String = StripNull(RLIST_DETAIL.source_name)
Dim RegNum As String = StripNull(RLIST_DETAIL.request_nbr)
Dim Target As String = StripNull(RLIST_DETAIL.target_name)
Dim ref_id_1 As String = StripNull(RLIST_DETAIL.ref_id_1)
Dim ref_id_2 As String = StripNull(RLIST_DETAIL.ref_id_2) & " "
Dim lvDate As String = StripNull(RLIST_DETAIL.creation_date)
Dim lvTime As String = StripNull(RLIST_DETAIL.creation_time)
Dim row1 As String() = {source, RegNum, Target, ref_id_1,
ref_id_2, lvDate, lvTime}
grd.Rows.Add(row1)
grd.Refresh()
Catch
MessageBox.Show("Error in Populate")
End Try
End Sub
.....
Private Sub cmdList_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdList.Click
RLIST_PARMS.size = Marshal.SizeOf(RLIST_PARMS)
RLIST_RESP.size = Marshal.SizeOf(RLIST_RESP)
RLIST_PARMS.ref_id_1 = edtRef1.Text & New String(Chr(0), 11 -
Len(edtRef1.Text))
RLIST_PARMS.hostname = edtHost.Text & New String(Chr(0), 101 -
Len(edtHost.Text))
RLIST_PARMS.system_user = "CHAINLNK" & New String(Chr(0), 101 - 8)
RLIST_PARMS.system_password = "CHAINLNK" & New String(Chr(0), 101 -
8)
RLIST_PARMS.system_select = edtStore.Text & New String(Chr(0), 11 -
Len(edtStore.Text))
RLIST_PARMS.target_name = edtStore.Text & New String(Chr(0), 11 -
Len(edtStore.Text))
RLIST_PARMS.proc_status_code = "REA" & New String(Chr(0), 4 - 3)
RLIST_PARMS.source_name = edtSource.Text & New String(Chr(0), 11 -
Len(edtSource.Text))
Dim cb As New ListDelegate(AddressOf RLIST_POPULATE)
---> rlist(RLIST_PARMS, RLIST_RESP, cb) <--- Errors here


Module 1
Public Declare sub rlist Lib "remotapi.dll" (ByRef RLIST_PARMS As
TRLIST_PARMS, ByRef RLIST_RESP As TRLIST_RESP, ByVal incb As ListDelegate)
 
B

Bob Simoneau

To which declaration: The help files are somewhat useless with
UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)
 
P

Peter Huang [MSFT]

Hi Bob,

Here is a link may help you.
http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/t
opic15209.aspx

Because without source code, it is hard to troubleshoot such P/Invoke issue.
If that still did not work for you, I suggest you try to contact MSPSS
directly.
http://support.microsoft.com

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bob Simoneau

I am almost 100% sure the problem is with the calling convention. How to
define a delegate with Cdecl calling convention in VB.NET?
 
B

Bob Simoneau

I can not find a way to do that
(UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)) n vb.net for
the delegate.
 
T

Tim Anderson

I can not find a way to do that
(UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)) n vb.net for
the delegate.

Something like this:

<UnmanagedFunctionPointer(CallingConvention.Cdecl)> _

Public Delegate Sub MyDelegate(ByRef RLISTSYS_DETAIL As TRLISTSYS_DETAIL)

Tim

Background worker exceptions in .NET 2.0

http://www.itwriting.com/blog/?postid=276
 
B

Bob Simoneau

Thank you so much for your help. That worked perfectly. This was a real
challenge for me, but thanks to people like you and Peter Huang from
Microsoft Online Partner Support, I survived.
 
P

Peter Huang [MSFT]

Hi Bob,

I am glad to hear that.
And thanks for Tim's knowledge sharing in the community.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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