移植C++;结构转换为C#(从非托管dll) 我是一个汽车工程师,我的公司决定购买一个API,它只在C++中(我得到了未经修改的DLL和头文件)。问题是,我只使用C#编写代码,我们的大多数应用程序都非常容易构建,因为硬件提供商总是用C#(托管dll)为我们提供API。我现在需要将非托管dll中的所有函数转换为C#函数。在我遇到之前,一切都很顺利 typedef struct can_msg { unsigned short ide; // Standard/extended msg unsigned int id; // 11 or 29 bit msg id unsigned short dlc; // Size of data unsigned char data[CAN_MSG_DATA_LEN]; // Message pay load unsigned short rtr; // RTR message } can_msg_t;

移植C++;结构转换为C#(从非托管dll) 我是一个汽车工程师,我的公司决定购买一个API,它只在C++中(我得到了未经修改的DLL和头文件)。问题是,我只使用C#编写代码,我们的大多数应用程序都非常容易构建,因为硬件提供商总是用C#(托管dll)为我们提供API。我现在需要将非托管dll中的所有函数转换为C#函数。在我遇到之前,一切都很顺利 typedef struct can_msg { unsigned short ide; // Standard/extended msg unsigned int id; // 11 or 29 bit msg id unsigned short dlc; // Size of data unsigned char data[CAN_MSG_DATA_LEN]; // Message pay load unsigned short rtr; // RTR message } can_msg_t;,c#,c++,dll,C#,C++,Dll,我不知道如何使用它,因为此结构是函数的参数,例如: VTC1010_CAN_BUS_API int CAN_Transmission(can_msg_t *msg); 请帮帮我。不要因为我缺乏知识而责骂我。我试图找到,但对我来说太难了 假设您不知道如何在.net托管应用程序中使用未老化的DLL,我可以告诉您一些非常基本的事情。 这样做的方法是使用VisualC++来为非托管类创建一个“包装器”,然后您可以使用这个包装器中定义的类与未经处理的代码一起运行。p> 您可以在此处找到一个很好的教程:

我不知道如何使用它,因为此结构是函数的参数,例如:

VTC1010_CAN_BUS_API int  CAN_Transmission(can_msg_t *msg);

请帮帮我。不要因为我缺乏知识而责骂我。我试图找到,但对我来说太难了

假设您不知道如何在.net托管应用程序中使用未老化的DLL,我可以告诉您一些非常基本的事情。 这样做的方法是使用VisualC++来为非托管类创建一个“包装器”,然后您可以使用这个包装器中定义的类与未经处理的代码一起运行。p> 您可以在此处找到一个很好的教程:


抱歉,请不要说得太具体,但您需要在创建代码之前开始学习。祝你好运

这是一个通过筛子快速而肮脏的转换

输入代码:

#define CAN_MSG_DATA_LEN 100 // adjust correct value here

typedef struct can_msg
{
    unsigned short ide;                         // Standard/extended msg
    unsigned int id;                            // 11 or 29 bit msg id
    unsigned short dlc;                         // Size of data
    unsigned char data[CAN_MSG_DATA_LEN];       // Message pay load
    unsigned short rtr;                         // RTR message
} can_msg_t;

int  CAN_Transmission(can_msg_t *msg);
public partial class NativeConstants {

    /// CAN_MSG_DATA_LEN -> 100
    public const int CAN_MSG_DATA_LEN = 100;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct can_msg {

    /// unsigned short
    public ushort ide;

    /// unsigned int
    public uint id;

    /// unsigned short
    public ushort dlc;

    /// unsigned char[100]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=100)]
    public string data;

    /// unsigned short
    public ushort rtr;
}

public partial class NativeMethods {

    /// Return Type: int
    ///msg: can_msg_t*
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="CAN_Transmission")]
public static extern  int CAN_Transmission(ref can_msg msg) ;

}
输出代码:

#define CAN_MSG_DATA_LEN 100 // adjust correct value here

typedef struct can_msg
{
    unsigned short ide;                         // Standard/extended msg
    unsigned int id;                            // 11 or 29 bit msg id
    unsigned short dlc;                         // Size of data
    unsigned char data[CAN_MSG_DATA_LEN];       // Message pay load
    unsigned short rtr;                         // RTR message
} can_msg_t;

int  CAN_Transmission(can_msg_t *msg);
public partial class NativeConstants {

    /// CAN_MSG_DATA_LEN -> 100
    public const int CAN_MSG_DATA_LEN = 100;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct can_msg {

    /// unsigned short
    public ushort ide;

    /// unsigned int
    public uint id;

    /// unsigned short
    public ushort dlc;

    /// unsigned char[100]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=100)]
    public string data;

    /// unsigned short
    public ushort rtr;
}

public partial class NativeMethods {

    /// Return Type: int
    ///msg: can_msg_t*
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="CAN_Transmission")]
public static extern  int CAN_Transmission(ref can_msg msg) ;

}
公共部分类NativeConstants{
///CAN\u MSG\u DATA\u LEN->100
公共常数int CAN\u MSG\u DATA\u LEN=100;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential,CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
公共结构can\u msg{
///无符号短
公共场所;
///无符号整数
公共uint id;
///无符号短
公共图书馆;
///无符号字符[100]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr,SizeConst=100)]
公共字符串数据;
///无符号短
公共卫生服务;
}
公共部分类NativeMethods{
///返回类型:int
///味精:可以吗*
[System.Runtime.InteropServices.DllImportAttribute(“,EntryPoint=“CAN_传输”)]
公共静态外部CAN\ U传输(参考CAN\ U msg);
}

它是有效的,但当我得到一个结构作为论点时,我感到困惑,这就是为什么我问这个问题,你已经知道short和int是什么样子了。字符[]需要
固定字节数据[something]
。谢谢您的回答。这给了我一个起点。非常感谢谢谢!对不起,我没有看到你评论的任何链接,你忘了什么吗?嘿,艾比,请看我的答案。这是处理未管理dll及其函数的好方法吗?您应该在问题中添加它,而不是发布答案。。。总之,1。为什么不像其他声明一样使用
extern
funcs呢?2.在我看来,我会抛出一个异常并在上面的应用程序中处理它,而不是显示一个消息框。3.您应该在请求代码审查,因为这正是本网站的目的:d感谢您的审查。我不知道这样一个网站的存在。1.我将尝试使用“外部”。2.当我在试验我的功能时,我使用消息框了解。试着去了解它是对还是错。再次感谢。。