473,396 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to interop .Net client and COM DLL?

Using VS2005 SP1, I have a C++ COM DLL and a C# client.

If the C++ and MIDL source use type INT_PTR and the C# client uses type
IntPtr, then the result works in x86 (32 bit) but doesn't compile in x64
(64-bit). The C# compiler gives error messages like
Error CS1503: Parameter '6': cannot convert from 'out System.IntPtr'
to 'out long'.

MSDN suggests that the C++ and MIDL source should use type void* instead.
http://msdn2.microsoft.com/en-us/library/sak564ww.aspx
* COM value type COM reference type System type
* void * void ** System.IntPtr

This MSDN page does not mention native type INT_PTR at all, it says void*.
I couldn't imagine what difference it would make, but I still can't find any
other solution, so I obeyed this page. Now the MIDL compiler gives error
messages like
error MIDL2139: type of the parameter cannot derive from void or void *

Does anyone know how to do COM interop in x64?

Oct 16 '07 #1
3 3472
I guess this is part of the third-best answer:
http://msdn2.microsoft.com/en-us/lib...20(vs.71).aspx

tlbimp, ildasm, notepad, ilasm. I changed a few parameter types from int64
to native int. Then the C# compiler saw them as IntPtr, matching the IntPtr
variables in the C# client code.

It still seems to me that the best answer would be for MIDL's INT_PTR to
translate to IL's native int automatically, instead of translating to int32
or int64 depending on the compilation environment. Does anyone know why
that doesn't happen?

It still seems to me that the second-best answer would be for the C#
compiler in an x64 environment to consider IntPtr and Int64 to be
compatible, as the C# compiler in an x86 environment does consider IntPtr
and Int32 to be compatible. Does anyone know why that doesn't happen?
"Norman Diamond" <nd******@community.nospamwrote in message
news:u0**************@TK2MSFTNGP03.phx.gbl...
Using VS2005 SP1, I have a C++ COM DLL and a C# client.

If the C++ and MIDL source use type INT_PTR and the C# client uses type
IntPtr, then the result works in x86 (32 bit) but doesn't compile in x64
(64-bit). The C# compiler gives error messages like
Error CS1503: Parameter '6': cannot convert from 'out System.IntPtr'
to 'out long'.

MSDN suggests that the C++ and MIDL source should use type void* instead.
http://msdn2.microsoft.com/en-us/library/sak564ww.aspx
* COM value type COM reference type System type
* void * void ** System.IntPtr

This MSDN page does not mention native type INT_PTR at all, it says void*.
I couldn't imagine what difference it would make, but I still can't find
any
other solution, so I obeyed this page. Now the MIDL compiler gives error
messages like
error MIDL2139: type of the parameter cannot derive from void or void *

Does anyone know how to do COM interop in x64?
Oct 18 '07 #2
Norman,
>It still seems to me that the best answer would be for MIDL's INT_PTR to
translate to IL's native int automatically, instead of translating to int32
or int64 depending on the compilation environment. Does anyone know why
that doesn't happen?
INT_PTR is a typedef that maps to int or __int64 depending on platform
(defined in BaseTsd.h). I believe the fact that it's pointer sized is
lost already in the typelib compilation process (you can check by
looking at the tlb file in OleView).

>It still seems to me that the second-best answer would be for the C#
compiler in an x64 environment to consider IntPtr and Int64 to be
compatible, as the C# compiler in an x86 environment does consider IntPtr
and Int32 to be compatible. Does anyone know why that doesn't happen?
Compatible in what way? In your original message it sounds like you
got something like this to work

static void Main()
{
IntPtr p;
Foo(out p);
}

static void Foo(out int i) { i = 42; }

But that gives a compile error for me.

>This MSDN page does not mention native type INT_PTR at all, it says void*.
I couldn't imagine what difference it would make, but I still can't find
any
other solution, so I obeyed this page. Now the MIDL compiler gives error
messages like
error MIDL2139: type of the parameter cannot derive from void or void *

Does anyone know how to do COM interop in x64?
You get that error because a void* doesn't carry enough type
information for marshaling code. But if that's not needed (for
in-proc, same apartment calls) you can apply the [local] attribute to
the method.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 18 '07 #3
"Mattias Sjogren" <ma********************@mvps.orgwrote in message
news:ei**************@TK2MSFTNGP04.phx.gbl...
Norman,
>>It still seems to me that the best answer would be for MIDL's INT_PTR to
translate to IL's native int automatically, instead of translating to
int32 or int64 depending on the compilation environment. Does anyone know
why that doesn't happen?

