473,320 Members | 1,976 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,320 software developers and data experts.

Bug: passing _com_ptr_t by value using managed extensions!?

Hi all,

I've experienced a problem when passing a smart-pointer (_com_ptr_t)
by value to another function using managed extensions. This only seems
to happen if the object is created via new. Can anyone confirm this
issue?

I wrote a little program that demonstrates this behaviour:

Main.cpp:
=========

#include "stdafx.h"

#import "msado25.tlb" rename_namespace("ADO") named_guids
rename("EOF", "EndOfFile")

#include "Test.h"

#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;

int main(int argc, char* argv[])
{
try
{
// CREATE A COM-OBJECT
CoInitialize(NULL);
ADO::_ConnectionPtr con(ADO::CLSID_Connection);
con->Open("PROVIDER=SQLOLEDB; SERVER=sqltest1\\dbserv1;
DATABASE=Netzdaten;Trusted_Connection=Yes;App=MCDa taSourceService",
"", "", 0);

CTest test;
test.TestMe(con); // WORKS FINE

CTest *ptest = new CTest();
ptest->TestMe(con); // THIS LINE DESTROYS THE UNDERLYING COM-OBJECT
(con)

// THE NEXT LINE THROWS A
System.NullReferenceException DUE TO THE FACT THAT THE COM-OBJECT HAS
BEEN DESTROYED
printf("connection string = %s\n",
(LPCTSTR)con->GetConnectionString());
}

catch (_com_error e)
{
_bstr_t bstrDescription = e.Description();
}

catch (Exception *e)
{
System::Console::WriteLine("\n\nException:");
System::Console::WriteLine(e->ToString());
}

return 0;
}

Test.h
=======

#ifndef _Test
#define _Test

#import "msado25.tlb" rename_namespace("ADO") named_guids
rename("EOF", "EndOfFile")

class CTest
{
public:
virtual void TestMe(ADO::_ConnectionPtr con);
};

Test.cpp
========

#include "stdafx.h"
#include "Test.h"

void CTest::TestMe(ADO::_ConnectionPtr con)
{
printf("by value = smart-ptr-address %x\n", con.GetInterfacePtr());
printf("connection string = %s\n",
(LPCTSTR)con->GetConnectionString());
}
Nov 17 '05 #1
0 1224

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

Similar topics

1
by: Bruise | last post by:
in VC6-SP5 with ProcessPack, \Include\comip.h, there's a serious bug might cause overwrite: HRESULT CreateInstance(LPCSTR clsidStringA, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)...
4
by: Jonathan Fielder | last post by:
Hi, My program (below) casts a double (which is in range for a float) to a float. As far as I know this should give me the nearest representable float, which will loose some precision. I...
0
by: Daniel G?rsch | last post by:
The following code works perfect in VS 2002 (compiled in a console project). Using VS 2003 the variable 'a' in the 'compute' function of the managed class 'M' is not correctly passed into this...
35
by: Maxim Yegorushkin | last post by:
The following code: #include <iostream> class base { private: virtual ~base() { std::cout << "virtual ~base()\n";
3
by: Wild Wind | last post by:
Hello, I made a post relating to this issue a while back, but I haven't received any answer, so here I am again. I am writing a mixed C++ dll which uses the following declaration: typedef...
1
by: Marty | last post by:
Hi, I'm still playing with this managed/unmanaged code within my VC++ project. I'm sure it's gonna work, need just some tuning that you could help me to find out. Here is below the .h and...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
3
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
4
by: PLS | last post by:
This problem is entirely in Microsoft's VS2005 C++ headers, so I'm hoping someone has hit it before and knows the fix. I am convertied a medium size (400k lines) C++ program over to VS2005...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.