473,654 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Q] Interop: Pointer to structure in interface method

I have COM-object with Iface interface:

typedef struct
{
....
} TStruct;

[
object,
uuid(...)
pointer_default (unique),
local
]
interface Iface : IUnknown
{
TStruct* getDataPtr();
}

Question: how can i use this object in .Net?

For wrappers

[StructLayout(La youtKind.Sequen tial)]
public struct TStruct
{
....
}

[Guid("..."), InterfaceType(C omInterfaceType .InterfaceIsIUn known)]
public interface Iface
{
TStruct getDataPtr();
}

Tlbexp output is following:

[
odl,
uuid(...),
version(1.0),
oleautomation,
custom(..., ...)

]
interface Iface : IUnknown {
HRESULT _stdcall getDataPtr([out, retval] TStruct * pRetVal);
};

Many Thanks!

Aug 15 '07 #1
1 2720
That's a poorly defined COM interface. Aside from the AddRef and
Release methods on the IUnknown interface, COM interfaces are supposed to
return an HRESULT. Return values are supposed to be passed through
parameters with the out and retval attributes applied to them.

The TLBIMP program is expecting this.

You are going to have to define this interface yourself in code, like
so:

[Guid("..."), InterfaceType(C omInterfaceType .InterfaceIsIUn known)]
[PreserveSig]
public interface Iface
{
IntPtr getDataPtr();
}

Notice the addition of the PreserveSig attribute, as well as the
returning of an IntPtr (since you are returning a pointer in your code).

Once you get the IntPtr, you can call the static PtrToStructure method
on the Marshal class to get the structure returned.

After you do that, you should pass the IntPtr to the static
FreeCoTaskMem method on the Marshal class, to de-allocate the memory
allocated in the implementation of the getDataPtr method. COM dictates that
you have to allocate memory locations that are passed around through the
IMalloc interface (which is wrapped up in the utility function
CoTaskMemAlloc) . If you are not allocating the memory that is returned with
this function, then that's another error in the implementation.

Your COM interface should really be defined like this:

[
object,
uuid(...)
pointer_default (unique),
local
]
interface IFace : IUnknown
{
HRESULT GetDataPtr([in, out] TStruct *struct);
}

This requires the structure to be allocated by the caller. All you have
to do is check to make sure that the pointer is not null in your code, and
then set the values on the structure passed in. Then, your .NET code will
work correctly.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<ya*****@gmail. comwrote in message
news:11******** **************@ 57g2000hsv.goog legroups.com...
>I have COM-object with Iface interface:

typedef struct
{
...
} TStruct;

[
object,
uuid(...)
pointer_default (unique),
local
]
interface Iface : IUnknown
{
TStruct* getDataPtr();
}

Question: how can i use this object in .Net?

For wrappers

[StructLayout(La youtKind.Sequen tial)]
public struct TStruct
{
...
}

[Guid("..."), InterfaceType(C omInterfaceType .InterfaceIsIUn known)]
public interface Iface
{
TStruct getDataPtr();
}

Tlbexp output is following:

[
odl,
uuid(...),
version(1.0),
oleautomation,
custom(..., ...)

]
interface Iface : IUnknown {
HRESULT _stdcall getDataPtr([out, retval] TStruct * pRetVal);
};

Many Thanks!

Aug 15 '07 #2

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

Similar topics

1
1993
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, this layer manage the underlying COM Objects and upon request, provide a pointer to those objects to the 'API Consumer', following is an illustration of the system: API Consumer ( Native C++/C# ) || ******************************************* * ...
1
6779
by: ivang | last post by:
Hello, All! Please help me to marshal following win32 structure: typedef struct _STARTUPINFOW { DWORD cb; LPWSTR lpReserved;
8
3097
by: Nadav | last post by:
Hi, Introduction: ********************* I am writing a mixed mode application I have a COM module and a .NET module that communicate with each other. The COM exposes a custom sink interface, the .NET module implement the Sink interface ( IUnknown based ) and the COM call the methods of this interface asynchronously.
17
1850
by: Aaron | last post by:
I've got a doozie of a problem! I and others have been trying to figure this out for too long and I've come to the conclusion that I should probably look for some support.. Ok, I have a COM component written in C++ (I don't have the source just the binary) and am referencing it from VB. I would say about 99% of the functionality exposed by this COM component works fine from vb .net but I am having a problem calling a method which has a...
6
2405
by: Nataraj1978 | last post by:
I have a requirement to use a dll written in Visual C++ 6.0 in my C# application. In order to establish the link between my application and the dll, I have written a ATL COM Component in Visual C++.NET . This COM component is referenced in my C# application. The COM component statically links with the 6.0 dll. I am facing problems in passing data from C# to the dll through the COM component. Description: 1. The dll exposes a function...
4
7367
by: Technics | last post by:
Ok I will be as clearer as I can (sorry for english/technical mistakes) I would like to write an audio application that supports ASIO drivers. I downloaded the ASIO sdk from Stainberg and I read the docs and viewed the code provided. The sdk retrieve the varius ASIO drivers available from windows registry. The various drivers are COM objects wich must be loaded dinamically after retrieving the CLSID from the registry. In C++ this is...
1
1360
by: =?Utf-8?B?UmFodWwgUGF0ZWw=?= | last post by:
I have a COM dll that I am consuming from C#, one of the methods is returning an object as void** which gets converted to IntPtr in C#. So the question is how do I convert the IntPtr to myObject wehre myObject is of type IMyIface myObject; (I know that hte void** is of type IMyIface).
3
6146
by: michelqa | last post by:
Hi, I already post a similar question last week without success. Ok I want to get the current text selection in a RICHEDIT control.. This can be easily done in C++ with EM_EXGETSEL message. I really need to do the same thing in C#. How can I put the structure in memory to be able to call SendMessage and get the expected results in the structure.
3
2748
by: =?Utf-8?B?TWluZ3lp?= | last post by:
Hi, I have the following question regarding communicating with a OCX control using C#. Idon't have access to the ocx code, so my c# code is actually mimic the behavior of C++ counterparts. Basically, I have problem passing a structure to a function. Here is the C++ part code: //****************************************
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.