急:c#的string和delphi的widestring不相容怎么办罗???(附了源码)

gaolang1 2003-08-25 09:00:06
//----------------------------------------------------------------
在delphi写了一个简单的动态库函数:
function TcpConnect(ServerAddr: WideString; var str:WideString ): Boolean; stdcall ;
begin
result:=false;
try
str := ServerAddr ;
result:=true;
except
;
end;
end;

//------------------------------------------------------------------------
然后在c#中调用:
[DllImport("Func5.dll", EntryPoint="TcpConnect", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool TcpConnect(string addr ,out string str);


Private void btnTcpConnect_Click(object sender, System.EventArgs e)
{
string str;
if(TcpConnect("192.0.0.76",out str)) {Response.Write(str);}
else
{Response.Write("failed");}
}
显示结果:192.0
(应该显示192.0.0.76 啊)

//-----------------------------------------------------------------------
delphi的widestring和c#的string不相容怎么办罗???

...全文
324 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhgj 2003-08-28
  • 打赏
  • 举报
回复
在delphi里把widestring定义成pchar
cnhgj 2003-08-28
  • 打赏
  • 举报
回复
在delphi里把widestring定义成pchar
gaolang1 2003-08-28
  • 打赏
  • 举报
回复
我试了,不行。
输出的字符是输入的一半,如输入“1234”,返回“12”。
我想是Unicode与Ansi的问题,但试了好多组合,都不行。
xixigongzhu 2003-08-26
  • 打赏
  • 举报
回复
这样定义试试:
[DllImport("Func5.dll", EntryPoint="TcpConnect", SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool TcpConnect(string addr ,out string str);
panyee 2003-08-26
  • 打赏
  • 举报
回复
function DoSomethingElseA(const Msg: String): Boolean;
function DoSomethingElseW(const Msg: WideString): Boolean;
...
[DllImport(Win32DLL, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
function DoSomethingElseA(const Msg: String): Boolean;
external;

[DllImport(Win32DLL, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
function DoSomethingElseW(const Msg: String): Boolean;
external;



http://www.blong.com/Conferences/BorConUK2002/Interop1/Win32AndDotNetInterop.htm
jiezhi 2003-08-25
  • 打赏
  • 举报
回复
換成string
cnhgj 2003-08-25
  • 打赏
  • 举报
回复
在delphi里把widestring定义成pchar
gaolang1 2003-08-25
  • 打赏
  • 举报
回复
string 不行 ,delphi 中的string 是ansi ,而c# 中的是unicode
gaolang1 2003-08-25
  • 打赏
  • 举报
回复
没有人懂吗?
我发现了一个规律:
每次传出来的是传进去的一半,如传“1234”,返回的就是“12”
我想应该是Unicode的问题,但不知如何匹配它们。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