在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Delphi程序调用C#.Net编译的DLL并打开窗体(详解) 2、使用强名称签名的Key编译成类库Tester.DLL 3、生成类型库 (此tlb文件提供给Delphi导入, 使用OLE方式调用此步可省略了)
reg.bat 文件内容:
sn -k MyKey.snk csc /t:library /keyfile:MyKey.snk Tester.cs tlbexp Tester.dll /out:Tester.tlb regasm Tester.DLL /regfile:Tester.reg
C# Code:
/// <summary> /// 测试类. 请给所有方法提供一个参数,即使是无用的签名 /// </summary> public class Tester { public string GetMyKey(int param) { return "Key:www.csframework.com"; } public string GetMyName(string input) { return "Jonny Sun" ":" input; } public int GetMyAge(string yourName) { return 33; } /// <summary> /// 显示消息并打开窗体 /// </summary> /// <param name="msg">消息</param> /// <returns></returns> public int ShowMessage(string msg) { MessageBox.Show(msg); frmTest.Execute(msg);//打开窗体 return 100; } /// <summary> /// 无签名(参数)的方法给Delphi调用,调用失败,Delphi程序会抛出异常。 /// </summary> /// <returns></returns> public int CallMethodWithNoSign() { return -1; } } // 来源:www.CSFramework.com, C/S结构框架学习网
Delphi Code:
procedure TForm1.Button1Click(Sender: TObject); var OBJ: Variant; //OLEVariant Type. ResultValue: string; //Value returned from C# i:Integer;//Value returned from C# begin try try OBJ := CreateOleObject('Dll4DelphiCall.Tester'); //Test a function with signature. ResultValue := OBJ.GetMyAge('JONNY!'); ResultValue := OBJ.GetMyKey(0); i:=OBJ.ShowMessage('Call CSharp Window....(Sender:Delphi Form)'); end; // 来源:www.CSFramework.com, C/S结构框架学习网 源码在360云盘 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论