c# - P/Invoke Calls Failing -
i have following struct defined in c++:
struct graphicsadapterdesc { // ... constructors / operators / destructor here define_default_constructor(graphicsadapterdesc); define_default_destructor(graphicsadapterdesc); allow_copy_assign_move(graphicsadapterdesc); std::wstring adaptername; int32_t adapternum; std::wstring hardwarehash; int64_t dedicatedvmem; int64_t dedicatedsmem; int64_t sharedsmem; int32_t numoutputs; };
in c#, have 'mirror' struct declared thusly:
[structlayout(layoutkind.sequential)] public struct graphicsadapterdesc { string adaptername; int adapternum; string hardwarehash; long dedicatedvmem; long dedicatedsmem; long sharedsmem; int numoutputs; };
i've tried careful matching widths of variables (although i'm bit unsure on strings exactly).
anyway, have following exported c method:
extern "c" __declspec(dllexport) bool getgraphicsadapter(int32_t adapterindex, graphicsadapterdesc& outadapterdesc) { outadapterdesc = render_component.getgraphicsadapter(adapterindex); return true; }
and, following extern
method in c# app:
[dllimport(interoputils.runtime_dll, entrypoint = "getgraphicsadapter", callingconvention = callingconvention.cdecl)] internal static extern bool _getgraphicsadapter(int adapterindex, out graphicsadapterdesc adapterdesc);
however, doesn't work right when call it. different result depending on whether or not i'm compiling in x64 or x86 mode (both c++ dll , c# app compiled x86 or x64):
- in x86 mode, call returns, struct has 'nonsense' values in, , strings null,
- in x64 mode, call throws nullpointerexception.
my expectation i'm doing wrong marshalling strings, , need specify 'wide-mode' characters, don't know how (or if that's right option).
thank in advance.
c++ types not compatible c# unless they're wrapped in managed c++. , you're using std::wstring
cannot marshaled .net.
to interop you'll either need use wchar_t[]
or whar_t*
, tell c# marshal it.
Comments
Post a Comment