DLLIMPORT的时候,unsigned char *好象应该定义为char[]

解决方案 »

  1.   

    这属于非托管的调用,如何满足指针的调用呢?首先你可以声明unsafe关键字,指针传递的是非托管堆的内存地址,下面的示例应该可以解决你的问题本示例显示如何从 Platform SDK 调用 Windows ReadFile 函数,这要求使用不安全上下文,因为读缓冲区需要将指针作为参数。// readfile.cs
    // compile with: /unsafe
    // arguments: readfile.cs
     
    // Use the program to read and display a text file.
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
     
    class FileReader
    {
          const uint GENERIC_READ = 0x80000000;
          const uint OPEN_EXISTING = 3;
          IntPtr handle;
     
          [DllImport("kernel32", SetLastError=true)]
          static extern unsafe IntPtr CreateFile(
                string FileName,                    // file name
                uint DesiredAccess,                 // access mode
                uint ShareMode,                     // share mode
                uint SecurityAttributes,            // Security Attributes
                uint CreationDisposition,           // how to create
                uint FlagsAndAttributes,            // file attributes
                int hTemplateFile                   // handle to template file
                );
     
           [DllImport("kernel32", SetLastError=true)]
          static extern unsafe bool ReadFile(
                IntPtr hFile,                       // handle to file
                void* pBuffer,                      // data buffer
                int NumberOfBytesToRead,            // number of bytes to read
                int* pNumberOfBytesRead,            // number of bytes read
                int Overlapped                      // overlapped buffer
                );
     
          [DllImport("kernel32", SetLastError=true)]
          static extern unsafe bool CloseHandle(
                IntPtr hObject   // handle to object
                );
          
          public bool Open(string FileName)
          {
                // open the existing file for reading          
                handle = CreateFile(
                      FileName,
                      GENERIC_READ,
                      0, 
                      0, 
                      OPEN_EXISTING,
                      0,
                      0);
          
                if (handle != IntPtr.Zero)
                      return true;
                else
                      return false;
          }
     
          public unsafe int Read(byte[] buffer, int index, int count) 
          {
                int n = 0;
                fixed (byte* p = buffer) 
                {
                      if (!ReadFile(handle, p + index, count, &n, 0))
                            return 0;
                }
                return n;
          }
     
          public bool Close()
          {
                // close file handle
                return CloseHandle(handle);
          }
    }
      

  2.   

    在VC中我引用那个dll时得到DestData的值用的是memcpy的函数。有没有不用unsafe代码的方法啊?
      

  3.   

    还请 Knight94(愚翁) 详示啊。我主要是想得到数据输出的方法,而不是输入数据的方法。
      

  4.   

    那个是内存地址,是引用传递的参数,你的DLL只认识指向那个内存地址的指针,我不知道在.net 框架中除了非托管的方法可以直接访问地址外还有什么可以访问不属于托管堆地质的方法,这是不属于托管堆的呀
      

  5.   

    unsigned pascal TripleDES(char DESType,                  //输入
                              unsigned char * TripleDESKey,  //输入
                              int SourDataLen,              //输入
                              unsigned char *SourData,        //输入
                              unsigned char *DestData);  //需要的数据  uint TripleDES(char DESType,string TripleDESKey,int SourDataLen,string SourData,StringBuilder DestData);
      

  6.   

    interop里面,输出的字符串就使StringBuilder.其他的可使MarshalAs属性来指定
      

  7.   

    回复人: walaqi(xiaohai) ( ) 信誉:95  2004-09-08 14:04:00  得分: 0  
     
     
       interop里面,输出的字符串就使StringBuilder.其他的可使MarshalAs属性来指定
      
     
    有没有例子?我看过有的贴子说了“输出的字符串就使StringBuilder.其他的可使MarshalAs属性”这种方法,但也惜字如金,看了半天也没明白。先谢了。
      

  8.   

    不太清楚,你要什么例子。其实,在c#中调用别人写的dll时候,一般都用
    StringBuilder来代替char*,例如:
    Dllimport……//这里我略去,你自己写吧
    static extern unsigned pascal TripleDES(
                              char DESType,                  //输入
                              StringBuilder TripleDESKey,  //输入
                              int SourDataLen,              //输入
                              StringBuilder SourData,        //输入
                              StringBuilder DestData);  //需要的数据    
    调用的时候,可以用StringBuilder来声明变量,例如:
    StringBuilder strMyTemp = new StringBuilder(100);//100是你的长度