473,387 Members | 1,721 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,387 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 1736
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

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,...
0
by: Andy Turner | last post by:
I've got an ATL out-of-process COM server, which implements a single CoClass (let's call it MyServer) which I'd like to get events from within a C# front end. I've inserted a reference to the COM...
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...
22
by: glenn | last post by:
I have a COM Server that I've written based on information from the book ..NET and COM / the complete Interop Guide. I have gotten the project to compile and I've located the regasm.exe program...
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...
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...
1
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a...
2
by: JimLad | last post by:
Hi, First of all I didn't design this website, but I have been asked to fix it with the minimum fuss! Website is using .NET on IIS6 with an Excel Interop to produce reports. The website...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.