473,657 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing COM autoptr to managed C++ dll

I have recently converted all of my native C++ dll's to be compiled
with the /CLR switch. After doing this, I notcied a very strange and
frustrating issue.

One of my managed dll's calls another using a COM smartptr
(MSXML2::IXMLDO MDocumentPtr to be exact). When calling my function in
another managed DLL, my app starts throwing exceptions.

If I step into my function, before getting into my function, I see a
call to AddRef() and later a call to Release() on my smartptr. When I
am in the Release() call I see several levels of calls into the CLR
(mscorwks.dll). After this, the IP gets lost somewhere.

Now, if I step over my function, it comes back ok, but the smartptr is
messed up and when I try to use it, an exception is thrown.

The strange thing is, if I add the function that is in my separate
dll, into the same dll that is making the call, everything works fine.

I've tried reproducing this in a small test case, but it appears to
work. I don't know if I'm doing something wrong and it manages to work
by accident in the small test case or not. What is going on with the
COM object (I've even tried using a dump ptr- same problem)

I have gone over the "Converting Managed Extensions ... from Pure ...
to Mixed Mode". All my settings are set according to the document.
Here's a snippet of my code.

CDEGlobalEngine ::TestFn()
{
MSXML2::IXMLDOM DocumentPtr pXmlDoc;
HRESULT hr = pXmlDoc.CreateI nstance(CLSID_D OMDocument);

if (SUCCEEDED(hr))
{
// if i change my function to not pass in the pXmlDoc, everything
works fine
boolbReturnCode = CXTXml::LoadXml ("options.xm l", pXmlDoc);
}
}

Here's my code to init the CRT :

CDEGlobalEngine ::CDEGlobalEngi ne()
{
HMODULE hDll = ::GetModuleHand le("otherDLL.dl l");

if (hDll)
{
pfnEnsureInit pfnDll = (pfnEnsureInit) ::GetProcAddres s(hDll,
"_DllEnsureInit @0");

if (pfnDll)
{
pfnDll();
}
}

// init COM for MSXML library
CoInitialize(NU LL);
}
Nov 16 '05 #1
8 5229
I have figured out what was causing the problem, and I have a solution
although a terrible one...

My code was split up where my class method declarations are in a header file
and my class definitions are in a .cpp file.

When my code is in the .cpp file, I need to link to the .lib file. I add the
appropriate .lib file in my projects properties (Link->input).

If I make the code inline by moving it to the header file, I no longer have
to link, and everything works. This however will blow the size of my dll's
enormously.

So obviously this has something to do with a linking issue. This is how I
linked libraries in unmanaged C++, what's going on with the managed
libraries?

Thanks

"Drew" <dr******@yahoo .com> wrote in message
news:f7******** *************** **@posting.goog le.com...
I have recently converted all of my native C++ dll's to be compiled
with the /CLR switch. After doing this, I notcied a very strange and
frustrating issue.

One of my managed dll's calls another using a COM smartptr
(MSXML2::IXMLDO MDocumentPtr to be exact). When calling my function in
another managed DLL, my app starts throwing exceptions.

If I step into my function, before getting into my function, I see a
call to AddRef() and later a call to Release() on my smartptr. When I
am in the Release() call I see several levels of calls into the CLR
(mscorwks.dll). After this, the IP gets lost somewhere.

Now, if I step over my function, it comes back ok, but the smartptr is
messed up and when I try to use it, an exception is thrown.

The strange thing is, if I add the function that is in my separate
dll, into the same dll that is making the call, everything works fine.

I've tried reproducing this in a small test case, but it appears to
work. I don't know if I'm doing something wrong and it manages to work
by accident in the small test case or not. What is going on with the
COM object (I've even tried using a dump ptr- same problem)

I have gone over the "Converting Managed Extensions ... from Pure ...
to Mixed Mode". All my settings are set according to the document.
Here's a snippet of my code.

CDEGlobalEngine ::TestFn()
{
MSXML2::IXMLDOM DocumentPtr pXmlDoc;
HRESULT hr = pXmlDoc.CreateI nstance(CLSID_D OMDocument);

if (SUCCEEDED(hr))
{
// if i change my function to not pass in the pXmlDoc, everything
works fine
boolbReturnCode = CXTXml::LoadXml ("options.xm l", pXmlDoc);
}
}

Here's my code to init the CRT :

CDEGlobalEngine ::CDEGlobalEngi ne()
{
HMODULE hDll = ::GetModuleHand le("otherDLL.dl l");

if (hDll)
{
pfnEnsureInit pfnDll = (pfnEnsureInit) ::GetProcAddres s(hDll,
"_DllEnsureInit @0");

if (pfnDll)
{
pfnDll();
}
}

// init COM for MSXML library
CoInitialize(NU LL);
}

Nov 16 '05 #2
Here's a small app that reproduces the problem. Hopefully someone
knows what is going on here. I'm hoping it's not a bug in VS 2003
compiler...

DLL project 1

Managed class (.h)
public __gc class CWrapper
{
public:
CWrapper() { }

void LoadOptions();
};

Managed class (.cpp)
void CWrapper::LoadO ptions()
{
MSXML2::IXMLDOM DocumentPtr pDoc;
pDoc.CreateInst ance(CLSID_DOMD ocument);

::LoadXml("opti ons.xml", pDoc); // declared/defined in header file
- works fine

CXml::Load("opt ions.xml", pDoc); // declared in header/defined in
cpp file (after this call- things go wacky)

CXml::Load("opt ions.xml", pDoc);
}
DLL project 2

(header file)
extern "C" __declspec(dlle xport) void LoadXml(const std::string& file,
MSXML2::IXMLDOM DocumentPtr pDoc)
{
_variant_t varXml = file.c_str();
_variant_t varOut = pDoc->load(varXml) ;
}

Mixed class (header file)
class __declspec(dlle xport) CXml
{
public:
CXml(void) { }
~CXml(void) { }

static bool Load(const std::string& filename,
MSXML2::IXMLDOM DocumentPtr pDoc);
};

Mixed class (cpp file)
bool CXml::Load(cons t std::string& filename,
MSXML2::IXMLDOM DocumentPtr pDoc)
{
_variant_t varXml = filename.c_str( );
_variant_t varOut = pDoc->load(varXml) ;

return (true);
}

If I move the definition of CXml::Load into the header file and
comment out the CXml::Load in the cpp file, things work normally. Note
that if I do the same thing to the first call to "::LoadXml" , the same
problem occurs. Of course when the code is in the cpp file, I have to
link specifically to the .lib file. When the code is in the header
file, a link is not necessary. I'm guessing the linking is where the
problem is occurring.
dr******@yahoo. com (Drew) wrote in message news:<f7******* *************** ***@posting.goo gle.com>...
I have recently converted all of my native C++ dll's to be compiled
with the /CLR switch. After doing this, I notcied a very strange and
frustrating issue.

One of my managed dll's calls another using a COM smartptr
(MSXML2::IXMLDO MDocumentPtr to be exact). When calling my function in
another managed DLL, my app starts throwing exceptions.

If I step into my function, before getting into my function, I see a
call to AddRef() and later a call to Release() on my smartptr. When I
am in the Release() call I see several levels of calls into the CLR
(mscorwks.dll). After this, the IP gets lost somewhere.

Now, if I step over my function, it comes back ok, but the smartptr is
messed up and when I try to use it, an exception is thrown.

The strange thing is, if I add the function that is in my separate
dll, into the same dll that is making the call, everything works fine.

I've tried reproducing this in a small test case, but it appears to
work. I don't know if I'm doing something wrong and it manages to work
by accident in the small test case or not. What is going on with the
COM object (I've even tried using a dump ptr- same problem)

I have gone over the "Converting Managed Extensions ... from Pure ...
to Mixed Mode". All my settings are set according to the document.
Here's a snippet of my code.

CDEGlobalEngine ::TestFn()
{
MSXML2::IXMLDOM DocumentPtr pXmlDoc;
HRESULT hr = pXmlDoc.CreateI nstance(CLSID_D OMDocument);

if (SUCCEEDED(hr))
{
// if i change my function to not pass in the pXmlDoc, everything
works fine
boolbReturnCode = CXTXml::LoadXml ("options.xm l", pXmlDoc);
}
}

Here's my code to init the CRT :

CDEGlobalEngine ::CDEGlobalEngi ne()
{
HMODULE hDll = ::GetModuleHand le("otherDLL.dl l");

if (hDll)
{
pfnEnsureInit pfnDll = (pfnEnsureInit) ::GetProcAddres s(hDll,
"_DllEnsureInit @0");

if (pfnDll)
{
pfnDll();
}
}

// init COM for MSXML library
CoInitialize(NU LL);
}

Nov 16 '05 #3
Hello Drew,

Thanks for your post.

As I understand, you exported unmanaged class and function from DLL2 which
will be used in another DLL say, DLL1. You include the header files of DLL2
in DLL1. Please correct me if there is any misunderstandin g. You are
correct that you will get linker errors if you neither link the .lib file
of DLL2 nor add the corresponding source code of DLL2 in DLL1.

If you want to link DLL2.lib to DLL1, please make sure that the DLL2
exported function or class should be imported to DLL1. That is, we should
change dllexport to dllimport of the header files of DLL2 and include them
to DLL1. For example,

Change
class __declspec(dlle xport) CXml
To
class __declspec(dlli mport) CXml

Please feel free to let me know if you have any problems or concerns. I
look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #4
Hi Tian- thanks for the response.

Even when I change the code to look like:

#ifdef MSXMLLIB2_EXPOR TS
#define XML_API __declspec(dlle xport)
#else
#define XML_API __declspec(dlli mport)
#endif

class XML_API CXml

where MSXMLLIB2_EXPOR TS is defined in my lib2 project, I still have to
manually link my app by placing msxmllib2.lib in the additional
dependencies box in my lib project (this isn't needed if I change my
project to not use managed extensions). Anyways, linking isn't really
my issue. It's what happens to the COM smart pointer after a call to
my lib2.

To illustrate my problem a slightly different way, here:

MSXML2::IXMLDOM DocumentPtr m_pDoc;
m_pDoc.CreateIn stance(CLSID_DO MDocument);

// if LoadXml is defined in the .h the Release call works.
// if LoadXml is defined in the .cpp file, release causes an
exception
::LoadXml("opti ons.xml", m_pDoc);

m_pDoc.Release( ); // ignore the fact I shouldn't call release on a
smartptr.
// the real point is that m_pDoc is messed up and
causes
// an exception when I try and use it when
// LoadXml is defined in the .cpp file

Thanks.
Drew

ti******@online .microsoft.com (Tian Min Huang) wrote in message news:<Jr******* *******@cpmsftn gxa06.phx.gbl>. ..
Hello Drew,

Thanks for your post.

As I understand, you exported unmanaged class and function from DLL2 which
will be used in another DLL say, DLL1. You include the header files of DLL2
in DLL1. Please correct me if there is any misunderstandin g. You are
correct that you will get linker errors if you neither link the .lib file
of DLL2 nor add the corresponding source code of DLL2 in DLL1.

If you want to link DLL2.lib to DLL1, please make sure that the DLL2
exported function or class should be imported to DLL1. That is, we should
change dllexport to dllimport of the header files of DLL2 and include them
to DLL1. For example,

Change
class __declspec(dlle xport) CXml
To
class __declspec(dlli mport) CXml

Please feel free to let me know if you have any problems or concerns. I
look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #5
Hi Drew,

Thanks for your response. As I understand, the problem you are facing is
that a smart pointer passing to a DLL function will cause an exception.
Please correct me if there is any misunderstandin g. I think more
information is needed before moving forward:

1. Could you please tell me the detailed exception message?

2. Please make sure that you import the same type library of MSXML on both
DLL and APP.

3. Do you link the same C run-time library to both DLL and APP? If not, it
may cause potential errors per KB article below:

PRB: Potential Errors Passing CRT Objects Across DLL Boundaries
http://support.microsoft.com/?id=190799

HOWTO: Link with the Correct C Run-Time (CRT) Library
http://support.microsoft.com/?id=140584

4. Is it possible for you to post simple projects which are able to
reproduce the problem? I will be glad to check it on my side.

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #6
I will post a simple application. I did last week, but my news server
must have lost it.
//MSXML2::IXMLDOM Document* m_pDoc; // native ptr this works fine

MSXML2::IXMLDOM DocumentPtr m_pDoc; // after passing the smart ptr
// into the function, the ptr is
invalid
// try to use it after the fn,
and it
// crashes the CLR

m_pDoc.CreateIn stance(CLSID_DO MDocument);

//CoCreateInstanc e(CLSID_DOMDocu ment, NULL,
// CLSCTX_INPROC_S ERVER,
// IID_IXMLDOMDocu ment, (void**)&m_pDoc );

::LoadXml("opti ons.xml", m_pDoc);

m_pDoc.Release( );
..cpp of DLL
void LoadXml(const std::string& filename, MSXML2::IXMLDOM DocumentPtr
pDoc)
{
_variant_t varXml = filename.c_str( );
_variant_t varOut = pDoc->load(varXml) ;
}
I am not linking with msvcrt.lib.

Thanks,
-Drew

ti******@online .microsoft.com (Tian Min Huang) wrote in message news:<#h******* *******@cpmsftn gxa06.phx.gbl>. ..
Hi Drew,

Thanks for your response. As I understand, the problem you are facing is
that a smart pointer passing to a DLL function will cause an exception.
Please correct me if there is any misunderstandin g. I think more
information is needed before moving forward:

1. Could you please tell me the detailed exception message?

2. Please make sure that you import the same type library of MSXML on both
DLL and APP.

3. Do you link the same C run-time library to both DLL and APP? If not, it
may cause potential errors per KB article below:

PRB: Potential Errors Passing CRT Objects Across DLL Boundaries
http://support.microsoft.com/?id=190799

HOWTO: Link with the Correct C Run-Time (CRT) Library
http://support.microsoft.com/?id=140584

4. Is it possible for you to post simple projects which are able to
reproduce the problem? I will be glad to check it on my side.

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #7
Hi Drew,

Thanks for your update. I noticed that the Release() call goes into CLR, so
I suggest you to explicitly declare IXMLDOMDocument Ptr as __nogc and then
check whether or not the problem still exists:

__nogc MSXML2::IXMLDOM DocumentPtr m_pDoc;

In the meantime, I am standing for your reproducible project.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #8
I spent a day or so thinking that this had something to do with the
COM smartptr's. After wrapping MSXML's calls in my own library, I
noticed I still had the problem (I was no longer crossing DLL
boundries when calling a COM smartptr, so this confused me). I then
removed all SmartPtr's from my wrapped class and I am now using native
ptrs (MSXML2::IXMLDO MDocument*, etc.) I still had the problem.

I finally contacted MS tech support yesterday, and they have been able
to reproduce this and are currently looking into the problem.
Hopefully this gets resolved and if/when it does, I'll post a response
here.

-Drew

ti******@online .microsoft.com (Tian Min Huang) wrote in message news:<xJ******* *******@cpmsftn gxa06.phx.gbl>. ..
Hi Drew,

Thanks for your update. I noticed that the Release() call goes into CLR, so
I suggest you to explicitly declare IXMLDOMDocument Ptr as __nogc and then
check whether or not the problem still exists:

__nogc MSXML2::IXMLDOM DocumentPtr m_pDoc;

In the meantime, I am standing for your reproducible project.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #9

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

Similar topics

5
36397
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
1
3529
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
1
5210
by: Steven Blair | last post by:
Hi, I am having problem passing a string by reference to some C++ code. Wondered if anyone could help me out. C# code: myFunc(str);
2
4673
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
22
25575
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to compile. <WebMethod()> _ Public Function VerifySku(ByVal skus As XmlDataDocument) As DataSet Test program : Dim cartSet As DataSet cartSet = ws.VerifySku(cartSet)
3
3665
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following runtime error, " Attempting to call into managed code without transitioning out first. Do not attempt to run managed code inside low-level native extensibility points, such as the vectored exception handler, since doing so can cause corruption...
17
3672
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 compile i get the following error: Attempted to read or write protected memory the C function should not be manipulating the input arra, only reading
0
2010
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to jump to this unmanaged function, but the values of the parameters inside this function Im seeing are not correct(they have some garbage values). //unmanaged class (C++ application)
17
7239
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
8397
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
8732
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
8605
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
7333
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
6167
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
5632
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();...
0
4158
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
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
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

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.