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;
}
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
Oops, that's a typo. Change "managed" to "unmanaged".
Regards,
Will
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...
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
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.
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...