interop delegate for COM

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Eric (wangshaoquan_at_msn.com)
Date: 12/01/04


Date: Wed, 1 Dec 2004 17:04:04 +0800

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



Relevant Pages

  • Re: I really hate .NET especially inside Delphi
    ... in Delphi.NET terms this is a package (yeah, yeah, I don't know why ... to a BPL it will compile to a dll, ... will see a namespace called TimsTest with TTestClass in it, ... in a Delphi app in the Uses class you would put ...
    (borland.public.delphi.non-technical)
  • Re: custom namespaces
    ... and for which I think I must need to compile ... it so that the code behind can actually see/find reference to the custom ... I'm trying to create custom a namespace for use ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Weird bug in VS.NET 2003 IDE
    ... > From time to time, when I save my code, the IDE decides to add lines to ... > End Sub ... > End Namespace ... > the IDE adding all these lines is that the program fails to compile, ...
    (microsoft.public.vsnet.ide)
  • Re: How to pass information, classes between forms in Windows Application mode [ FINALLY!]
    ... declarations, it will compile. ... [that Namespace only gets rid of ambiguity in class names, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Creating a custom class/Namespace
    ... Namespace ByteArray ... I then compile it using: ... >>> How then would I reference this in a new Web Application ... > If you need to reference a .Net Framework assembly or assemblies, ...
    (microsoft.public.dotnet.framework.aspnet)