高手请进,高分请教(100分),问题解决可以再给分!

onlyonetime 2006-05-29 09:58:43
.NET 调用DELPHI 编写的DLL时出现......

DLL中的一个函数为:
function WriteDemo(Pwd:Pchar):Longint;

DELPHI调用方法为缩写:

申明--
Function WriteDemo(Password :Pchar):Longint; far;external 'DEMO.dll' name 'WriteDemo';

调用--
procedure TForm1.Button4Click(Sender: TObject);
var
Password:Pchar;
IResult:Integer;
BEGIN
GetMem(Password,4);
Password[0] := Char($01);
Password[1] := Char($11);
Password[2] := Char($10);
Password[3] := Char($22);
IResult := WriteDemo( Password );
End



.Net调用方法缩写:

申明--
[DllImport("DEMO.dll",CharSet =CharSet.Ansi)]
public static extern int WriteDemo( byte[] bPwd );
或申明为
public static extern int WriteDemo( System.Text.StringBuilder strPwd );


调用1--
public static int PostBackDemo()
{
System.Text.StringBuilder strBl = new System.Text.StringBuilder("01111022");
return WriteDemo( strBl );
}

调用2--
public static int PostBackDemo()
{
byte[] by = new byte[];
by = System.Text.Encoding.Default.GetBytes( "01111022");
return WriteDemo( strBl );
}

以上两种方法调用都出错!出错问题主要是出现在pchar类型的转换问题,请高手指点如何解决.net调用DELPHI DELL 传PCHAR参数解决方法。
如查能提供完整的。NET调用DELPHI DLL方法最好,谢啦!


...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2006-05-29
  • 打赏
  • 举报
回复
如果只是手动生成,可以如下:
public static int PostBackDemo()
{
byte[] by = new byte[]{ 0x01, 0x11, 0x10, 0x22 };
return WriteDemo( strBl );
}
Knight94 2006-05-29
  • 打赏
  • 举报
回复
转为4个byte后,再传进去调用。
Knight94 2006-05-29
  • 打赏
  • 举报
回复
所以你不能直接通过string去转,自己做转换,如下:
先把"01111022"拆分为"01", "11", "10", "22"
然后把它们分别转为byte,例如
byte byt = (byte)(Convert.ToInt32( "01", 16 ));
Knight94 2006-05-29
  • 打赏
  • 举报
回复
当然错了
你需要的是char字符,那么按照你要求的格式,
01111022 要转换为4个char;
而你通过
Encoding.Default.GetBytes
进行转换,获得8个byte
因此传进去当然不对。
onlyonetime 2006-05-29
  • 打赏
  • 举报
回复

调用2写错了--
public static int PostBackDemo()
{
byte[] by1 = new byte[];
by1 = System.Text.Encoding.Default.GetBytes( "01111022");
return WriteDemo( by1 );
}

110,538

社区成员

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

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

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