delphi编写的xxx.dll导出函数定义如下:
function DataToStr(
const aType:Word;
Const aCode:Word;
const Data; //传入数据
Datalen:integer;//传入数据长度
out Re;//传出数据
const ReLen:word//传出数据长度
):integer ;
--------------
C#定义:
[DllImport(@"xxx.dll" , CharSet=CharSet.Ansi)]
public static extern int DataToStr(UInt16 aType, UInt16 aCode, byte[] Data, int len, ref byte[] re, UInt16 reLen);-----
C#中调用时候出现异常
System.AccessViolationException
-----
问题:
1.请问上述C#定义有无错误
2.出现异常是为什么
3.怎么解决
----
附:msdn对AccessViolationException 的解释
http://msdn2.microsoft.com/zh-cn/library/system.accessviolationexception.aspx
function DataToStr(
const aType:Word;
Const aCode:Word;
const Data; //传入数据
Datalen:integer;//传入数据长度
out Re;//传出数据
const ReLen:word//传出数据长度
):integer ;
--------------
C#定义:
[DllImport(@"xxx.dll" , CharSet=CharSet.Ansi)]
public static extern int DataToStr(UInt16 aType, UInt16 aCode, byte[] Data, int len, ref byte[] re, UInt16 reLen);-----
C#中调用时候出现异常
System.AccessViolationException
-----
问题:
1.请问上述C#定义有无错误
2.出现异常是为什么
3.怎么解决
----
附:msdn对AccessViolationException 的解释
http://msdn2.microsoft.com/zh-cn/library/system.accessviolationexception.aspx
---
function DataToStr(const aType:Word;Const aCode:Word;const data:string): string;
var
len ,relen: integer;
begin
Result:='';
if length(Data)>0 then
begin
if Assigned(FDataToStrFun) then
begin
len:=1024;
SetLength(Result,len);
FillChar(Result[1],len,0);//FDataToStrFun是从dll中导出的地址
relen:=FDataToStrFun(aType,aCode,Data[1],length(Data),Result[1],len);
if relen<=0 then relen:=0 ;
if relen<len then
SetLength(Result,len);
end
else
Result:=BinaryToHex(data[1],length(Data));
end;
end;
-------
我用icesword查了一下,程序运行的时候,xxx.dll没有在内存中
--
多谢
这个是delphi写的,不是c#写的
[DllImport(@"xxx.dll" , CharSet=CharSet.Ansi)]
public static extern int DataToStr(UInt16 aType, UInt16 aCode, [In]StringBuilder Data, int len, [Out]StringBuilder re, UInt16 reLen);
public static extern int DataToStr(UInt16 aType, UInt16 aCode, [In]StringBuilder Data, int len, [Out]StringBuilder re, UInt16 reLen);
------谢谢
这个不行,一样的结果
------
我怀疑是xxx.dll没有load
--用icesword查了一下,程序运行的时候,xxx.dll没有在内存中
不知道有什么方法可以检测
--
我看过一些资料这里好像只能使用byte[]
byte[] msg = new byte[lh.MessageLength];
if (lh.DataLength > 0)
{
fs.Read(data, 0, data.Length);
byte[] re = new byte[data.Length * 2];
//StringBuilder strData = new StringBuilder(System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
//StringBuilder strRe = new StringBuilder(strData.Length * 2);
//DataToStr(lh.Type, lh.Code, strData, strData.Length, strRe, (UInt16)strRe.Length);
DataToStr(lh.Type, lh.Code, data, data.Length,ref re, (UInt16)re.Length);
}
[DllImport(@"xxx.dll" , CharSet=CharSet.Ansi)]
public static extern int DataToStr(UInt16 aType, UInt16 aCode,
[MarshalAs(UnmanagedType.LPTStr)] byte[] Data, int len, [Out,MarshalAs(UnmanagedType.LPTStr)] byte[] re, UInt16 reLen);
内存也有xxx.dll
谢谢你的提醒