2

I have an application (CLI) that references a mixed-DLL. The DLL implements a "ref" class with static functions.

Here is the (partial) code for the ref-class

public ref class AAA
{
public:
static bool Write(System::String^ sz);

// Not accessible!!!
public: static BOOL TraceRect(const CRect& rc);
};

Within the EXE, in C++ code, I'm trying to call both functions:

// This works
AAA::Write("hello");

// This doesn't !!!
CRect rc(0, 0, 12, 234);
AAA::TraceRect(rc);

How can I access the second function?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Seb
  • 143
  • 2
  • 10
  • 1
    This doesn't look like C++ to me. – John Dibling Apr 11 '11 at 15:12
  • 1
    Added `c++-cli` tag, as this will help you more. – Xeo Apr 11 '11 at 15:14
  • Just a wild guess, but you've put unmanaged data types on a static method for a managed class. In addition, 'const' is not supported by the CLR, so I'm not sure how it would interpret it when compiling the class to IL. I would split your public unmanaged static methods into a separate location rather than trying to put them in a managed class. – Dan Bryant Apr 11 '11 at 15:18
  • Define "doesn't work". Compile error? Runtime error? Please show the error message, and indicate on which line of code it occurred. – Ben Voigt Apr 11 '11 at 15:38
  • It's not really C++, but this code is in "OnInitDialog" C3767 – Seb Apr 11 '11 at 15:41
  • (didn't mean to 'add comment' just yet)... The compilation error at the application level is C3767. It complains that the function is inaccessible. – Seb Apr 11 '11 at 15:42
  • UPDATE: I removed the 'const'... and I'm still getting the same error at line : AAA:TraceRect(rc); – Seb Apr 11 '11 at 15:44
  • 1
    @Seb; The code making the call is compiled with `/clr`, right? And not with `#pragma unmanaged` (or equivalent)? I think you're running into identity problems, since native classes have to be compiled to MSIL typerefs in order to be used in methods of managed types, but `CRect` in the DLL is a different type from `CRect` in the EXE. – Ben Voigt Apr 11 '11 at 15:50
  • possible duplicate of [C++ CLI candidate function(s) not accessible](http://stackoverflow.com/questions/947213/c-cli-candidate-functions-not-accessible) – Ben Voigt Apr 11 '11 at 15:51
  • Yes, I'm using /clr... without "pragma unmanaged"... I'm just trying to make a bridge class that receives native types, is that possible? – Seb Apr 11 '11 at 16:17

1 Answers1

4

It's probably because native types (in this case, CRect) are treated as private by default. So, while the method is accessible, the parameter type for rc is not accessible. You can make it accessible by using make_public: http://msdn.microsoft.com/en-us/library/ms235607.aspx

Search for C3767 and make_public and you'll find plenty of other info on the topic.

Matt Smith
  • 17,026
  • 7
  • 53
  • 103
  • Good idea, but it doesn't work. I even tried changing 'CRect' to 'RECT'... now the DLL won't even link, I get a LNK2022 (metadata). – Seb Apr 11 '11 at 16:01
  • Try and pare it down to what is causing the issue. Do you get the issue if it takes *no* parameters. Changing between CRect and RECT is not really trying anything new--they're both still native types. – Matt Smith Apr 11 '11 at 16:32
  • @Seb: `#pragma make_public (CRect)` should work. If CRect belongs to a namespace, you should write it down also, like `NAMESPACE::CRect` – ali_bahoo Apr 12 '11 at 13:35
  • +1 for `make_public`. Sometimes I forget about it and go crazy. – ali_bahoo Apr 12 '11 at 13:38
  • Okay... I was using MS-Dev 2005... Tried this on MS-Dev 2010 and found everything worked fine! When I reopened my workspace in MS-Dev 2005... it worked! So I guess the solution was : unload & reload the project ;) Thanks to everyone who tried to help, it was much appreciated! – Seb Apr 12 '11 at 14:49