DumbQuesionGuy314
DumbQuesionGuy314

Reputation: 173

Call a static C++/CLI method from C++

In a C++CLI project I make a call to a native object on an event, I'd like to be able to call a C++/CLI function from the native C++ when this event is triggered. I have following code at the moment, but it returns the error that PickObjects() is not a member of ManagedClass. Is calling back to a static C++CLI method possible?

#pragma once
#include "Stdafx.h"
#include "ManagedClass.h"

namespace Unmanaged
{
    public class EventHandlers
    {
    public:
        static void OnClick(customObject* caller, void *calldata)
        {
            //call managed method, can get here from CLI
            ManagedClass::ManagedObject::PickObjects();
        }
    };
}

Here is the C++CLI code snippet, everything here appears to run fine:

namespace ManagedClass
{
    public ref class ManagedObject  
    {
    public:
         static void PickObjects()
         {
         //pick stuff when called
         }
    };
}

EDIT: Got it working, the error was definitely related to how Visual Studio compiled the files. Will update with solution momentarily. Thanks to Matthias for the help.

Upvotes: 2

Views: 2330

Answers (2)

DumbQuesionGuy314
DumbQuesionGuy314

Reputation: 173

It seems as though the classes were not getting compiled in the correct order. Adding the EventHandlers class to the end of ManagedClass got things working.

namespace ManagedClass
{
    public ref class ManagedObject  
    {
    public:
         static void PickObjects()
         {
         //pick stuff when called
         }
    };
    public class EventHandlers
    {
    public:
        static void OnClick(customObject* caller, void *calldata)
        {
            ManagedClass::ManagedObject::PickObjects();
        }
    };
}

Upvotes: 2

Matthias
Matthias

Reputation: 11

Yes, calling a static method out of another static method is possible. In this case it sounds like you haven't declared ManagedClass::PickObjects() properly. You may show us the code (header should be enough) of it.

Upvotes: 1

Related Questions