• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

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
0
Mydeen Yussouf
Asked:
Mydeen Yussouf
  • 5
  • 4
  • 2
1 Solution
 
Easwaran ParamasivamCommented:
0
 
Mydeen YussoufTechnology GeneralistAuthor Commented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mydeen YussoufTechnology GeneralistAuthor Commented:
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

0
 
Mydeen YussoufTechnology GeneralistAuthor Commented:
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.
0
 
sarabandeCommented:
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
0
 
Mydeen YussoufTechnology GeneralistAuthor Commented:
@Sarabande, could you please point me to a working IJW example where a c# function is called from c++ code? thanks a bunch...
0
 
sarabandeCommented:
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
0
 
sarabandeCommented:
i have found the following

http://stackoverflow.com/questions/1068762/calling-c-sharp-from-c-reverse-p-invoke-mixed-mode-dlls-and-c-cli

which is exactly the same you want to do. i hope you can get t he necessary code from that.

by the way i searched for "call C# DLL's from a mixed mode DLL" to get the above.

Sara
0
 
Mydeen YussoufTechnology GeneralistAuthor Commented:
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!!!
0
 
sarabandeCommented:
thanks, i am happy if i could help.

Sara
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now