Archives

Anticipation

  • No dates present

Hidden natives

Continuing on the theme of mixing managed and native code from my last programming post, here’s another issue I ran into at about the same time…

Normally, you’re writing managed code as an interop layer to hide the native code away from the managed world, so it’s only used with private visibility. Sometimes, however, you need to make it publically accessible. The specific case that I ran into involved writing unit tests (in a different assembly) — they needed to be able to construct the underlying native data and pass it in to the managed class in order to test its behaviour properly.

You might think that you just need to make the method public, like so:

#include "Native.h"
 
public ref class Managed
{
public:
  Managed();
 
  void DoNative(const Native& native);
};

Unfortunately, it’s not quite as simple as that :) Trying to use the function above from a different assembly yields the following somewhat obscure error:

error C3767: 'Managed::DoNative': candidate function(s) not accessible

At the time this happened to me, the MSDN page for this error did not contain anything particularly useful — it was just talking about friend functions and namespaces (ie. only pure native code). Once I discovered the problem I added a Community note, and it’s interesting to see that it has since been updated to point to some more useful documentation — so yay, the system works! 😉

The specific documentation in question is this one; it relates to a change in VC++ 2005 that made native types in managed assemblies private by default. Which is fair enough, since as I said above for the most part native types are shameful implementation details that you want to hide away 😉

Modifying the code to the following lets everything work again:

#include "Native.h"
 
#pragma make_public(Native)
 
public ref class Managed
{
public:
  Managed();
 
  void DoNative(const Native& native);
};

(Apparently there is a warning [#4692] that would have made this more obvious, but for some reason it’s switched off by default.)

1 comment to Hidden natives


  • Catchable fatal error: Object of class WP_Comment could not be converted to string in /home/uecasm/public_html/lambert/wpdata/wp-content/themes/atahualpa/functions/bfa_custom_comments.php on line 16