<p>I have an old C library with a function that takes a void**:</p>

<pre><code>oldFunction(void** pStuff);
</code></pre>

<p>I'm trying to call this function from managed C++ (m_pStuff is a member of the parent ref class of type void*):</p>

<pre><code>oldFunction( static_cast&lt;sqlite3**&gt;(  &amp;m_pStuff ) );
</code></pre>

<p>This gives me the following error from Visual Studio:</p>

<blockquote>
  <p>error C2440: 'static_cast' : cannot convert from 'cli::interior_ptr' to 'void **'</p>
</blockquote>

<p>I'm guessing the compiler is converting the void* member pointer to a cli::interior_ptr behind my back.</p>

<p>Any advice on how to do this?</p>

[{'answer_id': 27326, 'author': 'Ben Childs', 'author_id': 2925, 'author_profile': 'https://Stackoverflow.com/users/2925', 'pm_score': 2, 'selected': True, 'text': '<p>EDIT: Fixed answer, see below.</p>\n\n<p>Really you need to know what oldFunction is going to be doing with pStuff. If pStuff is a pointer to some unmanaged data you can try wrapping the definition  of m_pStuff with:</p>\n\n<pre><code>#pragma unmanaged\n\nvoid* m_pStuff\n\n#pragma managed\n</code></pre>\n\n<p>This will make the pointer unmanaged which can then be passed into unmanaged functions. Of course you will not be able to assign any managed objects to this pointer directly. </p>\n\n<p>Fundamentally unmanaged and managed pointers are not the same and can\'t be converted without some sort of glue code that copies the underlying data. Basically managed pointers point to the managed heap and since this is garbage collected the actual memory address they point to can change over time. Unmanaged pointers do not change the memory address without you explicitly doing so. </p>\n\n<p>Scratch that, you can\'t define unmanaged / managed inside a class definition. But this test code seems to work just fine:</p>\n\n<pre><code>// TestSol.cpp : main project file.\n\n#include "stdafx.h"\n\nusing namespace System;\n\n#pragma unmanaged\n\nvoid oldFunction(void** pStuff)\n{\n    return;\n}\n\n#pragma managed\n\nref class Test\n{\npublic:\n    void* m_test;\n\n};\n\nint main(array&lt;System::String ^&gt; ^args)\n{\n    Console::WriteLine(L"Hello World");\n\n    Test^ test = gcnew Test();\n    void* pStuff = test-&gt;m_test;\n    oldFunction(&amp;pStuff);\n    test-&gt;m_test = pStuff;\n\n    return 0;\n}\n</code></pre>\n\n<p>Here I copy the pointer out of the managed object first and then pass that in by to the oldFunction. Then I copy the result (probably updated by oldFunction) back into the managed object. Since the managed object is on the managed heap, the compiler won\'t let you pass a reference to the pointer contained in that object as it may move when the garbage collector runs.</p>\n'}, {'answer_id': 28695, 'author': 'Danielb', 'author_id': 39040, 'author_profile': 'https://Stackoverflow.com/users/39040', 'pm_score': 0, 'selected': False, 'text': '<p>Thanks for the advice, the pointer is to an C style abstract structure which I think if I leave that structure exposed to the managed code is going to cause further pain due to its lack of defined structure.  So what I think I will do is wrap the C library in C++ and then wrap the C++ wrapper with managed C++, which will prevent exposing those C structures to managed code.</p>\n'}]
['https://Stackoverflow.com/questions/27071', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/39040/']