CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    late binding, InvokeMember managed C++

    Hi I am trying to load a c# dll at runtime and call a method in it from managed C++, I am not having much luck

    I am trying t use

    Assembly::LoadFrom and
    System::Type^ typ
    typ->InvokeMember

    methods.

    anyone have sample code in managed C++.

    Urgent. Please and thank you.

    Indika
    and Canada rocks!! Peace bro.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: late binding, InvokeMember managed C++

    [ moved thread ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: late binding, InvokeMember managed C++

    This works for me (using VC++ 2010 Express):

    Code:
    // TestDLL.h
    
    #pragma once
    
    using namespace System;
    
    namespace TestDLL {
    
    public ref class Class1
    {
    public:
      static void SayHello();
    };
    
    }
    Code:
    // TestDLL.cpp
    
    #include "stdafx.h"
    
    #include "TestDLL.h"
    
    using namespace TestDLL;
    
    void Class1::SayHello()
    {
      Console::WriteLine("Hello from the DLL!");
    }
    Code:
    // MCppDllTest.cpp: Hauptprojektdatei.
    
    #include "stdafx.h"
    
    using namespace System;
    using namespace System::Reflection;
    
    int main(array<System::String ^> ^args)
    {
    #ifdef _DEBUG
      String ^strDllName = "..\\Debug\\TestDLL.dll";
    #else
      String ^strDllName = "..\\Release\\TestDLL.dll";
    #endif
      Console::WriteLine("Hello World from the main app");
      Assembly ^ably = Assembly::LoadFrom(strDllName);
      Type ^cls = ably->GetType("TestDLL.Class1");
      cls->InvokeMember("SayHello", BindingFlags::Static | BindingFlags::Public | BindingFlags::InvokeMethod,
        nullptr, nullptr, nullptr);
      Console::WriteLine("Back in the main app");
      Console::ReadLine();
      return 0;
    }
    Does this model your scenario, more or less? Ok, the DLL is C++/CLI here because I don't have C# installed, but I suspect the invoking part to be the problematic one anyway. If your scenario differs significantly in any aspect (except the C#), please let me know. And a more detailed description of the problem than "I am not having much luck" would have been helpful, of course.

    It took me some time to put that together, including lots of debugging. But now this one works. I'm relatively sure, however, that I had it this way before and it did not work. But never mind, now...
    Last edited by Eri523; January 6th, 2011 at 02:14 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    Re: late binding, InvokeMember managed C++

    Thanks... yes that will do the trick. I had already fixed with similar code as yours.
    and Canada rocks!! Peace bro.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured