473,734 Members | 2,234 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 5476
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
4098
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
4193
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
2994
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
8783
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
9319
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...
1
9248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8193
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
6744
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
4555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3267
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
2185
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.