Dispatch tables with functions containing managed pointers 
Author Message
 Dispatch tables with functions containing managed pointers

How do I construct a dispatch table containing function
pointers that point to members of unmanaged objects, or
are normal functions? It is a requirement that the
functions accept at least one argument that is a pointer
to a managed object.

The following program illustrates the problem:

#using <mscorlib.dll>
#include <tchar.h>

using namespace System;

__gc class Managed
{
public:
  int I;

Quote:
};

int Func(Managed *M)
{
  return M->I;

Quote:
}

class CallBack
{
public:
  int (*foo)(Managed *);

Quote:
} CB;

int _tmain(void)
{
  Managed *MP=new Managed();
  MP->I=2;
  CB.foo=Func;
  int J=CB.foo(MP);
  return 0;

Quote:
}

The program throws an exception on the line
containing "int J=CB.foo(MP);" complaining that it cannot
marshal parameter #1, because the type definition contains
no layout information.

The only soution I have found is to use delegates, but
this will require a massive set of changes to the
application because delegates must point to managed object
members only.

Thanks/Anker



Mon, 26 Jul 2004 03:52:11 GMT  
 Dispatch tables with functions containing managed pointers
I'm not 100% certain why this works: but if you change the definition of
Managed to:

using namespace System::Runtime::InteropServices;

[ StructLayout(Sequential) ]
__gc class Managed {
public:
   int I;

Quote:
};

then your code compiles and run correctly.

--
Jonathan Caves
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. ? 2002 Microsoft Corporation. All rights
reserved.

http://www.microsoft.com/info/cpyright.htm.

Quote:
> How do I construct a dispatch table containing function
> pointers that point to members of unmanaged objects, or
> are normal functions? It is a requirement that the
> functions accept at least one argument that is a pointer
> to a managed object.

> The following program illustrates the problem:

> #using <mscorlib.dll>
> #include <tchar.h>

> using namespace System;

> __gc class Managed
> {
> public:
>   int I;
> };

> int Func(Managed *M)
> {
>   return M->I;
> }

> class CallBack
> {
> public:
>   int (*foo)(Managed *);
> } CB;

> int _tmain(void)
> {
>   Managed *MP=new Managed();
>   MP->I=2;
>   CB.foo=Func;
>   int J=CB.foo(MP);
>   return 0;
> }

> The program throws an exception on the line
> containing "int J=CB.foo(MP);" complaining that it cannot
> marshal parameter #1, because the type definition contains
> no layout information.

> The only soution I have found is to use delegates, but
> this will require a massive set of changes to the
> application because delegates must point to managed object
> members only.

> Thanks/Anker



Mon, 26 Jul 2004 04:08:41 GMT  
 Dispatch tables with functions containing managed pointers
Anker,

I'm looking into this issue to see what your options may be. I'll be
following up on your other two posts in this thread. Thanks!

Derrick Glass
Microsoft Developer Support - Visual Studio

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Content-Class: urn:content-classes:message


| Subject: Dispatch tables with functions containing managed pointers
| Date: Wed, 6 Feb 2002 11:52:11 -0800
| Lines: 50

| MIME-Version: 1.0
| Content-Type: text/plain;
|       charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
| Thread-Index: AcGvR8RhTAQ4PfN6RuKEqY4obJigIg==
| Newsgroups: microsoft.public.dotnet.languages.vc
| NNTP-Posting-Host: TKMSFTNGXA08 10.201.226.36
| Path: cpmsftngxa09!cpmsftngxa07
| Xref: cpmsftngxa09 microsoft.public.dotnet.languages.vc:6656
| X-Tomcat-NG: microsoft.public.dotnet.languages.vc
|
| How do I construct a dispatch table containing function
| pointers that point to members of unmanaged objects, or
| are normal functions? It is a requirement that the
| functions accept at least one argument that is a pointer
| to a managed object.
|
| The following program illustrates the problem:
|
| #using <mscorlib.dll>
| #include <tchar.h>
|
| using namespace System;
|
| __gc class Managed
| {
| public:
|   int I;
| };
|
| int Func(Managed *M)
| {
|   return M->I;
| }
|
| class CallBack
| {
| public:
|   int (*foo)(Managed *);
| } CB;
|
| int _tmain(void)
| {
|   Managed *MP=new Managed();
|   MP->I=2;
|   CB.foo=Func;
|   int J=CB.foo(MP);
|   return 0;
| }
|
| The program throws an exception on the line
| containing "int J=CB.foo(MP);" complaining that it cannot
| marshal parameter #1, because the type definition contains
| no layout information.
|
| The only soution I have found is to use delegates, but
| this will require a massive set of changes to the
| application because delegates must point to managed object
| members only.
|
| Thanks/Anker
|



Mon, 26 Jul 2004 06:26:04 GMT  
 Dispatch tables with functions containing managed pointers
Thanks Jonathan,

I give it a try. I appreciate the help from both you and
Derrick.

Anker

Quote:
>-----Original Message-----
>I'm not 100% certain why this works: but if you change
the definition of
>Managed to:

