帮我把 C++ 结构转换成 C#
Posted
技术标签:
【中文标题】帮我把 C++ 结构转换成 C#【英文标题】:Help me convert C++ structure into C# 【发布时间】:2010-02-08 19:12:53 【问题描述】:我对 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);
编辑2: 以下代码块是该问题的解决方案: 对于结构:
[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);
【问题讨论】:
你关心大小还是可以只使用字符串? 我相信尺寸很重要,但我不能确定 【参考方案1】:试试下面的定义
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;
【讨论】:
@JaredPar:您可能需要在属性中包含 SizeConst...即SizeConst = NativeConstants.QUE_ADDR_BUF_LENGTH @tommieb75,它确实在需要它的结构上包含 SizeConst 修饰符。由于代码非常长,您必须向右滚动 我已对原帖进行了编辑,这可能有助于确定答案。 这行得通。感谢您的帮助。我会用正确的解决方案更新帖子。【参考方案2】: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];
【讨论】:
@taylonr:我认为这行不通,因为您不能在结构中使用初始化程序... 不确定,MSDN (msdn.microsoft.com/en-us/library/aa288208(VS.71).aspx) 简单地说:结构不能具有以下形式的初始化程序:base(参数列表)。但我不这样做。授予 JaredPar 上面的那个更好......我认为他在我打字时提交了以上是关于帮我把 C++ 结构转换成 C#的主要内容,如果未能解决你的问题,请参考以下文章
哪位高手帮我把汇编语言程序转换成c语言程序啊?(对了是51单片机程序)
#FF99FF这是粉色字的颜色代码,有谁能帮我把它转成十进制的代码哦?~谢谢~!