Я совершенно новичок в 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, с которым я пытаюсь взаимодействовать. Любая помощь будет оценена.
EDIT:
Вот больше информации, функция в 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);