Home All Groups Group Topic Archive Search About

C DLL Callback to VB.NET 2005

Author
15 Jan 2006 9:41 PM
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)

Author
15 Jan 2006 9:58 PM
Tim Anderson
"Bob Simoneau" <bobsimoneau@newsgroups.nospam> wrote in message
news:%23UPK3xhGGHA.208@tk2msftngp13.phx.gbl...
>I am calling an c dll function which has a callback.  The callback gets

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

I believe you should include an additional attribute on your declaration:

UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)

Tim
Read my tech blog:
http://www.itwriting.com/blog
Author
15 Jan 2006 10:14 PM
Bob Simoneau
To which declaration:  The help files are somewhat useless with
UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)


Show quoteHide quote
"Tim Anderson" <timjand@nospam.hotmail.com> wrote in message
news:O1xeC7hGGHA.2704@TK2MSFTNGP15.phx.gbl...
> "Bob Simoneau" <bobsimoneau@newsgroups.nospam> wrote in message
> news:%23UPK3xhGGHA.208@tk2msftngp13.phx.gbl...
>>I am calling an c dll function which has a callback.  The callback gets
>
>> I end up with the same error.  Any ideas would be appreciated.  Thanks
>
> I believe you should include an additional attribute on your declaration:
>
> UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)
>
> Tim
> Read my tech blog:
> http://www.itwriting.com/blog
>
>
Author
16 Jan 2006 5:53 AM
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.
Author
16 Jan 2006 1:55 PM
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?
Show quoteHide quote
""Peter Huang" [MSFT]" <v-phu***@online.microsoft.com> wrote in message
news:EJmm2EmGGHA.3152@TK2MSFTNGXA02.phx.gbl...
> 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.
>
Author
16 Jan 2006 2:12 PM
Tim Anderson
"Bob Simoneau" <bobsimoneau@newsgroups.nospam> wrote in message
news:eELGPEiGGHA.984@tk2msftngp13.phx.gbl...
> To which declaration:  The help files are somewhat useless with
> UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)

On the declaration of the delegate.

Tim
Read my tech blog:
http://www.itwriting.com/blog
Author
16 Jan 2006 2:15 PM
Bob Simoneau
I can not find a way to do that
(UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)) n vb.net for
the delegate.

Show quoteHide quote
"Tim Anderson" <timjand@nospam.hotmail.com> wrote in message
news:%23eXFfbqGGHA.1628@TK2MSFTNGP12.phx.gbl...
>
> "Bob Simoneau" <bobsimoneau@newsgroups.nospam> wrote in message
> news:eELGPEiGGHA.984@tk2msftngp13.phx.gbl...
>> To which declaration:  The help files are somewhat useless with
>> UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)
>
> On the declaration of the delegate.
>
> Tim
> Read my tech blog:
> http://www.itwriting.com/blog
>
>
Author
16 Jan 2006 3:22 PM
Tim Anderson
"Bob Simoneau" <bobsimoneau@newsgroups.nospam> wrote in message
news:uIA%23DeqGGHA.1424@TK2MSFTNGP12.phx.gbl...

>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
Author
16 Jan 2006 3:53 PM
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.

Show quoteHide quote
"Tim Anderson" <timjand@nospam.hotmail.com> wrote in message
news:uqFP4CrGGHA.3864@tk2msftngp13.phx.gbl...
> "Bob Simoneau" <bobsimoneau@newsgroups.nospam> wrote in message
> news:uIA%23DeqGGHA.1424@TK2MSFTNGP12.phx.gbl...
>
>>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
>
>
Author
17 Jan 2006 6:10 AM
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.