>using namespace System::Runtime::InteropServices;

>[ StructLayout(Sequential) ]
>__gc class Managed {
>public:
>   int I;
>};

>then your code compiles and run correctly.

>--
>Jonathan Caves
>Microsoft Corporation

>This posting is provided "AS IS" with no warranties, and
confers no rights.
>You assume all risk for your use. ? 2002 Microsoft

Corporation. All rights

- Show quoted text -

Quote:
>reserved.

>http://www.microsoft.com/info/cpyright.htm.


>> How do I construct a dispatch table containing function
>> pointers that point to members of unmanaged objects, or
>> are normal functions? It is a requirement that the
>> functions accept at least one argument that is a pointer
>> to a managed object.

>> The following program illustrates the problem:

>> #using <mscorlib.dll>
>> #include <tchar.h>

>> using namespace System;

>> __gc class Managed
>> {
>> public:
>>   int I;
>> };

>> int Func(Managed *M)
>> {
>>   return M->I;
>> }

>> class CallBack
>> {
>> public:
>>   int (*foo)(Managed *);
>> } CB;

>> int _tmain(void)
>> {
>>   Managed *MP=new Managed();
>>   MP->I=2;
>>   CB.foo=Func;
>>   int J=CB.foo(MP);
>>   return 0;
>> }

>> The program throws an exception on the line
>> containing "int J=CB.foo(MP);" complaining that it
cannot
>> marshal parameter #1, because the type definition
contains
>> no layout information.

>> The only soution I have found is to use delegates, but
>> this will require a massive set of changes to the
>> application because delegates must point to managed
object
>> members only.

>> Thanks/Anker

>.



Mon, 26 Jul 2004 11:03:00 GMT  
 Dispatch tables with functions containing managed pointers
Jonathan,

It didn't last long!

As soon as there is a managed pointer in the class it
breaks again!

Try this:

#using <mscorlib.dll>

using namespace System::Runtime::InteropServices;

[ StructLayout(Sequential) ]
__gc class Managed
{
public:
  int I;
  Managed *Next;

Quote:
};

int Func(Managed *M)
{
  return M->I;

Quote:
}

class CallBack
{
public:
  int (*foo)(Managed *);

Quote:
} CB;

int _tmain(void)
{
  Managed *MP=new Managed();
  MP->I=2;
  MP->Next=NULL;
  CB.foo=Func;
  int J=CB.foo(MP);
  return 0;

Quote:
}

And you get this:

An unhandled exception of type 'System.TypeLoadException'
occurred in Test.exe

Additional information: Can not marshal field Next of type
Managed: This type can not be marshaled as a structure
field.

Bummer!

Quote:
>-----Original Message-----
>I'm not 100% certain why this works: but if you change
the definition of
>Managed to:

>using namespace System::Runtime::InteropServices;

>[ StructLayout(Sequential) ]
>__gc class Managed {
>public:
>   int I;
>};

>then your code compiles and run correctly.

>--
>Jonathan Caves
>Microsoft Corporation

>This posting is provided "AS IS" with no warranties, and
confers no rights.
>You assume all risk for your use. ? 2002 Microsoft

Corporation. All rights

- Show quoted text -

Quote:
>reserved.

>http://www.microsoft.com/info/cpyright.htm.


>> How do I construct a dispatch table containing function
>> pointers that point to members of unmanaged objects, or
>> are normal functions? It is a requirement that the
>> functions accept at least one argument that is a pointer
>> to a managed object.

>> The following program illustrates the problem:

>> #using <mscorlib.dll>
>> #include <tchar.h>

>> using namespace System;

>> __gc class Managed
>> {
>> public:
>>   int I;
>> };

>> int Func(Managed *M)
>> {
>>   return M->I;
>> }

>> class CallBack
>> {
>> public:
>>   int (*foo)(Managed *);
>> } CB;

>> int _tmain(void)
>> {
>>   Managed *MP=new Managed();
>>   MP->I=2;
>>   CB.foo=Func;
>>   int J=CB.foo(MP);
>>   return 0;
>> }

>> The program throws an exception on the line
>> containing "int J=CB.foo(MP);" complaining that it
cannot
>> marshal parameter #1, because the type definition
contains
>> no layout information.

>> The only soution I have found is to use delegates, but
>> this will require a massive set of changes to the
>> application because delegates must point to managed
object
>> members only.

>> Thanks/Anker

>.



Wed, 28 Jul 2004 04:40:17 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Pointers to Structure that contains pointer to Function

2. memory block containing pointers, aka pointer to pointer i believe

3. Struct containing pointer to function

4. Pointer to a function containing variable argument list

5. How to use (pointer to function), and function and pointer to (pointer to function)

6. Arrays containing pointers to functions...

7. dispatch table question

8. How to pass a function pointer from Managed C++ to unmanaged c++

9. pass vb.net function pointer to Managed C++

10. problem passing unmanaged pointer from one managed member function to another

11. Pointer Functions and Pointers to Pointer Functions

12. dereferencing pointer to a structure containing a pointer to int

 

 
Powered by phpBB® Forum Software