C# 无法正确封送结构中的StringBuilder字段

C# 无法正确封送结构中的StringBuilder字段,c#,pinvoke,marshalling,C#,Pinvoke,Marshalling,我已经和at p/invoke问题斗争了一段时间了。不断尝试读取或写入受保护内存。一直以来,我怀疑我的指挥有点不对劲 这是c签名: long ft_show( /* show file-attributes */ const struct ft_admission *admis, /* I: transfer admission */ const struct ft_shwpar *p

我已经和at p/invoke问题斗争了一段时间了。不断尝试读取或写入受保护内存。一直以来,我怀疑我的指挥有点不对劲

这是c签名:

long ft_show(                             /* show file-attributes        */
    const struct ft_admission *admis,     /* I: transfer admission       */
    const struct ft_shwpar    *par,       /* IO: parameter-list          */
    struct ft_fileinfo        *info,      /* O: requested information    */
    struct ft_err             *errorinfo, /* O: error information        */
    void                      *options    /* I: options                  */
    );
下面是c中的相关结构

struct ft_admission
{
    char    *remsys;
    char    *remadmis;
    char    *remaccount;
    char    *rempasswd;
};

struct ft_shwpar
{
    int                shwparvers;
    char               *fn;
    char               *mgmtpasswd;
    char               *fud;
    int                fudlen;
};

struct ft_fileinfo
{
    int             ftshowivers;
    char            fn[INFO_FN_LEN];
    enum ft_ftype   filetype;
    enum ft_charset charset;
    enum ft_rform   recordform;
    long            recsize;
    enum ft_available availability;
    int             access;
    char            account[ACC_LEN];
    long            size;
    long            maxsize;
    char            legalqual[LQ_LEN];
    char            cre_user[USER_LEN];
    long            cre_date;
    char            mod_user[USER_LEN];
    long            mod_date;
    char            rea_user[USER_LEN];
    long            rea_date;
    char            atm_user[USER_LEN];
    long            atm_date;
    long long       fsize;
    long long       fmaxsize;
};

struct ft_err
{
    long    main;
    long    detail;
    long    additional;
};

struct ft_options
{                                                                       
    int ftoptsvers;
    int ftapivers;
};
我尝试过创建c结构和调用,如下所示:

[DllImport("ftapi.dll", EntryPoint = "ft_showdir")]
private static extern long ft_showdir(ft_admission admis, ref ft_shwpar par, ft_fileinfo[] buf, int bufsize, ref ft_err errorinfo, ft_options options);

    public struct ft_admission
    {
        public String remsys;
        public String remadmis;
        public String remaccount;
        public String rempasswd;
    };
    public struct ft_shwpar
    {
        public int shwparvers;
        public String fn;
        public String mgmtpasswd;
        public IntPtr fud;
        public int fudlen;
    };
    [StructLayout(LayoutKind.Sequential, Size = 1464, CharSet = CharSet.Ansi), Serializable]
    public struct ft_fileinfo
    {
        public int ftshowivers;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
        public String fn;
        public ft_ftype filetype;
        public ft_charset charset;
        public ft_rform recordform;
        public long recsize;
        public ft_available availability;
        public int access;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 65)]
        public String account;
        public long size;
        public long maxsize;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)]
        public String legalqual;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 68)]
        public String cre_user;
        public long cre_date;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 68)]
        public String mod_user;
        public long mod_date;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 68)]
        public String rea_user;
        public long rea_date;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 68)]
        public String atm_user;
        public long atm_date;
        public long fsize;
        public long fmaxsize;
    };
    public struct ft_err
    {
        public long main;
        public long detail;
        public long additional;
    }
    public struct ft_options
    {
        public int ftoptsvers;
        public int ftapivers;
    };
我已经在另一个调用中成功地使用了ft_err和ft_options结构,因此我认为它们已正确封送

我认为问题在于ft_文件信息结构。我想在其中使用StringBuilder,但框架中存在一个错误,阻止了StringBuilder在结构内部的正确封送。我已经尝试了这里描述的解决方法: 但是没有成功

以下是我试图访问的api文档中提供的代码片段。一些代码已被删除,因为它仅用于将信息打印到屏幕上:

/* sample3.c     File management requests                    */
/*************************************************************/
static const char Sccsid[] = "@(#)sample3.c   2.33 2009/03/25";
/*************************************************************

  Program call: sample3 <directory>

 *************************************************************/
/*   Include Files                                           */
/*************************************************************/
#include <stdio.h>       /* printf()                         */
#include <stdlib.h>      /* exit()                           */
#include <string.h>      /* strcat(), strcpy()               */
#include <ftapi.h>

/*************************************************************/
/*                Local Constants and Macros                 */
/*************************************************************/
#define BUFSIZE 200      /* number of ft_fileinfo structures */
                         /* in output buffer                 */

/*************************************************************/
/*               Local Functions Declarations                */
/*************************************************************/
static void error_print(struct ft_err *, char *);
static void printinfo(struct ft_fileinfo *info); 

int main (int argc, char *argv[])
{
    int i;                     /* Some local counter         */
    int count;                 /* Number of files            */
    char remotesys[201];       /* Address of remote systems  */
    char remoteadm[68];        /* Transfer admission         */
    char fud[STAT_FUD_LEN];    /* Further details            */
    struct ft_err errorinfo;   /* Error information          */
    struct ft_admission admis; /* Admission parameters       */
    struct ft_shwpar par;      /* Show directory parameters  */
    struct ft_options opt;     /* Options                    */
    struct ft_fileinfo buf[BUFSIZE]; /* Output buffer        */

    /* Check program arguments                               */
    if (argc != 2)
    {
        printf("Call: %s <directory>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* Initialize ft_transpar structure                      */
    memset(&par, 0, sizeof(par));
    /* Set version of the parameter list                     */
    par.shwparvers  = FT_SPARV2;
    /* Set the name of the remote directory                  */
    par.fn = argv[1];
    /* Set buffer and buffer length for the further details  */
    par.fud = fud;               
    par.fudlen = sizeof(fud);      
    /* Set version of the ft_fileinfo structure in the       */
    /* output buffer. The version in the first structure     */
    /* is valid for all structtures in the output buffer.    */
    buf[0].ftshowivers = FT_SHOWIV2;

    /* Get the name/address of the remote system and the     */
    /* transfer admission                                    */
    printf("Enter name of remote system: ");
    scanf("%s", remotesys);
    printf("Input transfer admission to remote system: ");
    scanf("%s", remoteadm);
    admis.remsys     = remotesys;
    admis.remadmis   = remoteadm;
    admis.remaccount = NULL;
    admis.rempasswd  = NULL;

    /* Prepare the options structure                         */
    opt.ftoptsvers = FT_OPTSV1;
    opt.ftapivers = FT_APIV2; 

    /* Read the contents of the remote directory             */
    count = ft_showdir(&admis, &par, buf, BUFSIZE, &errorinfo,
                       &opt);
    if (count == -1) 
    {   
        /* Error                                             */
        error_print(&errorinfo, par.fud); 
    }

    /* Display result of request                             */
    printf("There are %d entries in remote directory %s\n",
           count, argv[1]);

    /* If the output range was not large enough for all      */
    /* the information, only the data in the buffer is       */
    /* displayed                                             */
    if (count > BUFSIZE)
        count = BUFSIZE;
    for (i = 0; i < count; i++)
         printinfo(&buf[i]);
    return(0);
}
有人能帮我在C中实现同样的目标吗?甚至解释为什么必须这样,这样我们都可以从中学习:


谢谢

我看到的第一个问题是在C结构声明中使用长类型。替换为int-C long的长度为64位,而Win32 C中的long的大小与int-32位相同

关于ft_fileinfo*info参数:对于默认封送处理程序无法处理的每个非平凡参数,请使用低级封送处理类函数。函数需要info指向大小为sizeoffileinfo*bufsize的非托管内存块。将此参数定义为IntPtr:

[DllImport("ftapi.dll", EntryPoint = "ft_showdir")] private static extern long ft_showdir(ft_admission admis, ref ft_shwpar par, IntPtr buf, int bufsize, ref ft_err errorinfo, ft_options options); 分配非托管内存块:

IntPtr f = Marshal.AllocHGlobal(Marshal.Sizeof(typeof(ft_fileinfo)) * bufsize); 将此参数传递给非托管函数。函数返回后,使用Marshal.PtrToStructure方法bufsize times读取fileinfo结构,将IntPtr增加sizeoft_fileinfo。不要忘记使用Marshal.FreeHGlobal释放非托管内存块


您可以看到,封送处理类允许在C语言级别编写互操作性代码。

看起来您的DllImport属性中的ft_fileinfo前面缺少一个ref。我复制了你的问题,删减如下。在C++中定义入口点:

    // OneEntryPoint.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <stdio.h>
extern "C"
{
    struct AStruct
    {
        char     fn[11];
    };

    __declspec(dllexport) void AnEntry(AStruct* someStruct) 
    {
        printf("A character %c", someStruct->fn[255]); 
    }
}
运行此操作,您将获得AccessViolationException尝试读取\写入受保护内存。将ref放入,结构被正确封送,没有错误

好吧,我想出来了

当查看函数的c声明时,我们可以看到所有参数都是通过引用或指针发送的。所以我只是修改了pinvoke声明,这样所有参数

[DllImport("ftapi.dll", EntryPoint = "ft_showdir", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 ft_showdir(ref ft_admission admis, ref ft_shwpar par, ref ft_fileinfo[] buf, int bufsize, ref ft_err errorinfo, ref ft_options options);

对不起,这不起作用。问题是我传入了一个ft_fileinfo结构数组,它是一个in/out参数。这就是为什么示例设置codebuf[0].ftshowivers=FT_SHOWIV2;代码我可以提供完整的示例代码以及ftapi.dll,如果这有任何帮助的话。嗯,你的正确答案不是在我刚才说的下面吗?你需要将参数作为引用传递吗?因为struct是一个值参数,构造在堆栈上,而您的c代码需要指向它的指针,而不是副本。很抱歉,没有成功。我已经尝试过很多不同的版本,但是为了它我尝试了一个单独的版本。我建议你简化它,试着一次调试一小块。搜索数百行代码并不能完成任务。
[DllImport("ftapi.dll", EntryPoint = "ft_showdir", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 ft_showdir(ref ft_admission admis, ref ft_shwpar par, ref ft_fileinfo[] buf, int bufsize, ref ft_err errorinfo, ref ft_options options);