I have a class define in dll. I export this class by interface. I use this dll and class in other Delphi project very good but I want use this class in c# but send error for me.
Source DLL:
type
IMyDate = interface
['{D032F796-167D-4B0D-851D-2AEEA226646A}']
Procedure Analyze_Date(var y:Integer; m,d:Integer); stdcall;
end;
TMyDate = Class(TInterfacedObject, IMyDate)
Public
Procedure Analyze_Date(var y:integer; m,d:Integer); stdcall;
End;
procedure TMyDate.Analyze_Date(var y:Integer; m, d: Integer);
begin
y:= m+d;
end;
Function MyDate:IMyDate; stdcall;
begin
result:= TMyDate.Create;
end;
exports
MyDate;
Unit use in Delphi example:
Function MyDate:IMyDate; stdcall; external 'Project11';
implementation
procedure TForm3.Button1Click(Sender: TObject);
var
md:IMyDate;
var y,m,d:Integer;
begin
md:= MyDate;
y:=10;
m:=20;
d:=30;
md.Analyze_Date(y,m,d);
showMessage(intTostr(y));
end;
Use in c# but get error:
//[Guid("D032F796-167D-4B0D-851D-2AEEA226646A")]
public interface IMyDate
{
void Analyze_Date(ref int y, int m, int d);
}
[DllImport(
"Project11.dll"
, EntryPoint = "MyDate"
, SetLastError = true
, CallingConvention = CallingConvention.StdCall
, CharSet = CharSet.Unicode)]
public static extern IMyDate MyDate();
private void button9_Click(object sender, EventArgs e)
{
int y, m, d;
IMyDate da;
da = MyDate(); // get error this line
y = 10;
m = 20;
d = 30;
da.Analyze_Date(ref y, m, d);
}
Update
thank you for answer
i add new function to Interface and class but my function only return false
also my function and procedure in delphi is stdcall and interface in C# is normaly !!!
IMyDate = interface
['{D032F796-167D-4B0D-851D-2AEEA226646A}']
Function testing():Boolean; stdcall;
end;
TMyDate = Class(TInterfacedObject, IMyDate)
Public
Function testing():Boolean; stdcall;
End;
function TMyDate.testing(): Boolean;
begin
result:= true;
end;
c#:
[ComImport, Guid("D032F796-167D-4B0D-851D-2AEEA226646A"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMyDate
{
bool testing();
}
[DllImport(
"Project11.dll"
, EntryPoint = "CreateMyDate"//"MyDate"
, SetLastError = true
, CallingConvention = CallingConvention.StdCall
, CharSet = CharSet.Unicode)]
public static extern void CreateMyDate(out IMyDate Date);
Boolean b;
b = da.testing(); //this line only return false
if (b)
{
MessageBox.Show("ffff");
}