现在VS2005 C#的环境下调用一个VC做成的OCX,
1、其中有个函数:vc
long CKeyOcx::ReadLockList(long sys_code, long password, long* lock_list)
{
long result;
static BYTE parms[] =
VTS_I4 VTS_I4 VTS_PI4;
InvokeHelper(0xb, DISPATCH_METHOD, VT_I4, (void*)&result, parms,
sys_code, password, lock_list);
return result;
}
2、做成OCX,在C#中调用时的格式:
int  ReadLockList(int  sys_code,int  password, ref int lock_list);  
3、而在VC调用上面那个VC函数的利子为:
void CKeyOcxTestDlg::OnReadLockList() 
{
// TODO: Add your control notification handler code here

UpdateData(TRUE);
CString m_str;
long list[3080];
long length = m_keyocx.ReadLockList(m_edit1,m_edit2,list);
if(length != 10000)
{
m_edit27 = "";
for(long i=0; i<length; i++)
{
m_str.Format("%d ",list[i]);
m_edit27 = m_edit27+m_str;
}
UpdateData(FALSE);
}
else
{
MessageBox("读锁具列表失败!");
}
}
谁能把这个VC调用的例子翻译成C#的啊?在线等................
  

解决方案 »

  1.   

    应该是
    int     ReadLockList(int     sys_code,int     password,   ref   int[]   lock_list);     public void OnReadLockList()   

    UpdateData(true); 
    String   m_str; 
    int[] list=new long[3080]; 
    int   length   =   ReadLockList(m_edit1,m_edit2,list); 
    if(length   !=   10000) 

    m_edit27   =   ""; 
    for(int   i=0;   i <length;   i++) 

    m_edit27   = String.Format("{0}{1:d}",m_edit27,list[i]);  

    UpdateData(false); 

    else 

    MessageBox.Show("读锁具列表失败!"); 


      

  2.   

    String       m_str;  好像没用上,可以去掉.UpdateData函数和变量 m_edit1,m_edit2  都是外部定义的
      

  3.   

    把这个OCX拖到工具箱里面,在C#代码中
    这个函树的格式是: 
    int     ReadLockList(int     sys_code,int     password,   ref   int   lock_list);   
    而不是:  
    int     ReadLockList(int     sys_code,int     password,   ref   int[]  lock_list);   写OCX的人说,要用指针,就不怎么写了。
      

  4.   

    现在主要问题是: lock_list这个参数,因为它在VC的代码中是数组型的,但封装到OCX,在C#调用就只有
    int  ReadLockList(int sys_code,int  password,ref  int  lock_list); 如果在C#中传int 整型的参数进去,只能取出lock_list中的第一个值
      

  5.   

    如果这样定义
    int[]   list=new   long[3080];   
    int       length       =       ReadLockList(m_edit1,m_edit2,list);   运行时就出现下面的错误:
    与“AxKEYOCXLib.AxKeyOcx.ReadLockList(int, int, ref int)”最匹配的重载方法具有一些无效参数
    参数“3”: 无法从“ref int[]”转换为“ref int”
      

  6.   

    是不是做成OCX时,输入参数lock_list出错呢???高手帮帮忙啊.......
      

  7.   


    事实上应该是指针,是表示一个长整数数组的指针看VC的调用:
    long   list[3080]; //这就是那个长指针
    long   length   =   m_keyocx.ReadLockList(m_edit1,m_edit2,list); 你可以用不安全代码,定义一个长整数数组,然后把这个数组的地址转成int传到C#自动生成的方法里面。我没具体试过,但感觉这样应该可以
      

  8.   

    你可以把那个数组的指针参数类型定义为IntPtr
    然后你把那个数组用Marshal.Copy方法拷贝到一个IntPtr变量里面传入
      

  9.   


    IntPtr arrptr=IntPtr.Zero;
    long   list[3080]={0};
    MarShal.Copy(list,0,arrptr,list.Length);
    int length = ReadLockList(m_edit1,m_edit2,arrptr.ToInt32);  
      

  10.   

    出了点小问题,晕,vb玩惯了...int length = ReadLockList(m_edit1,m_edit2,arrptr.ToInt32());
      

  11.   

    IntPtr arrptr=IntPtr.Zero;
                    long[]   list=new long[3080];
                    for (int p = 0; p < 3080;++p )
                        list[p] = 0;
                    Marshal.Copy(list, 0, arrptr, list.Length);
                    int abc=arrptr.ToInt32();
                    RecordCount = axKeyOcx1.ReadLockList(sys_code, password, ref abc);
                    int mm = 0;
                    if (RecordCount != 10000)
                    {
                        string strlock_list = "";
                        for (int i = 0; i < RecordCount; ++i)
                        {
                            mm = (int)list[i];
                            strlock_list = strlock_list + mm.ToString() + " ";
                        }
                        this.textBox1.Text = strlock_list;                }
    调试到:Marshal.Copy(list, 0, arrptr, list.Length);
    出现异常:
    未处理 ArgumentNullException
    值不能为空。
    参数名: destination这是什么问题呢?
      

  12.   

    先用IntPtr   arrptr=Marshal.AllocHGlobal(sizeof(list));分配内存看看