calling unmanaged code from managed code

U

Usman Jamil

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
 
M

Mattias Sjögren

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
 
U

Usman Jamil

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.
 
U

Usman Jamil

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/////////////////////////////////////////////////////////////////////////////////////
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top