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 / Languages / Managed C++ / June 2007

Tip: Looking for answers? Try searching our database.

VC6 <-> C#(net 2.0) interaction

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Elmar - 13 Jun 2007 12:49 GMT
Hi there.

We maintain a big VC6 project that we currently can´t convert to VS2005 for
various reasons. For our current project we need to establish a communication
channel between this VC6 project and another project that is running on
VS2005 C# and NET 2.0.

All our efforts so far were unsuccessful. Especially triggering events from
C++ to NET doesn´t work. We would be very thankfull for a set of sample
projects that actually interact VC6 <-> C#. Any help would be very
appreciated!

Thanks a lot!
Jochen Kalmbach [MVP] - 13 Jun 2007 13:09 GMT
Hi Elmar!

> We maintain a big VC6 project that we currently can´t convert to VS2005 for
> various reasons. For our current project we need to establish a communication
[quoted text clipped - 5 lines]
> projects that actually interact VC6 <-> C#. Any help would be very
> appreciated!

What is this VC6 project??? DLL/LIB/EXE/(In/Out)COM-Server ?

Greetings
  Jochen
Elmar - 13 Jun 2007 14:22 GMT
Hi Jochen.

It is an EXE-File.

Thanks.
Elmar

> What is this VC6 project??? DLL/LIB/EXE/(In/Out)COM-Server ?
>
> Greetings
>    Jochen
Bruno van Dooren - 13 Jun 2007 13:10 GMT
> We maintain a big VC6 project that we currently can´t convert to VS2005
> for
[quoted text clipped - 8 lines]
> projects that actually interact VC6 <-> C#. Any help would be very
> appreciated!

There are 2 ways:

1)build a plain C style DLL that communicates with your C++ app in whatever
way you see fit, and then consume that DLL in your .NET app.
.NET 2.0 has facilities for converting delegates to C style function
pointers so with some decent plumbing code, your should be able to make it
work.

2)build a .NET class library that communicates with your .NET app, and
register is for COM interop. then consume that COM class in your C++
application to work with the .NET application.

either way should work without too many problems.
Signature

Kind regards,
   Bruno van Dooren  MVP - VC++
   http://msmvps.com/blogs/vanDooren
   bruno_nos_pam_van_dooren@hotmail.com

Elmar - 13 Jun 2007 15:24 GMT
Can we discuss this a little bit more?
Assuming I have a simple test class library with one class:

------
using System;
using System.Collections.Generic;
using System.Text;

namespace vc6interop
{
   public class Interop
   {
       public static string GreetMe(string name)
       {
           return "Hallo " + name;
       }
   }
}
------

...that is compiled with "expose as COM" flag turned on.
What do I have to do in order to call this method in C++?

I found some code that would fit this sample but it gives me a lot of errors.

------
GreetMe function;
CString result;

HINSTANCE hinstLib = LoadLibrary("vc6interop.dll");

if (hinstLib) {

    function = (GreetMe) GetProcAddress(hinstLib, "GreetMe");

    if (function)
        result = (function) ("John");

    BOOL fFreeResult = FreeLibrary(hinstLib);
}

// Das Ergebnis anzeigen
if (!hinstLib  || !function)
    printf("Error\n");
else
    printf("Result: %s\n", result);
------

> 2)build a .NET class library that communicates with your .NET app, and
> register is for COM interop. then consume that COM class in your C++
> application to work with the .NET application.
>
> either way should work without too many problems.
Ben Voigt [C++ MVP] - 13 Jun 2007 18:08 GMT
> Can we discuss this a little bit more?
> Assuming I have a simple test class library with one class:
[quoted text clipped - 24 lines]
> ------
> GreetMe function;
Don't know what that means.

> CString result;
>
> HINSTANCE hinstLib = LoadLibrary("vc6interop.dll");
Nope, only .NET framework can load a .NET assembly.

> if (hinstLib) {
>
> function = (GreetMe) GetProcAddress(hinstLib, "GreetMe");
Nope, .NET code doesn't have an address until the JIT compiles it.

Instead, you must use CoInitialize to enable COM support in your
application, and CoCreateObject to create an instance of a .NET class.  Then
you will be able to call the methods.

> if (function)
> result = (function) ("John");
[quoted text clipped - 14 lines]
>>
>> either way should work without too many problems.

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.