INT_PTR is a typedef that maps to int or __int64 depending on platform
(defined in BaseTsd.h). I believe the fact that it's pointer sized is lost
already in the typelib compilation process (you can check by looking at
the tlb file in OleView).
I looked at the C# error message and didn't need to look at OleView ^_^

Yes I see that BaseTsd.h destroys the information. Do you know if it would
be possible to put something in BaseTsd.h that converts to the .Net type
System.IntPtr instead? Just for MIDL compilations of course. (C++
compilations will still need the underlying type.)
>>It still seems to me that the second-best answer would be for the C#
compiler in an x64 environment to consider IntPtr and Int64 to be
compatible, as the C# compiler in an x86 environment does consider IntPtr
and Int32 to be compatible. Does anyone know why that doesn't happen?

Compatible in what way? In your original message it sounds like you got
something like this to work

static void Main()
{
IntPtr p;
Foo(out p);
}

static void Foo(out int i) { i = 42; }

But that gives a compile error for me.
You are right, it yields a compile error. The Foo in my case was a C++ COM
method, but that doesn't matter at the moment. In my older code, there were
two crucial differences which I forgot. The method used to return only one
value, so it was the return type instead of being an out parameter. And
indeed even for that there was no built-in compatibility, but a cast solved
it trivially.

So this interop problem is identical for both x86 and x64. out IntPtr seems
to be impossible (except by hand editing with tlbimp -ildasm -notepad ->
ilasm).
>>This MSDN page does not mention native type INT_PTR at all, it says
void*. I couldn't imagine what difference it would make, but I still
can't find any other solution, so I obeyed this page. Now the MIDL
compiler gives error messages like
error MIDL2139: type of the parameter cannot derive from void or void
*

Does anyone know how to do COM interop in x64?

You get that error because a void* doesn't carry enough type information
for marshaling code.
It does for what I want. My COM method returns two IntPtr values to the C#
client (used to be just one as mentioned above). The IntPtr values need to
be marshalled as either 32 or 64 bits. native int is working, it's just a
pain that this manual process was required.
But if that's not needed (for in-proc, same apartment calls) you can apply
the [local] attribute to the method.
Aha, I'll try that. Thank you.

Of course it would be meaningless to send a pointer value out to a different
process. A different thread shouldn't matter though.

Oct 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kedar Agarkar | last post by:
: This is general query seeking opinions about COM+ Development wherein Server is developed in C# and Client accessing that across machines is also C#. Wish to seek experienced words on...
1
by: Nadav | last post by:
Hi, Introduction *************** I have a system build of a collection of 'Native COM objects' and '.NET COM interop' objects, all of the COM objects are managed through a 'Native COM' layer,...
6
by: Chris | last post by:
Hi, - How can I open an existing word document from a C#-client and manipulate that document from within the C#-client ? - How can I open an excel document that is embedded in a word-document...
4
by: Razzie | last post by:
Hey all, I developed an application as a com object. I set the 'register for com interop' in the project properties to true. How do I deploy this on another machine? I can't add the DLL as a...
3
by: Jeffery Franzen | last post by:
Anyone know where the documentation is regarding Activex controls in asp web forms? I'm using VS.NET 2002 enterprise and am trying to use Activex controls in vb.net web form app. I do the add...
2
by: pagates | last post by:
Hi All, I am having a problem getting a .NET control library to "play nice" with VB6. This particular control wraps a number of other .NET controls in the same project. It has a number of...
5
by: Richard Lewis Haggard | last post by:
I am trying to create multi-dimensioned arrays in conventional ASP pages and pass these arrays as arguments to functions that are in a C# interop assembly. ASP complains because it doesn't...
0
by: user2008 | last post by:
Hi everybody, I'm working on exposing a .Net (C#) COM Server. Manage Code is exposting an interface with two methods. These methods take an IN/OUT Struct as parameter. Requirement is that the...
4
by: Christiano Donke | last post by:
i'm writing an app that uses an excel interop to convert the xls file to html.. while writing it, i had no problem... it problem is coming out when i try do deploy it... i've tried merging it...
0
by: ramanujam kv | last post by:
1) I have created the COM assembly and added a reference to Interop DLL. For example: I have added IStreaming interface to idl file and generated a Streaming.tlb file. I created...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.