temp = Marshal.AllocHGlobal(IntPtr.Size);
这个没申请成功?
检测下if (temp != IntPtr.Zero){
    ask(question, out temp);}else{
//
//报错
//
}

解决方案 »

  1.   

    内存的释放问题,DLL里申请的内存,你的代码里自动释放了。最好别用IntPtr直接获得DLL里的内存首地址,这样很容易造成内存释放问题。比较好的方法是,你自己再申请一块内存来拷贝DLL内存里的东西,然后DLL用的内存你就别去访问,操作自己申请的内存,或者干脆就你自己申请内存,给DLL去操作。
      

  2.   

    void ask(const char *msg, char **answer);
    在C#中应该这么写[DllImport(@"XXX.dll")]
    //如果这个函数第2个参数是一个out参数
    public extern static int ask(string msg, out StringBuilder[] temp);
      

  3.   

      回:sjzlxd
    如果 在c#中这么写:
    [DllImport(@"XXX.dll")]
    //如果这个函数第2个参数是一个out参数
    public extern static int ask(string msg, out StringBuilder[] temp);那该怎么调用呢?我按西面写,运行时就直接抛出异常了,一次调用也过不去
                StringBuilder[] temp = new StringBuilder[200];
                ask(Id, question, out temp);
      

  4.   

    Wtypes.h   中的非托管类型         非托管C   语言类型         托管类名               说明     
      HANDLE                                           void*                               System.IntPtr     32   位     
      BYTE                                               unsigned   char               System.Byte         8   位     
      SHORT                                             short                               System.Int16       16   位     
      WORD                                               unsigned   short             System.UInt16     16   位     
      INT                                                 int                                   System.Int32       32   位     
      UINT                                               unsigned   int                 System.UInt32     32   位     
      LONG                                               long                                 System.Int32       32   位     
      BOOL                                               long                                 System.Int32       32   位     
      DWORD                                             unsigned   long               System.UInt32     32   位     
      ULONG                                             unsigned   long               System.UInt32     32   位     
      CHAR                                               char                                 System.Char         用   ANSI   修饰。     
      LPSTR                                             char*                               System.String   或   System.StringBuilder   用   ANSI   修饰。     
      LPCSTR                                           Const   char*                   System.String   或   System.StringBuilder   用   ANSI   修饰。     
      LPWSTR                                           wchar_t*                           System.String   或   System.StringBuilder   用   Unicode   修饰。     
      LPCWSTR                                           Const   wchar_t*             System.String   或   System.StringBuilder   用   Unicode   修饰。     
      FLOAT                                             Float                                 System.Single   32   位     
      DOUBLE                                           Double                                   System.Double   64   位
      

  5.   

    To LCL_data:
    这个表我也搜索到了,我最开始用IntPtr的写法就是参照这个表写的,但是只能调用一次,多次调用就会抛出异常
      

  6.   

    public extern static int ask(string msg, String[] temp);试试
      

  7.   

    to LCL_data:
    public extern static int ask(string msg, String[] temp);
    这种方式我也试过,需要加上ref或out才有值,不加的话,temp就是空的,但是也只能调用1次,多次调用后就会随机抛出异常