This is my structure and function in C ++ dll
struct Address { TCHAR* szAddress; }; extern "C" DllExport void SetAddress(Address* addr);
From C #, I want to name this API by passing an address structure. So I have the following in C #
[StructLayout(LayoutKind.Sequential)] public struct Address { [MarshalAs(UnmanagedType.LPTStr)] public String addressName; } [DllImport("Sample.dll")] extern static void SetAddress(IntPtr addr);
Now I call the C ++ API from C #
Address addr = new Address(); addr.addressName = "Some Address"; IntPtr pAddr = Marshal.AllocHGlobal(Marshal.SizeOf(addr)); Marshal.StructureToPtr(addr , pAddr , false); SetAddress(pAddr);
I get NULL for Address.szAddress in C ++ code. Any idea what is going wrong here?
source share