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

calling unmanaged code from managed code

Hi

I've a dotnet application in C#. I need to call a COM method from it. The
com component is written in VC++. One method is to add a refrence to that
COM component in my dotnet application that would create and interop and I
can use the methods of that COM interfaces easily. But the problem with this
implementation is that I need ship the interop also along with the output
assembly of the dotnet project. Is there any other way to call the COM
method such that my output assembly of dotnet application does'nt rely on
the interop.

Regards

Usman
May 28 '07 #1
3 1650
>Is there any other way to call the COM
>method such that my output assembly of dotnet application does'nt rely on
the interop.

You can manually declare the COM types you need to use in your code.
See the ComImport, Guid, ComClass and ComInterfaceType attributes.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 28 '07 #2
Can you please explain it a bit more. How to manually declare the COM types
etc. Also are CompImport, Guid, ComClass etc are classes of dotnet?? I
could'nt find any.
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:e9****************@TK2MSFTNGP06.phx.gbl...
Is there any other way to call the COM
method such that my output assembly of dotnet application does'nt rely on
the interop.


You can manually declare the COM types you need to use in your code.
See the ComImport, Guid, ComClass and ComInterfaceType attributes.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

May 29 '07 #3
Ok I found it. Infact I disassembled the interop using a dissembler and got
the code from there.

I've added following classes to my dotnet code (given below), from where I
want to call the COM methods. But when I try to use it as

string szParam1, szParam2, szParam3;
string szRetVal;

MyInterfaceClass objCom = new MyInterfaceClass();
szRetVal = objCom.MyMethod(szParam1, szParam2, szParam3);

I get a runtime exception as

An unhandled exception of type 'System.ExecutionEngineException' occurred in
ConsoleApplication1.exe

Do you have any idea, where I'm doing it wrong. I've checked the GUID of the
interface and the COM, both are correct. Please help me with any suggestion.

Regards

Usman

////////////////////////////////////////////////START OF
CODE/////////////////////////////////////////////////////////////////////////////////////

[assembly: Guid("2feb9bb4-35a5-4044-82f2-4063f31c156d")]
[assembly: ImportedFromTypeLib("MyComLib")]
namespace MyComLib
{

[ComImport, Guid("0287D794-187B-4CAC-A87D-BF2AD3BCFBA5"),
CoClass(typeof(MyInterfaceClass))]
public interface MyInterface : IMyInterface
{
}

[ComImport, TypeLibType((short) 2), ClassInterface((short) 0),
Guid("BEB239AB-6220-4FBF-BBEB-9B6234FA23BA")]
public class MyInterfaceClass : IMyInterface, MyInterface
{
// Methods
[MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType=MethodCodeType.Runtime), DispId(1)]
public virtual extern short MyMethod([In, MarshalAs(UnmanagedType.BStr)]
string szParam1, [In, MarshalAs(UnmanagedType.BStr)] string szParam2, [In,
MarshalAs(UnmanagedType.BStr)] string szParam3);
[return: MarshalAs(UnmanagedType.BStr)]
}

[ComImport, TypeLibType((short) 0x10c0),
Guid("0287D794-187B-4CAC-A87D-BF2AD3BCFBA5")]
public interface IMyInterface
{
[MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType=MethodCodeType.Runtime), DispId(1)]
short MyMethod([In, MarshalAs(UnmanagedType.BStr)] string szParam1, [In,
MarshalAs(UnmanagedType.BStr)] string szParam2, [In,
MarshalAs(UnmanagedType.BStr)] string szParam3);

}
}
////////////////////////////////////////////////END OF
CODE/////////////////////////////////////////////////////////////////////////////////////

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:e9****************@TK2MSFTNGP06.phx.gbl...
Is there any other way to call the COM
method such that my output assembly of dotnet application does'nt rely on
the interop.


You can manually declare the COM types you need to use in your code.
See the ComImport, Guid, ComClass and ComInterfaceType attributes.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


May 29 '07 #4

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

Similar topics

9
by: Andrew Cumming | last post by:
I am having trouble using Platform Invoke to call C++ DLLs from C# windows applications. The DLLs seem to load, but I get run-time errors to the effect that the named entry points cannot be found....
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
1
by: Adam Clauss | last post by:
I have an unmanaged C++ library that I need to use through C#. I created a Managed C++ class library and through it wrote a wrapper class to call what I need. However, one of the methods (an...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
0
by: sklett | last post by:
I'm having a really hard time with wrapping an unmanaged class with a managed class, then calling that managed class from my C# code. I will show you the three pieces, then explain: --------...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
1
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
1
by: Arne Adams | last post by:
Hi, I try to use a C# Dialog in a legacy MFC application. The problem seems to boil down to the following: from an unmanaged console application I can call any function of the managed bridge - if...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.