dotnet interop:
Hello everyone!
I really need help on this one.
I have a vc++ ATL COM server object that I need to use from a c# application.
I have created an interface.
[Guid("AB883F7F-17A0-49EA-847E-BDCB77461311"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IRemote3
{
int SetIndex(ref int index);
int GetIndex(ref int index);
int GetComment([MarshalAs(UnmanagedType.BStr)] ref String comment);
int SetComment([MarshalAs(UnmanagedType.BStr)] String comment);
}
And I have tried two different ways to create an instance of the object and then try to connect to the member functions above.
First I tried to call coCreateInstanceEx like this.
int hResult = Ole32Methods.CoCreateInstanceEx(ref CLSID_Remote3,
IntPtr.Zero, 4, IntPtr.Zero, 1, ref qi);
public class Ole32Methods
{
[DllImport("ole32.dll", PreserveSig=false)]
static public extern int CoCreateInstanceEx(ref Guid clsid, IntPtr pUnkOuter,
int dwClsContext, IntPtr srv,
int num, ref MULTI_QI amqi );
}
I never got this to work. Maybe because of the COSERVERINFO object, I tried to use NULL instead of COSERVERINFO object because I don't need to run it remotely. Shouldn?t it work probably anyway?
Then I tried to use GetTypeFromCLSID like this.
int i = 5;
String str = "aaa";
bool bVal = true;
Guid CLSID_Remote3 = new Guid("6A8EFD8E-73CF-40B4-9AB7-75E841619022");
Type dcomType = Type.GetTypeFromCLSID( CLSID_Remote3, "localhost", false );
Object dcomObj = Activator.CreateInstance( dcomType );
IRemote3 iai = (IRemote3) dcomObj;
iai.SetIndex(ref i);
Console.WriteLine(i);
i = 4;
iai.GetIndex(ref i);
Console.WriteLine(i);
iai.SetComment(str);
Console.WriteLine(str);
str = "ooooo";
iai.GetComment(ref str);
Console.WriteLine(str);
iai = null;
Marshal.ReleaseComObject( dcomObj );
dcomObj = null;
This worked with the setIndex and GetIndex functions but when I tried with Get/SetComment functions something goes wrong with the BSTR parameters.
//The function
int SetComment([MarshalAs(UnmanagedType.BStr)] String comment);
//The call
iai.SetComment(str);
Does anyone know where the problem is? How should I do this the best way? Is there another way to do this?
Thanx! Help is really needed here I am ripping my hair of soon..
-----------------------
Posted by a user from .NET 247 (
http://www.dotnet247.com/)