遇到一个dll,其中他的一个函数如下
 Function:
Acquire ID data from ID information table in the internal DLL file.
 Form:
int hst_idtblgetid(long recno, UCHAR *iddata)
 Parameter:
recno Designated record number of acquired ID data
1~128
iddata Stored pointer for acquired ID data
 Return value:
0 Normal End.
-1 Abnormal End. 
关于这个UCHAR *iddata实在是郁闷,因找厂商无果,只能自己尝试
经过察看其vb代码,估计该函数调用后会返回iddata值。
vb代码如下
引用:
Public Declare Function hst_idtblgetid Lib "c:\HostCM32.dll" (ByVal recno As Long, ByVal iddata As String) As Integer    
代码:
idret = "000000"
ret = hst_idtblgetid(1, idret)
If ret = 0 Then
  MsgBox " ok  two " & idret
Else
  MsgBox iddata
End If
老是:
未处理的“System.NullReferenceException”类型的异常出现在 firec.exe 中。其他信息: 未将对象引用设置到对象的实例。写下过程,请高手帮忙解答。
1、使用byte[]
引用部分:
[DllImport("HostCM32.dll")]
private static extern int hst_idtblgetid(long recno,out byte[] iddata);
代码:
byte[] abc=System.Text.Encoding.Default.GetBytes("000000");
ret = hst_idtblgetid(0,out abc);
经尝试ref同样出错
2、使用string []
private static extern int hst_idtblgetid(long recno, out  string [] iddata);
代码:
string[] abc=new string[6] ;
for(int k=0;k<6;k++)
abc[k]="0";
ret = hst_idtblgetid(0,out abc);
同样尝试了ref 
 
3、使用StringBuilder
同样
4、
private static extern int hst_idtblgetid(long recno,   System.IntPtr iddata); byte[] abc=System.Text.Encoding.Default.GetBytes("000000");
System.IntPtr bp = Marshal.UnsafeAddrOfPinnedArrayElement( abc , 0 );
ret = hst_idtblgetid(0, bp);
5、char[]这个牵扯参数封装问题,估计也不行就没往下试代码
char[] abc=new char [7] ;
for(int k=0;k<6;k++)
abc[k]='0';
abc[6]='\0';
ret = hst_idtblgetid(0,ref abc );

解决方案 »

  1.   

    vb代码如下
    引用:
    Public Declare Function hst_idtblgetid Lib "c:\HostCM32.dll" (ByVal recno As Long, ByVal iddata As String) As Integer    
    代码:
    idret = "000000"
    ret = hst_idtblgetid(1, idret)
    If ret = 0 Then
      MsgBox " ok  two " & idret
    Else
      MsgBox iddata
    End If这个是不是VB6下面的代码??是否可以正常使用??
      

  2.   

    private static extern int hst_idtblgetid(int recno,   string iddata); 这样就行了!
      

  3.   

    试过不行的
    因为这个iddata还要接受dll传值
      

  4.   

    你是说iddata还要返回值吗???private static extern int hst_idtblgetid(int recno, ref string iddata);
      

  5.   

    我是十三月,接上午和你讲的,估计要用unsafe将string的指针传进去了.
      

  6.   

    to:wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) 
    因为string 传过去的值dll函数不能修改所以这样也不行的
    这样可以:
    private static extern int hst_idtblgetid(short recno,ref IntPtr idata); IntPtr idata = IntPtr.Zero;
    //MessageBox.Show(idata.ToString());
    if(hst_idtblgetid(1,ref idata)==0)
    MessageBox.Show(idata.ToString());
    else
    MessageBox.Show("fail!");
    ======================
    但是我这里这个idata是要有初始值的"000000";
    当完成这个函数调用的时候idata值就变成了"&&&&&&"
    我现在看看intptr怎么指向一个string
      

  7.   

    trystringbuilder
    注意字符编码