帮我转换 C++结构体转换为 C#

发布于 2024-08-20 07:17:09 字数 2796 浏览 4 评论 0原文

我对 C# 完全陌生,需要帮助将 C++ 结构转换为 C#。 C++ 结构给出如下:

#define QUE_ADDR_BUF_LENGTH 50
#define QUE_POST_BUF_LENGTH 11

typedef struct
    {
    const WCHAR *streetAddress;
    const WCHAR *city;
    const WCHAR *state;
    const WCHAR *country;
    const WCHAR *postalCode;
} QueSelectAddressType;

typedef struct
    {
    WCHAR   streetAddress[QUE_ADDR_BUF_LENGTH + 1];
    WCHAR   city[QUE_ADDR_BUF_LENGTH + 1];
    WCHAR   state[QUE_ADDR_BUF_LENGTH + 1];
    WCHAR   country[QUE_ADDR_BUF_LENGTH + 1];
    WCHAR   postalCode[QUE_POST_BUF_LENGTH + 1];
    } QueAddressType;

我无法对 C++ 结构进行更改,因为它们是由我尝试与之交互的 API 定义的。任何帮助将不胜感激。

编辑: 这是更多信息,我尝试调用的 dll 中的函数声明如下:

#ifdef QUEAPI_EXPORTS
#define QueAPIExport __declspec(dllexport)
#elif defined QUEAPI_SERVER
#define QueAPIExport
#else
#define QueAPIExport __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

typedef uint32 QuePointHandle;

QueAPIExport QueErrT16 QueCreatePointFromAddress
    (
    QueSelectAddressType*   addr,   // in:  Address data to search on.
    QuePointHandle*         point   // out: Handle to selected point. Must be closed with QueClosePoint.
    );

这是我定义 DllImport 的方式:

[DllImport("QueAPI.DLL", EntryPoint = "QueCreatePointFromAddress")]
public static unsafe extern QueTypesnConst.QueErrT16 QueCreatePointFromAddress(QueTypesnConst.QueSelectAddressType *address, uint *point);

EDIT2: 以下代码块是该问题的解决方案: 对于结构:

[StructLayout(LayoutKind.Sequential)]
public struct QueSelectAddressType
{
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string streetAddress;
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string city;
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string state;
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string country;
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string postalCode;
};

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct QueAddressType
{
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst=51)]
    public string streetAddress;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 51)]
    public string city;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 51)]
    public string state;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 51)]
    public string country;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 12)]
    public string postalCode;
};

对于 DllImport:

[DllImport("QueAPI.DLL", EntryPoint = "QueCreatePointFromAddress")]
public static extern QueTypesnConst.QueErrT16 QueCreatePointFromAddress(ref QueTypesnConst.QueSelectAddressType address, ref uint point);

I am completely new to C#, and need help converting a C++ structure to C#. The C++ structure is given as:

#define QUE_ADDR_BUF_LENGTH 50
#define QUE_POST_BUF_LENGTH 11

typedef struct
    {
    const WCHAR *streetAddress;
    const WCHAR *city;
    const WCHAR *state;
    const WCHAR *country;
    const WCHAR *postalCode;
} QueSelectAddressType;

typedef struct
    {
    WCHAR   streetAddress[QUE_ADDR_BUF_LENGTH + 1];
    WCHAR   city[QUE_ADDR_BUF_LENGTH + 1];
    WCHAR   state[QUE_ADDR_BUF_LENGTH + 1];
    WCHAR   country[QUE_ADDR_BUF_LENGTH + 1];
    WCHAR   postalCode[QUE_POST_BUF_LENGTH + 1];
    } QueAddressType;

I cannot make changes to the C++ structure as they are defined by the API I am attempting to interface with. Any help would be appreciated.

EDIT:
Here is more information, the function in the dll I am attempting to call is declared the as follows:

#ifdef QUEAPI_EXPORTS
#define QueAPIExport __declspec(dllexport)
#elif defined QUEAPI_SERVER
#define QueAPIExport
#else
#define QueAPIExport __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

typedef uint32 QuePointHandle;

QueAPIExport QueErrT16 QueCreatePointFromAddress
    (
    QueSelectAddressType*   addr,   // in:  Address data to search on.
    QuePointHandle*         point   // out: Handle to selected point. Must be closed with QueClosePoint.
    );

Here is how I have defined the DllImport:

[DllImport("QueAPI.DLL", EntryPoint = "QueCreatePointFromAddress")]
public static unsafe extern QueTypesnConst.QueErrT16 QueCreatePointFromAddress(QueTypesnConst.QueSelectAddressType *address, uint *point);

EDIT2:
The following code blocks were the solution to the problem:
For the Structures:

[StructLayout(LayoutKind.Sequential)]
public struct QueSelectAddressType
{
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string streetAddress;
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string city;
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string state;
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string country;
    [MarshalAsAttribute(UnmanagedType.LPWStr)]
    public string postalCode;
};

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct QueAddressType
{
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst=51)]
    public string streetAddress;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 51)]
    public string city;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 51)]
    public string state;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 51)]
    public string country;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 12)]
    public string postalCode;
};

For the DllImport:

[DllImport("QueAPI.DLL", EntryPoint = "QueCreatePointFromAddress")]
public static extern QueTypesnConst.QueErrT16 QueCreatePointFromAddress(ref QueTypesnConst.QueSelectAddressType address, ref uint point);

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

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

发布评论

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

评论(2

仅冇旳回忆 2024-08-27 07:17:09

尝试下面的定义

public partial class NativeConstants {

    /// QUE_ADDR_BUF_LENGTH -> 50
    public const int QUE_ADDR_BUF_LENGTH = 50;

    /// QUE_POST_BUF_LENGTH -> 11
    public const int QUE_POST_BUF_LENGTH = 11;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct QueSelectAddressType {

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string streetAddress;

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string city;

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string state;

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string country;

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string postalCode;
}

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

    /// WCHAR[51]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=51)]
    public string streetAddress;

    /// WCHAR[51]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=51)]
    public string city;

    /// WCHAR[51]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=51)]
    public string state;

    /// WCHAR[51]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=51)]
    public string country;

    /// WCHAR[12]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=12)]
    public string postalCode;
}

Try the following definition

public partial class NativeConstants {

    /// QUE_ADDR_BUF_LENGTH -> 50
    public const int QUE_ADDR_BUF_LENGTH = 50;

    /// QUE_POST_BUF_LENGTH -> 11
    public const int QUE_POST_BUF_LENGTH = 11;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct QueSelectAddressType {

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string streetAddress;

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string city;

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string state;

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string country;

    /// WCHAR*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string postalCode;
}

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

    /// WCHAR[51]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=51)]
    public string streetAddress;

    /// WCHAR[51]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=51)]
    public string city;

    /// WCHAR[51]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=51)]
    public string state;

    /// WCHAR[51]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=51)]
    public string country;

    /// WCHAR[12]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=12)]
    public string postalCode;
}
锦欢 2024-08-27 07:17:09
public struct QueSelectAddressType
{
    public readonly string StreetAddress;
    public readonly string City;
    public readonly string State;
    public readonly string Country;
    public readonly string PostalCode;

    public QueSelectAddressType(string street, string  city, string state, string country, string code)
{
    this.StreetAddress = street;
    this.City = city;
    this.State = state;
    this.Country = country;
    this.PostalCode = code;
}
}

public struct QueAddressType
{
    char[] streetAddress = new char[QUE_ADDR_BUF_LENGTH + 1];
    char[] city = new char[QUE_ADDR_BUF_LENGTH + 1];
    char[] state = new char[QUE_ADDR_BUF_LENGTH + 1];
    char[] country = new char[QUE_ADDR_BUF_LENGTH + 1];
    char[] postalCode = new char[QUE_POST_BUF_LENGTH + 1];
}
public struct QueSelectAddressType
{
    public readonly string StreetAddress;
    public readonly string City;
    public readonly string State;
    public readonly string Country;
    public readonly string PostalCode;

    public QueSelectAddressType(string street, string  city, string state, string country, string code)
{
    this.StreetAddress = street;
    this.City = city;
    this.State = state;
    this.Country = country;
    this.PostalCode = code;
}
}

public struct QueAddressType
{
    char[] streetAddress = new char[QUE_ADDR_BUF_LENGTH + 1];
    char[] city = new char[QUE_ADDR_BUF_LENGTH + 1];
    char[] state = new char[QUE_ADDR_BUF_LENGTH + 1];
    char[] country = new char[QUE_ADDR_BUF_LENGTH + 1];
    char[] postalCode = new char[QUE_POST_BUF_LENGTH + 1];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文