How to use in C++ a DLL written in .NET

  • Thread starter Thread starter Marco
  • Start date Start date
M

Marco

I wrote a DLL in VB.NET using .NET Framework library, to
access a Crystal Report. Then I registered the DLL using
regasm.exe.
Now I need to use this DLL from an executable that:
1) is written in C++ 2) Is based on MFC classes

This is a part of my executable code:

CLSID clsID;
HRESULT hr;
StartReport::_clsStartReportPtr pReport;

hr = CLSIDFromProgID(OLESTR("StartReport.clsStartReport"),
&clsID);
if( FAILED(hr) )
{
TRACE(_T("Error in research of ProgID for
StartReport.clsStartReport\n"));
return;
}

pReport.CreateInstance(clsID);



My C++ program can find correctly the clsID
but 'CreateInstance' can't initialize 'pReport' which is
NULL.


Can someone help me?
Thanks in advance.

Marco.
 
YOu are trying to use COM interop. Dont do this if you are
using vs.net c++. Much better is to use managed c++ code

Then just do

#using <yourdll>

StartReport sr = new StartReport()
.....

assuming StartReport is the name of your VB class
 
Thanks a lot, it is really better and easier.

To avoid error C3828 during compilation
is also necessary modify the statement 'new':

StartReport* sr;
#pragma push_macro("new")
#undef new
sr = new StartReport();
#pragma pop_macro("new")


Marco
 
Back
Top