hi,
I want to pass a function pointer to COM, so that COM can callback in some
situation.
/////////////////////////////////////
// C# compile into COM
using System;
using System.Runtime.InteropServices;
namespace Whatever
{
public delegate void dProc();
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("6DAFA2C2-BE89-4af3-92CF-A278E81D9C2D")]
public class MyClass
{
public bool SetHandler([MarshalAs(UnmanagedType.FunctionPtr)]dProc dlgt)
{
System.Console.WriteLine("in the COM\n");
return true;
}
}
}
////////////////////////////////////////
// C++ use the COM
#define _WIN32_WINNT 0x0501
#include <objbase.h>
#include <iostream>
#import "d:\project\lev3test\mscorlib.tlb" raw_interfaces_only
#import "d:\project\lev3test\ericbase.tlb" no_namespace named_guids
extern "C"
void __stdcall ProcData()
{
return;
}
int main ()
{
::CoInitialize(NULL);
try
{
_MyClassPtr pPtr(__uuidof(MyClass));
int dlgt = reinterpret_cast<int>(ProcData);
pPtr->SetHandler(dlgt);
}
catch(_com_error e)
{
std::cout << e.ErrorMessage() << std::endl;
}
::CoUninitialize();
getchar();
return 0;
}
// source end
/////////////////
two projects compile and link well. but when i run the program, i got an
exception at "pPtr->SetHandler(dlgt)" saying "invalid agument".
What's wrong here?
Thanks in advance.
Regards,
Eric
Mattias Sj?gren - 01 Dec 2004 15:32 GMT
The runtime currently doesn't support marshaling a native function
pointer to a managed delegate, only the other way around.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Eric - 06 Dec 2004 00:27 GMT
Thank you for your comment.
Regards,
Eric
> The runtime currently doesn't support marshaling a native function
> pointer to a managed delegate, only the other way around.
>
> Mattias
Howard Swope - 03 Dec 2004 16:06 GMT
You could setup your managed code to be COM callable and then pass an
interface pointer to your COM object. The COM object could then call back on
a functioned defined by the interface.
> hi,
>
[quoted text clipped - 63 lines]
> Regards,
> Eric
Eric - 06 Dec 2004 00:27 GMT
Would you please give me a sample or a link, shows me how to do this ?
Thanks.
Regards,
Eric
> You could setup your managed code to be COM callable and then pass an
> interface pointer to your COM object. The COM object could then call back on
[quoted text clipped - 68 lines]
> > Regards,
> > Eric