Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Call c# dll from c++

Posted on 2006-05-06
4
Medium Priority
?
14,260 Views
Last Modified: 2008-01-09
Dear Experts!

I have to call c# dll from c++.
I used MSDN Tutorial: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkCOMInteropPart2CServerTutorial.asp

c# dll code
// CSharpServer.cs
// compile with: /target:library
// post-build command: regasm CSharpServer.dll /tlb:CSharpServer.tlb

using System;
using System.Runtime.InteropServices;
namespace CSharpServer
{
   // Since the .NET Framework interface and coclass have to behave as
   // COM objects, we have to give them guids.
   [Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]
   public interface IManagedInterface
   {
      int PrintHi(string name);
   }

   [Guid("C6659361-1625-4746-931C-36014B146679")]
   public class InterfaceImplementation : IManagedInterface
   {
      public int PrintHi(string name)
      {
         Console.WriteLine("Hello, {0}!", name);
         return 33;
      }
   }
}

No problems to this point.

Next Step

In .NET C++ I Created  Project and use this code
(copied CSharpServer.tlb, mscorlib.tlb to project directory)

#include <windows.h>
#include <stdio.h>

#pragma warning (disable: 4278)

// To use managed-code servers like the C# server,
// we have to import the common language runtime:
#import <mscorlib.tlb> raw_interfaces_only

// For simplicity, we ignore the server namespace and use named guids:
#if defined (USINGPROJECTSYSTEM)
#import "..\RegisterCSharpServerAndExportTLB\CSharpServer.tlb" no_namespace named_guids
#else  // Compiling from the command line, all files in the same directory
#import "CSharpServer.tlb" no_namespace named_guids
#endif
int main(int argc, char* argv[])
{
   IManagedInterface *cpi = NULL;
   int retval = 1;

   // Initialize COM and create an instance of the InterfaceImplementation class:
   CoInitialize(NULL);
   HRESULT hr = CoCreateInstance(CLSID_InterfaceImplementation,
               NULL, CLSCTX_INPROC_SERVER,
               IID_IManagedInterface, reinterpret_cast<void**>(&cpi));

   if (FAILED(hr))
   {
      printf("Couldn't create the instance!... 0x%x\n", hr);
   }
   else
   {
      if (argc > 1)
      {
         printf("Calling function.\n");
         fflush(stdout);
         // The variable cpi now holds an interface pointer
         // to the managed interface.
         // If you are on an OS that uses ASCII characters at the
         // command prompt, notice that the ASCII characters are
         // automatically marshaled to Unicode for the C# code.
         if (cpi->PrintHi(argv[1]) == 33)
            retval = 0;
         printf("Returned from function.\n");
      }
      else
         printf ("Usage:  COMClient <name>\n");
      cpi->Release();
      cpi = NULL;
   }

   // Be a good citizen and clean up COM:
   CoUninitialize();
   return retval;
}

Compilation Errors:

Warning      43      warning C4279: 'value': identifier in type library 'mscorlib.tlb' is a keyword; use the 'rename' qualifier      c:\Documents and Settings\Develop\My Documents\Visual Studio 2005\Projects\COMClient\COMClient\COMClient.cpp      11      

Error      51      error C2065: 'IManagedInterface' : undeclared identifier      c:\Documents and Settings\Develop\My Documents\Visual Studio 2005\Projects\COMClient\COMClient\COMClient.cpp      25      

Error      53      error C2065: 'CLSID_InterfaceImplementation' : undeclared identifier      c:\Documents and Settings\Develop\My Documents\Visual Studio 2005\Projects\COMClient\COMClient\COMClient.cpp      30      

I am a DBA and my area of expertise is SQL not c#/c++.

If possible give me step by step instructions.

Please Help


0
Comment
Question by:mwolph
2 Comments
 
LVL 2

Accepted Solution

by:
SChertkov earned 1000 total points
ID: 16624597
This error occur because the typelib CSharpServer.tlb is not contain IManagedInterface
and InterfaceImplementation definitions.
(You can see this with OLE/COM object viewer)
All managed objects using in COM interop
must be compiled with attribute ComVisible:

using System;
using System.Runtime.InteropServices;
namespace CSharpServer
{
    // Since the .NET Framework interface and coclass have to behave as
    // COM objects, we have to give them guids.
    [ComVisible(true)]
    [Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]
    public interface IManagedInterface
    {
        int PrintHi(string name);
    }

    [ComVisible(true)]
    [Guid("C6659361-1625-4746-931C-36014B146679")]
    public class InterfaceImplementation : IManagedInterface
    {
        public int PrintHi(string name)
        {
            Console.WriteLine("Hello, {0}!", name);
            return 33;
        }
    }
}
0
 

Expert Comment

by:bhoyeto
ID: 20117844
hI,

Tried the solution and compilation was smooth. However, when I tried running the MFC app, I get the "Couldn't create the instance". Any advise for this problem?

Regards.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Introduction to Processes
Starting up a Project

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question