将 C 函数和 DLL 结构类型正确转换/编组为 C#

发布于 2024-12-11 02:35:54 字数 582 浏览 0 评论 0原文

有人可以帮我将 C 结构/函数转换为 C# 吗?

我相信,如果我能够正确转换函数并正确编组我的结构,我就可以正确调用 DLL。

这是我的结构和使用它的函数:

typedef struct UIclient_info {
  char name[50];
  int age;
  SECONDUSER *stParent;
} FIRSTUSER;

typedef struct ui_product_form {    
  char m_sCoupon;
  double amountTotal;
  char m_sErrorCode[50];
  SUBPRODUCT *pstSubProduct;
} PRODUCT;

typedef struct tagReport
{
  REPORTSUMMARY *pstRepsum;
  char errorMsg[100];
} FINAL_REPORT;

int Save(FIRSTUSER *myUser, PRODUCT *uiStructure);
FINAL_REPORT GenerateReport(FIRSTUSER *myUser, PRODUCT *uiStructure);

Can someone please help me convert my C structure/function into C#?

I believe I can call the DLL properly if I have able to correctly convert the function and marshal properly my structure.

Here is my struct and the function that uses it:

typedef struct UIclient_info {
  char name[50];
  int age;
  SECONDUSER *stParent;
} FIRSTUSER;

typedef struct ui_product_form {    
  char m_sCoupon;
  double amountTotal;
  char m_sErrorCode[50];
  SUBPRODUCT *pstSubProduct;
} PRODUCT;

typedef struct tagReport
{
  REPORTSUMMARY *pstRepsum;
  char errorMsg[100];
} FINAL_REPORT;

int Save(FIRSTUSER *myUser, PRODUCT *uiStructure);
FINAL_REPORT GenerateReport(FIRSTUSER *myUser, PRODUCT *uiStructure);

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

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

发布评论

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

评论(1

一刻暧昧 2024-12-18 02:35:54

这可能有效,也可能无效。你可以试试,我不知道我是否正确处理了指针。我无法就此提供任何其他支持。如果它有效,那么它就有效。如果没有的话……哦,好吧。

//typedef struct UIclient_info {
//  char name[50];
//  int age;
//  SECONDUSER *stParent;
//} FIRSTUSER;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct FirstUser
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)]
    public string Name;
    public int Age;
    [MarshalAs(UnmanagedType.LPStruct)]
    public IntPtr Parent;
}

//typedef struct ui_product_form {    
//  char m_sCoupon;
//  double amountTotal;
//  char m_sErrorCode[50];
//  SUBPRODUCT *pstSubProduct;
//} PRODUCT;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct Product
{
    public char Coupon;
    public double AmountTotal;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)]
    public string ErrorCode;
    [MarshalAs(UnmanagedType.LPStruct)]
    public IntPtr SubProduct;
}

//typedef struct tagReport
//{
//  REPORTSUMMARY *pstRepsum;
//  char errorMsg[100];
//} FINAL_REPORT;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct FinalReport
{
    [MarshalAs(UnmanagedType.LPStruct)]
    public IntPtr ReportSummary;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
    public string ErrorMsg;
}

static class MyLibraryHelper
{
    //int Save(FIRSTUSER *myUser, PRODUCT *uiStructure);

    [DllImport(@"MyLibrary.dll", CharSet=CharSet.Ansi)]
    internal static extern int Save(IntPtr myUser, IntPtr uiStructure);

    //FINAL_REPORT GenerateReport(FIRSTUSER *myUser, PRODUCT *uiStructure);

    [DllImport(@"MyLibrary.dll", CharSet=CharSet.Ansi)]
    internal static extern FinalReport GenerateReport(IntPtr myUser, IntPtr uiStructure);
}

This may or may not work. You can try it, I don't know whether or I handled the pointers correctly. I won't be able to offer any other support on this. If it works, then it works. If not then... oh well.

//typedef struct UIclient_info {
//  char name[50];
//  int age;
//  SECONDUSER *stParent;
//} FIRSTUSER;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct FirstUser
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)]
    public string Name;
    public int Age;
    [MarshalAs(UnmanagedType.LPStruct)]
    public IntPtr Parent;
}

//typedef struct ui_product_form {    
//  char m_sCoupon;
//  double amountTotal;
//  char m_sErrorCode[50];
//  SUBPRODUCT *pstSubProduct;
//} PRODUCT;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct Product
{
    public char Coupon;
    public double AmountTotal;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)]
    public string ErrorCode;
    [MarshalAs(UnmanagedType.LPStruct)]
    public IntPtr SubProduct;
}

//typedef struct tagReport
//{
//  REPORTSUMMARY *pstRepsum;
//  char errorMsg[100];
//} FINAL_REPORT;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct FinalReport
{
    [MarshalAs(UnmanagedType.LPStruct)]
    public IntPtr ReportSummary;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
    public string ErrorMsg;
}

static class MyLibraryHelper
{
    //int Save(FIRSTUSER *myUser, PRODUCT *uiStructure);

    [DllImport(@"MyLibrary.dll", CharSet=CharSet.Ansi)]
    internal static extern int Save(IntPtr myUser, IntPtr uiStructure);

    //FINAL_REPORT GenerateReport(FIRSTUSER *myUser, PRODUCT *uiStructure);

    [DllImport(@"MyLibrary.dll", CharSet=CharSet.Ansi)]
    internal static extern FinalReport GenerateReport(IntPtr myUser, IntPtr uiStructure);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文