C#使用FILE *参数调用C函数

时间:2014-02-27 12:30:05

标签: c# c pinvoke

我在C库的结构中定义了以下函数指针:

struct SOME_STRUCT {
    [...]
    uint8_t(*printinfo) (SOME_STRUCT * ss, FILE * hFile);
    [...]
}

此函数将一些数据写入文件句柄hFile,我想从C#中调用它。在C#中我有:

[StructLayout(LayoutKind.Sequential)]
public struct SomeStruct
{
    [...]
    public printinfoDelegate printinfo;

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate byte printinfoDelegate(IntPtr ss, IntPtr hFile);
    [...]
}

我使用以下代码调用该函数:

SomeStruct sStruct = [...];
String output;

using (FileStream stream = new FileStream(tmpFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
    IntPtr structPtr = Marshal.AllocHGlobal(Marshal.SizeOf(sStruct));
    Marshal.StructureToPtr(sStruct, structPtr, false);

    byte result = sStruct.printinfo(structPtr, stream.SafeFileHandle.DangerousGetHandle());

    stream.Seek(0, System.IO.SeekOrigin.Begin);

    using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
    {
        output = reader.ReadToEnd();
    }
}

但我无法让它发挥作用。我怀疑问题是我不能只将文件流中的句柄作为文件*传递。任何帮助将不胜感激......

3 个答案:

答案 0 :(得分:2)

.NET中的句柄引用Win32 HANDLE(或HINSTANCE等等),例如: CreateFile函数返回的那个。另一方面,FILE *是C运行时库的一部分,并通过调用fopen函数返回。

因此,如果您想使用带有FILE *参数的函数,那么您也必须P / Invoke fopen方法,例如here

答案 1 :(得分:1)

我认为没有办法将FileStream编组为FILE *。如果只需要C函数内部的流,可以通过callind fdopen()创建句柄。

答案 2 :(得分:0)

您需要为fopenfclose声明包装器。你需要的是这样的:

public static class LegacyFileManager
{
    [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public static extern IntPtr fopen(String filename, String mode);
    [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public static extern Int32 fclose(IntPtr file);
}

然后在你的代码中:

IntPtr fileHandle = LegacyFileManager.fopen(tmpFileName, "r+");

// your logic here

LegacyFileManager.fclose(fileHandle);
相关问题