Calling Delphi code from C#

  • Thread starter Thread starter jewalsh2k
  • Start date Start date
J

jewalsh2k

Hi,

I have a delphi dll which I am trying to call from C# but something
isn't working, I get an SEHException thrown by the dll but I don't get
enough info to debug it. I can't change the dll either.

Could someone please take a look and see if or what I'm doing wrong?

Many thanks,

John Walsh

class ConversionNative
{
public delegate bool ConversionEventHandler(int currentFrame,
int totalFrames);
public event ConversionEventHandler ConversionStatus;

[DllImport("swf2avi.dll", EntryPoint="CreateAVI",
CallingConvention = CallingConvention.StdCall)]
public static extern void CreateAVI(ref TVideoParams
videoParams);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi,
Pack = 1)]
public class TVideoParams {

public int mailForm_handle;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=150)] public
string swf_name;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=150)] public
string avi_name;
public int avi_bitrate;
public byte avi_fps;
public byte avi_gop;
[MarshalAs(UnmanagedType.FunctionPtr)]public
ConversionEventHandler callback_fn;
}

public void convert()
{
ConversionStatus = updateStatus;

TVideoParams vParams = new TVideoParams();
vParams.avi_bitrate = 22000;
vParams.avi_fps = 3;
vParams.avi_gop = 12;
vParams.avi_name = @"C:\test\Slide4.avi";
vParams.swf_name = @"C:\test\Slide4.swf";
//vParams.mailForm_handle = 0;// new IntPtr(1);
vParams.callback_fn = ConversionStatus;

CreateAVI(ref vParams);

}

private bool updateStatus(int currentFrame, int totalFrames){
Console.WriteLine ("Processed " + currentFrame + " out of "
+ totalFrames + " frames...");

return true;
}
}
 
(e-mail address removed) wrote in
Hi,

I have a delphi dll which I am trying to call from C# but
something isn't working, I get an SEHException thrown by the dll
but I don't get enough info to debug it. I can't change the dll
either.

Could someone please take a look and see if or what I'm doing
wrong?

John,

32-bit Delphi code has no idea how to work with a .Net class
(TVideoParams).

One possible solution that comes to mind is to create a proxy DLL w/
Delphi. The proxy would expose a routine that took as parameters the
individual fields that make up the TVideoParams class. The proxy
would then package these values up in the Delphi version of
TVideoParams and pass that to CreateAVI in swf2avi.dll.
 
Hi,

I have a delphi dll which I am trying to call from C# but something
isn't working, I get an SEHException thrown by the dll but I don't get
enough info to debug it. I can't change the dll either.

Could someone please take a look and see if or what I'm doing wrong?

Hi,
you haven't posted the Pascal implementation of the structure
TVideoParams, at least regarding the C# implementation seems
to be correct. But it's hard to tell, because of the missing
Pascal definitions.

If you have a Delphi compiler I would do the following:

a) Create a new dll with the same function exported and single
step through the code, if the function is called correctly
and where it does crash.

b) Use Delphi.NET and compile a wrapper DLL for the native one.
If the function is called without crashing you can use an
IL debugger to get the structure definition and you should
be able to compare this one with your own.


Hope that helps.
Andre
 

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

Back
Top