日期:2014-05-18  浏览次数:20670 次

C#调用C写的函数,但函数的参量是文件指针,请问在C#中该函数如何声明呢?
我用C#做的软件的一个功能模块要调用C语言写的函数,但这个fftshift()的参数被声明为了文件指针,请问在C#里面函数的各参量(文件指针)是如何声明的,然后就可以使用这个函数。

/*函数 fftshift()
输入参数:file1 输入数组文件指针,file2输出数组文件指针,Row 输入数组行数,Column 输入数组列数,
flag 交换方向设置(flag=1左右交换,flag=2 上下交换)*/ 
void fftshift(FILE *file1,FILE *file2,int Row,int Column,int flag)
{............}

------解决方案--------------------
FILE是个struct
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
个人认为,你可以用自定义的struct替代
------解决方案--------------------
http://blog.163.com/xiaozhi797@126/blog/static/62440288201231341344512/
------解决方案--------------------
C# code

void fftshift(ref FILE file1,ref FILE  file2,int Row,int Column,int flag)
{............}

------解决方案--------------------
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;
这是C++对FILE的定义,我想跟C应该差不多吧,C#需要自己定义结构相同的FILE。

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct FILE {

/// char*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string _ptr;

/// int
public int _cnt;

/// char*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string _base;

/// int
public int _flag;

/// int
public int _file;

/// int
public int _charbuf;

/// int
public int _bufsiz;

/// char*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string _tmpfname;
}
试试吧,不一定能解决问题。