Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2006

Tip: Looking for answers? Try searching our database.

Using a C# ClassLib from a VC++ 6 application

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Komischke - 12 Sep 2006 09:29 GMT
Hi,

I tried the following example from this newsgroup (found it via goolge)
http://groups.google.com/group/microsoft.public.dotnet.framework.interop/browse_
thread/thread/f86c9af7b8fdac37/b9153307ba483255?lnk=st&q=&rnum=3&hl=en#b9153307b
a483255


C# Class library:
--------------------------
using System;
using System.Runtime.InteropServices;

namespace ClassLibrary2
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Class1
    {
        public Class1()
        {
        }

        public static void SayHello()
        {
            Console.WriteLine("C# says hello!");
        }
    }
}

1. I compiled the C# project
2. I called "regasm /tlb:ClassLibrary2.tlb ClassLibrary2.dll" in the C#
projects bin-directory -> successfull registered

VC++ 6.0 Win32-Console-application
-----------------------------------------------
#include <windows.h>
#import "mscorlib.tlb" raw_interfaces_only

using namespace mscorlib;
#import "D:\Workbench\ClassLibrary2\bin\Debug\ClassLibrary2.tlb"
using namespace ClassLibrary2;

int main()
{
    CoInitialize(0);

    _Class1Ptr class1(__uuidof(Class1));

    class1->SayHello();

    CoUninitialize();
    return 0;
}

When I try to compile the unmanaged Code I get the following error:
fatal error C1010: Unexpected end of file while searching for directive
of precompiled header
(this was translated from my german IDE)

I searched a ClassLibrary.tlh was created in the output folder, do I
have to include it in my console application?
Can somebody please be so kind to explain what am I doing wrong?

Thanks
Martin
Brian M - 13 Sep 2006 21:01 GMT
If using .NET 2.0, try this:

///////C# project is compiled as TestCSharpCOM.dll:
///////
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TestCSharpCOM
{
   [Guid("FE896CA2-3EBB-45c0-A9F3-595D1E81A41E")]
   public interface ICSharpCOM
   {
       [DispId(1)]
       void SayHello();
   };

   [Guid("C4C41C12-6725-4f7a-91B4-014E9DABFC43"),
    ClassInterface(ClassInterfaceType.None)]
   public class CSharpCOM : ICSharpCOM
   {
       public CSharpCOM()
       {
       }

       public void SayHello()
       {
           MessageBox.Show("Hello From CSharp");
       }
   };
}

run regasm on above dll, then:

VC++ 6 project:

// TestConsole.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#import "TestCSharpCOM.tlb" no_namespace raw_dispinterfaces

int main(int argc, char* argv[])
{
CoInitialize(NULL);
ICSharpCOMPtr p = NULL;
p.CreateInstance(__uuidof(CSharpCOM));
p->SayHello();
CoUninitialize();
return 0;
}

Good luck
Brian M
Keller TX
matt.m.jacobsen@gmail.com - 10 Nov 2006 09:00 GMT
Hi,

Erm, Which Stdafx.h? Are you building this yourself?

Cheers,

Matt

Brian M schrieb:

> If using .NET 2.0, try this:
>
[quoted text clipped - 51 lines]
> Brian M
> Keller TX
matt.m.jacobsen@gmail.com - 10 Nov 2006 09:00 GMT
Hi,

Erm, Which Stdafx.h? Are you building this yourself?

Cheers,

Matt

Brian M schrieb:

> If using .NET 2.0, try this:
>
[quoted text clipped - 51 lines]
> Brian M
> Keller TX

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.