0

I have a C DLL being called by a C# app, exchanging data using pinvoke. In the following, I have a simple class consisting of an int and 2 arrays of chars. Everything works dandy up to a point: the C# calls the DLL correctly (using "getStreamGroup"), and the DLL fills the passed streamGroup structure with the correct data.

But, once the C function is done, and we are back on the C# side, the streamGroup that got passed and filled with the correct data is now barren: 3 null values. No errors/warnings from VS2010. This is a 64bit app. Any ideas?

#define STREAM_COUNT 9000

typedef struct s_streamGroup
{
    int systemDefinedGroup;

    char name[BUFFER_SIZE_128];
    char streamList[STREAM_COUNT];

} streamGroup;


public class streamGroup
{
    public int systemDefinedGroup;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = constants.BUFFER_SIZE_128)]
    public byte[] name;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = constants.STREAM_COUNT)]
    public byte[] streamList;
}


DLL int getStreamGroup( int groupIndex, streamGroup *RequestedStreamGroup)
{   
    *RequestedStreamGroup = Environment.StreamGroup[groupIndex];
    return(DLL_NO_ERROR);
}

[DllImport(constants.DLL_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int getStreamGroup([In] int groupIndex, [In, Out] streamGroup group);
4

1 回答 1

0

在 streamGroup 之前需要 [StructLayout(LayoutKind.Sequential)]。

如果这还不够,请让你的代码更清晰一点。我不确定哪个项目中有什么代码(如果我理解正确,你有一个本地 C++ 项目和一个 C# 项目)。请给出两个单独的清单,显示哪个是哪个,或者如果我的理解不正确,请解释你在做什么。

于 2012-09-24T23:36:42.627 回答