473,854 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetNativeVarian tForObject

Very recently I have written a custom interop in Managed
C++. We have a C# application that uses a COM DLL as a
data provider. The COM data provider talks to other
servers for retrieving real-time data.

First of all, we used TlbImp to generate the metadata for
the COM DLL and everything worked fine except the fact
that the COM Interop was slow.

In the next phase, we decided to write a custom interop in
MC++ to improve data retrieval rates, etc. Unfortunately,
we had to make some changes in the C# application for the
custom interop to work. Consider the following two
methods (after #import by the C++ compiler):

variant_t getTag(void)
void putTag(const VARIANT& var)

When we use the default COM interop, the C# application
finds VARIANTs as System::Objects . In our C# application,
we can pass any managed object reference in the putTag()
method and later on retrieve it using the getTag()
method. It works fine - RCW does the magic to marshall a
managed object reference straight into the COM layer and
later on it can retrieve it correctly.

I could not replicate this behaviour in my custom
interop :-(. I thought Marshal::GetNat iveVariantForOb ject
() and its counterpart should help me to marshal a managed
object reference. Say, for example:

void ManagedRecord:: set_Tag(Object* tagObj /* managed
object from C# app */)
{
try
{
if (m_pRecord != NULL) // unmanaged COM
smart pointer
{
Debug::Assert(t agObj != NULL);
IntPtr intPtr;
Marshal::GetNat iveVariantForOb ject
(tagObj, intPtr);
VARIANT* varTag =
static_cast<VAR IANT*>(static_c ast<void*>(intP tr));
Debug::Assert(v arTag != NULL);
m_pRecord->putTag(*varTag );
Nov 16 '05 #1
2 5482
Pratul,
But GetNativeVarian tForObject sets intPtr to 0 and the
rest obviously does not work.


GetNativeVarian tForObject doesn't change intPtr (not possible since
it's passed by value). It expects intPtr to already point to the
memory where you want the VARIANT to be stored. So try this

VARIANT varTag;
::VariantInit(& varTag);
IntPtr intPtr = (IntPtr)&varTag ;
Marshal::GetNat iveVariantForOb ject(tagObj, intPtr);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 16 '05 #2
Mattias

Thank you very much. It works. Your website,
http://www.dotnetinterop.com/ is also very good.

Regards

Pratul
Nov 16 '05 #3

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

Similar topics

9
4104
by: mupe | last post by:
Hi, i have a problem with a Type Library, which is written in C++. I am developing an application in C#.NET and have to use functions from this COM-Type Library. When I use these functions in the "old" VB it works but not in .NET. I think it is a problem with marshalling but I could not find a solution yet. First I included the Type Library in VS.NET 2003. Other functions of the TL work so the reference to the Lib must be correct.
2
4198
by: Heike | last post by:
Hello everybody, I have a Problem with C Sharp. I have to use external functions in DLLs. This Functions are written in Visual C++ 6 like this: int (__stdcall *GetGlobalValue) (VARIANT Appl, VARIANT &vRetVal); Now for the first time I want to use C Sharp and I find some examples to use Functions in extarnal DLLs, but I don't know how to cast the Variant-Paramters in C Sharp.
0
3004
by: dileepsree | last post by:
My aim is :- Pass C# object as a parameter of unmanaged Dll(vc++ 6.0) method . with in the unmaaged dll methedod construct similar C++ class instance . vice versa from dll method argument VARIANT result has to be converted to C# object For this , I tried Marshal.GetNativeVariantForObject(s, p); or Marshal.StructureToPtr(segInfo, ptrValue, false);
0
9751
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
11024
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
10679
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
10370
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
9513
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
7914
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...
0
7079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4559
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3186
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.