Link to home
Start Free TrialLog in
Avatar of ol muser
ol muserFlag for United States of America

asked on

Use C# code in VC++ app

Hi, I have a C++ application that needs to use a class and its function available in a non-GUI class library written in C# (.Net 3.5) and made available in a DLL. How best can I make use of the C# class library? Please explain steps. thanks. OS used: Win XP. Environment: Visual Studio 2008
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Avatar of ol muser

ASKER

Thanks EawaranP, your first comment has some potential. I will give it a shot. The second comment talks about a Ogre project which I am not involved in. Also, if I haven't made it clear I need to be able to call my C# class library from unmanaged C++ code.
HI, I am able to follow all steps in the first section. However, in the 2nd section, steps  1 through 5 produce a .tlh file but being new to COM I am not sure if it has produced the proper structures. I am attaching it for reference. What am I missing here? thanks!
// Created by Microsoft (R) C/C++ Compiler Version 15.00.30729.01 (deae56a4).
//
// c:\transplant_2008\source\shell\transplant\debug\com.emailer.tlh
//
// C++ source equivalent of Win32 type library c:\Program Files\Transplant\com.Emailer.tlb
// compiler-generated file created 11/01/11 at 15:21:18 - DO NOT EDIT!

#pragma once
#pragma pack(push, 8)

#include <comdef.h>

namespace EMailer {

//
// Forward references and typedefs
//

struct __declspec(uuid("0eb6c8d0-06c0-4f84-9f9e-7810ace6ce0a"))
/* LIBID */ __EMailer;
struct /* coclass */ Communicator;

//
// Type library items
//

struct __declspec(uuid("637c504b-7329-47c3-a605-bdcb109b361a"))
Communicator;
    // [ default ] interface _Object

//
// Named GUID constants initializations
//

extern "C" const GUID __declspec(selectany) LIBID_EMailer =
    {0x0eb6c8d0,0x06c0,0x4f84,{0x9f,0x9e,0x78,0x10,0xac,0xe6,0xce,0x0a}};
extern "C" const GUID __declspec(selectany) CLSID_Communicator =
    {0x637c504b,0x7329,0x47c3,{0xa6,0x05,0xbd,0xcb,0x10,0x9b,0x36,0x1a}};

} // namespace EMailer

#pragma pack(pop)

Open in new window

Made some more changes to the C# code and the type library generated looks differetn now. But still it does not have the method I am trying to expose from the Interface.

Below is my interface.

[assembly: System.Runtime.InteropServices.ComVisible(true)]
namespace EMailer
{
    // Interface declaration.
    [Guid("75DEC494-2758-4d8f-B2A3-3AF875CDCF37")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ICommunicator
    {
        [DispId(1)]
        bool Send(string tolist, string subject, string body);
    }

Open in new window


But below is the type library header generated:

struct __declspec(uuid("75dec494-2758-4d8f-b2a3-3af875cdcf37"))
ICommunicator : IDispatch
{};

Open in new window


It does not have the Send method I am trying to access from the C++ client.
you probably could use a mixed-code assembly (dll) between (unmanaged) c++ and c#. you may get more information by searching for IJW (it just works) samples. the dll would be a c++ wrapper between your app and the c# library.

Sara
@Sarabande, could you please point me to a working IJW example where a c# function is called from c++ code? thanks a bunch...
i can try though my weak point is the c# . i have no idea what i need to do to call a function of a c# assembly from managed c++ though that probably is an easy job.

Sara
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
With some work the pointers in the article gave me my working solution. @Sarabande, thanks for the timely response and consistent follow through, appreciate it!!!
thanks, i am happy if i could help.

Sara