编组包含字符串的结构

发布于 2024-08-16 16:11:11 字数 1614 浏览 6 评论 0原文

我基本上想在 C# 中从用户那里获取 int name 和字符串年龄,并将其发送到用 c 编写的 dll 方法,该方法接受 int 和 char[50] 参数并返回字符串。我创建了以下场景,但我失败了,任何机构都有 我有一个用 c 开发的 dll代码

,它有一个结构

struct Argument 
{
int age;
char name[50];
} ;

和一个方法,

extern "C"
{
    __declspec(dllexport) Argument FillData(Argument data)
 {
        Argument mydata;

        mydata.age=data.age;
        for(int i=0;i<=sizeof(data);i++)
        {
            mydata.name[i]=data.name[i];

        }
        return mydata;

 }

我在 Cs_dll.cs 中的 c# 中声明它,

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
    public struct Argument
    {
        public int age;
        [MarshalAs(UnmanagedType.TBStr)]
        //public char name;
       public char[] name;

    };
  public  class Cs_Dll
    {
      [DllImport("TestLib.dll")]
            public static extern Argument FillData (Argument data);


    }

现在又出现了一个我想做的按钮

 private void button1_Click(object sender, EventArgs e)
        {
            Argument data=new Argument();
            data.age=Convert.ToInt32(textBox_age.Text);
            char[] name={'a','b','r','a','r', ' ', 'a', 'h', 'm', 'e', 'd', '\0' };
            for (int i = 0; i <= name.Length; i++)
            {
                data.name[i] = name[i];
            }

               // Array.Copy(name, data.name, name.Length);


            Argument result = Cs_Dll.FillData(data);
            textBox_get.Text = result.age.ToString();
            textBox_age.Text = result.name.ToString();

        }

,但我遇到了错误

i basically want to take int name and string age from user in c# and send it to dll method written in c which take int and char[50] arguments in it and return string .i created following scenario but i am failed ,any body has the code

i have a dll developed in c which ahas a structure

struct Argument 
{
int age;
char name[50];
} ;

and a method

extern "C"
{
    __declspec(dllexport) Argument FillData(Argument data)
 {
        Argument mydata;

        mydata.age=data.age;
        for(int i=0;i<=sizeof(data);i++)
        {
            mydata.name[i]=data.name[i];

        }
        return mydata;

 }

i declare it in c# in Cs_dll.cs

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
    public struct Argument
    {
        public int age;
        [MarshalAs(UnmanagedType.TBStr)]
        //public char name;
       public char[] name;

    };
  public  class Cs_Dll
    {
      [DllImport("TestLib.dll")]
            public static extern Argument FillData (Argument data);


    }

now againts a button iwant to do

 private void button1_Click(object sender, EventArgs e)
        {
            Argument data=new Argument();
            data.age=Convert.ToInt32(textBox_age.Text);
            char[] name={'a','b','r','a','r', ' ', 'a', 'h', 'm', 'e', 'd', '\0' };
            for (int i = 0; i <= name.Length; i++)
            {
                data.name[i] = name[i];
            }

               // Array.Copy(name, data.name, name.Length);


            Argument result = Cs_Dll.FillData(data);
            textBox_get.Text = result.age.ToString();
            textBox_age.Text = result.name.ToString();

        }

but i am stuck with error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能否归途做我良人 2024-08-23 16:11:11

您需要将 Argument 的结构定义更改为


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Argument
{
    public int age;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
    public string name;
}

- 或 -


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
unsafe public struct Argument
{
    public int age;
    fixed char name[50];
}

您还可能会找到文章字符串的默认封送处理 有帮助。

You need to change your struct definition of Argument to either


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Argument
{
    public int age;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
    public string name;
}

- or -


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
unsafe public struct Argument
{
    public int age;
    fixed char name[50];
}

You might also find the article Default Marshaling for Strings helpful.

怀里藏娇 2024-08-23 16:11:11

在结构体中,要封送定义为 char[] 的 char 数组,您应该使用 UnmanagementType.ByValTStr。

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Argument
{
    public int age;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
    public string name;
}

Within a struct, to marshal char arrays defined as char[] you should use the UnmanagedType.ByValTStr instead.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Argument
{
    public int age;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
    public string name;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文