Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VC++6.0 / C# Interoperability

116 views
Skip to first unread message

Masi

unread,
May 25, 2005, 8:47:51 AM5/25/05
to
My problem is, that I need to call C# objects from Legacy VC++6.0 code.
Porting to VC7.0 is not an option for now.

For example I have C# class, and I need to call its Test() method.

Any links or better If someone has example of how this is done.

Thanks in advance
-Markku-

C#
=====
public class IopTest
{
public int Test()
{
}
}


VC++
===========
...
....
int main()
{
//Call to IopTest::Test() method????
return 0;
}


William DePalo [MVP VC++]

unread,
May 25, 2005, 10:15:03 AM5/25/05
to
"Masi" <ma...@lentolaivue69.com> wrote in message
news:OSFO4fSY...@TK2MSFTNGP09.phx.gbl...

> My problem is, that I need to call C# objects from Legacy VC++6.0 code.
> Porting to VC7.0 is not an option for now.
>
> For example I have C# class, and I need to call its Test() method.
>
> Any links or better If someone has example of how this is done.

One thing you can do is to expose the C# class to managed callers as a COM
object. That done, you can take advantage of the C++ compiler's type library
import facility to use the COM object as a C++ class in VC++. If you do that
it is as easy as 1-2-3:

1) With VS.Net 2003 I created this toy class in C#

using System;
using System.Runtime.InteropServices限;

namespace ClassLibrary1
{
[ClassInterface(ClassInterface限Type.AutoDual)]
public class Class1
{
public Class1()
{
}

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

Note the attribute ClassInterfaceType.AutoDual.

2) Then I registered the assembly and created a type library from it with
the
command:

regasm /tlb:ClassLibrary1Lib.tlb ClassLibrary1.dll

3) With VC++ 6.0 I created this toy application which uses the compiler's
type library import facility to read the type library and create a COM
wrapper to be used by unmanaged callers:

#include <windows.h>
#import "mscorlib.tlb" raw_interfaces_only

using namespace mscorlib;
#import "ClassLibrary1Lib.tlb"
using namespace ClassLibrary1;

int main()
{
CoInitialize(0);

_Class1Ptr class1(__uuidof(Class1));

class1->SayHello();

CoUninitialize();
return 0;
}

Regards,
Will


William DePalo [MVP VC++]

unread,
May 25, 2005, 12:25:16 PM5/25/05
to
"William DePalo [MVP VC++]" <willd....@mvps.org> wrote in message
news:u3cPXQTY...@TK2MSFTNGP14.phx.gbl...

> One thing you can do is to expose the C# class to managed callers as a COM
> object.

Oops, that's a typo. Change "managed" to "unmanaged".

Regards,
Will


Masi

unread,
May 26, 2005, 12:35:24 AM5/26/05
to
Thanks.
I have also heard that it is possible to call delegate functions somehow
from old VC++ code.
In my case this could also be one solution (since we just need to update
very simple thing between C# app and legacy app).

Found this one.
http://www.dotnet247.com/247reference/msgs/55/276613.aspx

Can anyone verify if this is legal or not, we tried very simple delegate
( no parameters) and it worked. So is it legal to pass
delegate as an function pointer to C-Dll. And then call that function
through its pointer?

Thanks in advance and greetings from Finland
-Markku-

"William DePalo [MVP VC++]" <willd....@mvps.org> wrote in message

news:%23VteYZU...@tk2msftngp13.phx.gbl...

William DePalo [MVP VC++]

unread,
May 26, 2005, 10:44:32 AM5/26/05
to
"Masi" <ma...@lentolaivue69.com> wrote in message
news:e1ONZxaY...@TK2MSFTNGP09.phx.gbl...
> Thanks.

You are welcome.

> I have also heard that it is possible to call delegate functions somehow
> from old VC++ code.
> In my case this could also be one solution (since we just need to update
> very simple thing between C# app and legacy app).

Sorry, but this is not something that I have investigated so I don't have a
sample to share.

Regards,
Will


Willy Denoyette [MVP]

unread,
May 26, 2005, 4:55:29 PM5/26/05
to

"Masi" <ma...@lentolaivue69.com> wrote in message
news:e1ONZxaY...@TK2MSFTNGP09.phx.gbl...

> Thanks.
> I have also heard that it is possible to call delegate functions somehow
> from old VC++ code.
> In my case this could also be one solution (since we just need to update
> very simple thing between C# app and legacy app).
>
> Found this one.
> http://www.dotnet247.com/247reference/msgs/55/276613.aspx
>
> Can anyone verify if this is legal or not, we tried very simple delegate
> ( no parameters) and it worked. So is it legal to pass
> delegate as an function pointer to C-Dll. And then call that function
> through its pointer?
>
> Thanks in advance and greetings from Finland
> -Markku-
>

But this is not the same thing, you asked if it was possible to call C#
methods from C/C++, what's described in the article is how to call back into
C#.
Anyway if it fits your needs, it's perfectly legal and it's THE way to
set-up callbacks, but remember they are callbacks!

Willy.


Masi

unread,
May 27, 2005, 1:08:55 AM5/27/05
to
Thanks for response and
sorry for my bad english, it leads to misunderstandings:)

Callback is the way for us to do things.

1. What is the calling convention delegates uses?
__stdcall or something else?

2. How to pass function with parameters to C-DLL and call that delegate with
parameters. In our case it is identification code (13 chars)
- Example of this would be nice?

What I need is something like this.

C#
====
class IopTest
{
.....
delegate void UpdateInfo(string Id) //this is delegate to be passed to
C, string is max 13 characters

[DllImport("Iop.dll")]
private static extern void SetCallBack(UpdateInfo pFn)

void SetDelegate()
{
UpdateInfo pFn = new UpdateInfo(SetInfo)
SetCallBack(pFn); // This call is directed to C-Dll
}

public void SetInfo(string id)
{
....//Do stuff
}
....
}

C-Dll
====
extern "C" __stdcall void SetCallBack( void (*delegateFn) (char* pszId))
{
delegateFn("1234567890123");
}

Thanks in advance
-Markku-

"Willy Denoyette [MVP]" <willy.d...@telenet.be> wrote in message
news:ucv3BVjY...@TK2MSFTNGP15.phx.gbl...

0 new messages