PC Review


Reply
Thread Tools Rate Thread

Delphi and C#, Implementing Callback functions

 
 
Jon E. Scott
Guest
Posts: n/a
 
      9th Jan 2007
I've got a project where I need to create a C# project and a Delphi DLL, in
which the DLL has a callback function to send statuses back to the C#
application. It seems pretty straightforward in a MSDN example in which it
explains the use of delegates
(http://msdn2.microsoft.com/en-us/library/843s5s5x.aspx), but for some
reason I get an AccessViolationException when the callback function is
called. Anyone have any ideas? Here's the Delphi DLL:

library Project1;

uses
SysUtils,
Classes;

{$R *.res}

type
TTestProc = procedure(TestInt: integer) of object;

function TestCallBack(CallBack: TTestProc): boolean; stdcall;
begin
CallBack(12345);
Result := True;
end;

exports
TestCallBack;

end.

Then the C# project:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private delegate void CallBack(int testParam);

[DllImport("N:\\Temp\\Project1.dll")]
private static extern bool TestCallBack(CallBack x);

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CallBack myCallBack = new CallBack(Form1.Report);
TestCallBack(myCallBack); //<-- AccessViolationException here
}

private static void Report(int testParam)
{
MessageBox.Show(testParam.ToString());
}
}
}


--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com



 
Reply With Quote
 
 
 
 
Barry Kelly
Guest
Posts: n/a
 
      9th Jan 2007
Jon E. Scott wrote:

> I've got a project where I need to create a C# project and a Delphi DLL, in
> which the DLL has a callback function to send statuses back to the C#
> application.


> type
> TTestProc = procedure(TestInt: integer) of object;


This is a Delphi method pointer, and therefore has an implicit 'Self'
argument. Also, it is not declared to use the 'stdcall' calling
convention.

You should try something more like:

type
TTestProc = procedure(TestInt: Integer) stdcall;

> function TestCallBack(CallBack: TTestProc): boolean; stdcall;
> begin
> CallBack(12345);
> Result := True;
> end;


> private delegate void CallBack(int testParam);


This is a .NET delegate, and the P/Invoke marshalling layer of .NET
handles it as a simple function pointer, with no implied 'Self'
argument.

> [DllImport("N:\\Temp\\Project1.dll")]
> private static extern bool TestCallBack(CallBack x);


Finally, for questions which need Delphi-specific knowledge, you're
probably better off asking on the Delphi newsgroups, currently at
nntp://newsgroups.borland.com/.

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
Jon E. Scott
Guest
Posts: n/a
 
      9th Jan 2007
"Barry Kelly wrote...
>> type
>> TTestProc = procedure(TestInt: integer) of object;

>
> This ought to be:
>
> type
> TTestProc = procedure(TestInt: Integer) stdcall;


Yup, that's where the problem was. Changed that line and the callback
functions work!

Thanks for your help.

--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing a Callback function (_cdecl) in VB.NET =?Utf-8?B?a3VydGNvYmFpbjE5Nzg=?= Microsoft VB .NET 8 4th Oct 2005 10:34 AM
Callback functions Sakharam Phapale Microsoft VB .NET 1 31st Aug 2004 12:33 PM
Callback Functions db_from_mn Microsoft C# .NET 6 17th Apr 2004 12:32 PM
Callback functions Kevin Hutchison Microsoft Dot NET Compact Framework 2 8th Mar 2004 08:07 PM
Replace functions in VB or Delphi Vincent Choy Microsoft Access 1 28th Oct 2003 03:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:19 PM.