C#调用Delphi写的DLL
2011-01-19 15:14
387 查看
Delphi动态链接库中函数定义为:
在C#中可以这样调用:
调用代码:
这里需要注意的是要外传的PChar类型参数,在C#中对应使用StringBuilder,如果使用string没有任何信息传出,如果使用ref string形式,则会出现内存错误。
如果Delphi中函数的参数定义为var s :PChar类型,则相应的外传参数需要使用ref string类型,例如:
Delphi动态链接库中函数定义为:
在C#中可以这样调用:
调用代码:
经过测试:
Delphi中Integer的参数使用C#中的int即可;
Delphi中Real的参数使用C#中的double即可;
Delphi中Boolean的参数使用C#中的bool即可;
Delphi中TDateTime的参数使用C#中的DateTime即可;
function Get(s:PChar):Boolean;stdcall;
在C#中可以这样调用:
[DllImport(@"D:/Delphi/Test.dll", EntryPoint = "Get", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern bool Get(StringBuilder sb);
调用代码:
StringBuilder sb = new StringBuilder(); bool ret = Get(sb); if (ret) { MessageBox.Show(sb.ToString()); }
这里需要注意的是要外传的PChar类型参数,在C#中对应使用StringBuilder,如果使用string没有任何信息传出,如果使用ref string形式,则会出现内存错误。
如果Delphi中函数的参数定义为var s :PChar类型,则相应的外传参数需要使用ref string类型,例如:
Delphi动态链接库中函数定义为:
function Get(var s:PChar):Boolean;stdcall;
在C#中可以这样调用:
[DllImport(@"D:/Delphi/Test.dll", EntryPoint = "Get", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern bool Get(ref string s);
调用代码:
string s = ""; bool ret = Get(ref s); if (ret) { MessageBox.Show(s); }
经过测试:
Delphi中Integer的参数使用C#中的int即可;
Delphi中Real的参数使用C#中的double即可;
Delphi中Boolean的参数使用C#中的bool即可;
Delphi中TDateTime的参数使用C#中的DateTime即可;
相关文章推荐
- C# 调用Delphi dll
- 【摘自网上】Delphi 调用 C# dll
- [整理]c#简单调用DELPHI DLL封装窗体
- Delphi调用C#动态链接库DLL(摘自网络)
- 关于delphi Com+调用C# DLL的一点说明
- C#调用DELPHI的DLL时传递string数组
- 黄聪:关于delphi Com+调用C# DLL的一点说明[转]
- 【转自元宝兄】关于delphi Com+调用C# DLL的一点说明
- C#调用Delphi Dll返回字符串的示例(使用Move才能拷贝字符串)
- C#生成DLL以及Delphi调用
- 怎样在C#中调用Delphi6写的DLL
- C#调用delphi编写的dll时如何传入传出OLEVariant
- Delphi程序调用C#.Net编译的DLL并打开窗体(详解)
- C#中如何调用Delphi写的Dll,实现画图功能
- C#调用delphi生成的dll获取CPU序列号(一)
- windows服务的托管资源(C#) 静态调用 非托管资源(Delphi-dll) 开机造成无法进入桌面(一)
- asp.net C# 调用 delphi的dll
- 求教,c#如何调用delphi编写的dll中的函数
- Delphi调用C#dll的问题
- Delphi 调用 C# Dll 时一个编译问题 mscorlib_TLB.h Ambiguity between 'String' and 'System::String'