473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Delphi and C#, Implementing Callback functions

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

Jan 9 '07 #1
2 4468
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/
Jan 9 '07 #2
"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
Jan 9 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: achrist | last post by:
I'm aving some trouble getting callbacks from a Delphi DLL back to python working through ctypes. The call from python to the DLL works fine. In the Delphi(5) code: type TCallbackFunc =...
5
by: Pratik | last post by:
what are callback functions? Where we require callback functions? In what scenario we require callback functions?
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
2
by: Huseyin Altun | last post by:
I have DLL written in Delphi 6. Its prototype is below: function EnumUserGroups(Proc: TEnumUserGroupProc; APtr: Pointer): LongInt; stdCall; I have declared it in c# like below: (VS 2003) ...
2
by: Larry Bates | last post by:
I have a rather large Python class that I've converted to a COM object. I can dispatch (using either Python or VB) and call the methods perfectly. Now a new client wants to call it from Delphi. ...
0
by: Larry Bates | last post by:
I have a Python COM object working. I want to pass to it a Delphi callback function (showing progress) that I can call periodically. I've Googled until I'm cross-eyed and I'm getting nowhere. ...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
5
by: romcab | last post by:
Hi guys, I know function pointers but I'm having a hard time about callback function when it is associated with abstract class. I have an example code below which I got from a book. Kindly...
1
by: durumdara | last post by:
Hi! I have a component, and a tool in Delphi. I wanna call it from Python. I can implement all things I need into a Delphi generated DLL. It is ok, I can use this DLL from Python, but the DLL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.