Dumb COM Interop question
-
Hi, I'm pretty new to all this interop stuff, and I've come up against a bit of an obstacle. In my method I'm trying to return an instance of another class from C# assembly. Great! Except it doesn't work. :( I keep getting Error 5 "Object variable or with block not set" in my VB client. Here's some sample code I knocked together to demonstrate... C# COM Server
using System; using System.Runtime.InteropServices; namespace COMReturnTest { [Guid("883EF833-3874-4831-9757-B44E987E80DF")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface _ReturnType { string TestString { get; } int TestInt { get; } } [Guid("29B3F02E-27A6-4535-8835-62D23BA2DA1E")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface _TestClass { COMReturnTest._ReturnType StringEmptyTest(); COMReturnTest._ReturnType StringNullTest(); COMReturnTest._ReturnType StringSetTest(); System.String StringReturn(); } [Guid("01A699E1-05ED-404e-A5B9-7C2735F97268")] [ClassInterface(ClassInterfaceType.None)] [ProgId("COMReturnTest.ReturnType")] public class ReturnType : COMReturnTest._ReturnType { #region class members private string _teststring = String.Empty; private int _testint = 0; #endregion #region class constructors public ReturnType(string TestString, int TestInt) { _teststring = TestString; _testint = TestInt; } #endregion #region _ReturnType Members public string TestString { get { return _teststring; } } public int TestInt { get { return _testint; } } #endregion } [Guid("73FB94E2-23EA-4e2c-A366-B234415FB4FE")] [ClassInterface(ClassInterfaceType.None)] [ProgId("COMReturnTest.TestClass")] public class TestClass : COMReturnTest._TestClass { public TestClass() { } #region _TestClass Members public COMReturnTest._ReturnType StringEmptyTest() { COMReturnTest.ReturnType rt = new COMReturnTest.ReturnType(String.Empty, 100); return rt; } public COMReturnTest._ReturnType StringNullTest() { COMReturnTest.ReturnType rt = new COMReturnTest.ReturnType(null, 100); return rt; } //[return: MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] public COMReturnTest._ReturnType StringSetTest() { COMReturnTest.ReturnType rt = new COMReturnTest.ReturnType("bugger", 100); return rt; } public String StringReturn() { return "Buggeration!!!"; } #endregion } }
-
Hi, I'm pretty new to all this interop stuff, and I've come up against a bit of an obstacle. In my method I'm trying to return an instance of another class from C# assembly. Great! Except it doesn't work. :( I keep getting Error 5 "Object variable or with block not set" in my VB client. Here's some sample code I knocked together to demonstrate... C# COM Server
using System; using System.Runtime.InteropServices; namespace COMReturnTest { [Guid("883EF833-3874-4831-9757-B44E987E80DF")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface _ReturnType { string TestString { get; } int TestInt { get; } } [Guid("29B3F02E-27A6-4535-8835-62D23BA2DA1E")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface _TestClass { COMReturnTest._ReturnType StringEmptyTest(); COMReturnTest._ReturnType StringNullTest(); COMReturnTest._ReturnType StringSetTest(); System.String StringReturn(); } [Guid("01A699E1-05ED-404e-A5B9-7C2735F97268")] [ClassInterface(ClassInterfaceType.None)] [ProgId("COMReturnTest.ReturnType")] public class ReturnType : COMReturnTest._ReturnType { #region class members private string _teststring = String.Empty; private int _testint = 0; #endregion #region class constructors public ReturnType(string TestString, int TestInt) { _teststring = TestString; _testint = TestInt; } #endregion #region _ReturnType Members public string TestString { get { return _teststring; } } public int TestInt { get { return _testint; } } #endregion } [Guid("73FB94E2-23EA-4e2c-A366-B234415FB4FE")] [ClassInterface(ClassInterfaceType.None)] [ProgId("COMReturnTest.TestClass")] public class TestClass : COMReturnTest._TestClass { public TestClass() { } #region _TestClass Members public COMReturnTest._ReturnType StringEmptyTest() { COMReturnTest.ReturnType rt = new COMReturnTest.ReturnType(String.Empty, 100); return rt; } public COMReturnTest._ReturnType StringNullTest() { COMReturnTest.ReturnType rt = new COMReturnTest.ReturnType(null, 100); return rt; } //[return: MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] public COMReturnTest._ReturnType StringSetTest() { COMReturnTest.ReturnType rt = new COMReturnTest.ReturnType("bugger", 100); return rt; } public String StringReturn() { return "Buggeration!!!"; } #endregion } }
Of course after about twenty hours looking at this I discovered what the error was immediately after posting!!! :doh: steve_hocking wrote:
_Private Sub Command3_Click() On Error GoTo Command3_Click_Err Dim tc As COMReturnTest.TestClass Dim rt As COMReturnTest.ReturnType Set tc = New COMReturnTest.TestClass_ **SET** _rt = tc.StringSetTest Command3_Click_Err: MsgBox "An error occurred" + vbCrLf + vbCrLf + "Error: 0x" + Hex(Err.Number) + vbCrLf + _ "Description: " + Err.Description + vbCrLf + "Source: " + Err.Source End Sub_
You need toSET
objects in wonderful vb... :doh: Thanks anyway, I'm just getting me coat, if you can call a taxi for me... :-O