Note that there are some explanatory texts on larger screens.

plurals
  1. POUnmanaged struct as return value to managed
    text
    copied!<p>I have a unmanaged third party library (C). I'm trying to write a wrapper in C++/CLI, so I can use the library from C#. I have several C examples, but I can't figure out how to bridge the unmanaged and managed heaps.</p> <p>One of the functions in the library returns a struct, which I need to keep in my managed wrapper. This struct is then used as a parameter for other functions.</p> <p>Working C-example:</p> <p>library.h</p> <pre><code>typedef struct FOO_BAR_STRUCT* FOO_BAR; DLLEXPORT FOO_BAR FooBarCreate(); </code></pre> <p>Example.c</p> <pre><code>#include &lt;library.h&gt; int main(int argc, char* argv[]) { char* address = "127.0.0.1"; FOO_BAR fb = NULL; fb = FooBarCreate(); FooBarRegister(fb, address); } </code></pre> <p>So in my Wrapper, I try to recreate what the example do. I've figured out that the problem is which heap the struct is on, but I haven't been able to figure out how to solve this.</p> <p>My C++ code, compiled as a C++/CLI .dll for use in my C# project.</p> <p>FooBarComm.h</p> <pre><code>#include &lt;library.h&gt; ref class FooBarComm { public: FooBarComm(char* address); ~FooBarComm(); private: FOO_BAR fb; }; </code></pre> <p>FooBarComm.cpp</p> <pre><code>#include "FooBarComm.h" FooBarComm::FooBarComm(char* address) { fb = FooBarCreate(); FooBarRegister(fb, address); } FooBarComm::~FooBarComm() { } </code></pre> <p>And this fails. How can I get the instance of the FOO_BAR struct from the unmanaged code into the managed class, then use it as a argument in later functions.</p> <p>Edit:</p> <blockquote> <p>I fails with a warning LNK4248: unresolved typeref token (0100000D)<br /> error LNK2028: unresolved token (0A00000A) "extern "C" struct FOO_BAR_STRUCT<br /> error LNK2019: unresolved external symbol "extern "C" struct FOO_BAR_STRUCT</p> </blockquote> <p>I guess the problem is that I don't have a definition of the FOO_BAR_STRUCT in any header files that came with the library. I'm starting to dislike this library.</p> <p>Is it sensible to create a unmanaged class that holds the reference to the struct, and then reference this unmanaged class from a managed class?</p> <p>If I change to a "normal" C++ class, I get a different compile error:</p> <p>FooBarComm.h</p> <pre><code>#include &lt;library.h&gt; #pragma unmanaged class FooBarComm { ... </code></pre> <p>I get the compile error:</p> <blockquote> <p>error LNK2019: unresolved external symbol _FooBarCreate referenced</p> </blockquote> <p>Edit 2: </p> <p>Ofcourse it was a missing link to the .lib file.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload