首页 / C# / 帮我把C结构转换成C#
帮我把C结构转换成C#
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了帮我把C结构转换成C#,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4515字,纯文字阅读大概需要7分钟。
内容图文
![帮我把C结构转换成C#](/upload/InfoBanner/zyjiaocheng/821/9af8dfc7f796427094da781408d77140.jpg)
我是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);
解决方法:
请尝试以下定义
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;
}
内容总结
以上是互联网集市为您收集整理的帮我把C结构转换成C#全部内容,希望文章能够帮你解决帮我把C结构转换成C#所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。