C# 86: 我使用的是InstallSheild 11.5,没有看到“.NET COM互操作”选项。11.5附带的帮助文件确实说它应该在那里,所以我不知道我只是看错了还是怎么了。您可以更新您的答案以包括该属性的设置位置吗?使用InstallShield 201
86:C# 86: 我使用的是InstallSheild 11.5,没有看到“.NET COM互操作”选项。11.5附带的帮助文件确实说它应该在那里,所以我不知道我只是看错了还是怎么了。您可以更新您的答案以包括该属性的设置位置吗?使用InstallShield 201,c#,.net,com,interop,com-interop,C#,.net,Com,Interop,Com Interop,86: 我使用的是InstallSheild 11.5,没有看到“.NET COM互操作”选项。11.5附带的帮助文件确实说它应该在那里,所以我不知道我只是看错了还是怎么了。您可以更新您的答案以包括该属性的设置位置吗?使用InstallShield 2011,在组织/组件树中有一个.NET设置部分,其中包括.NET COM互操作设置。您的答案非常有用。当我尝试在VB6 IDE中引用DLL时,我收到错误:“无法添加对指定文件的引用”。我应该引用DLL还是TLB文件?回答太好了,我错过了-TLB-c
我使用的是InstallSheild 11.5,没有看到“.NET COM互操作”选项。11.5附带的帮助文件确实说它应该在那里,所以我不知道我只是看错了还是怎么了。您可以更新您的答案以包括该属性的设置位置吗?使用InstallShield 2011,在组织/组件树中有一个.NET设置部分,其中包括.NET COM互操作设置。您的答案非常有用。当我尝试在VB6 IDE中引用DLL时,我收到错误:“无法添加对指定文件的引用”。我应该引用DLL还是TLB文件?回答太好了,我错过了
-TLB-codebase
上的部分。在我找到这个之前,我的头在这上面撞了几个小时。谢谢谢谢你的例子!如果您试图通过CreateObject
访问该类,那么如何在VB6中调用GetDellAsset
?@Panzercrisis Hi,看看这个@Matas Vaitkevicius。您可以提供一些资源或使用事件的示例吗。因为我不知道如何处理COM事件。我在这里写了一个问题
RegAsm.exe -tlb -codebase mydll.dll
public class ApiCaller
{
public DellAsset GetDellAsset(string serviceTag, string apiKey)
{
....
}
}
public class DellAsset
{
public string CountryLookupCode { get; set; }
public string CustomerNumber { get; set; }
public bool IsDuplicate { get; set; }
public string ItemClassCode { get; set; }
public string LocalChannel { get; set; }
public string MachineDescription { get; set; }
public string OrderNumber { get; set; }
public string ParentServiceTag { get; set; }
public string ServiceTag { get; set; }
public string ShipDate { get; set; }
}
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
[ComVisible(true)]
public interface IComClassApiCaller
{
IComClassDellAsset GetDellAsset(string serviceTag, string apiKey);
}
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IComClassApiCallerEvents
{
}
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComClassApiCallerEvents))]
[ComVisible(true)]
[ProgId("ProgId.ApiCaller")]
public class ApiCaller : IComClassApiCaller {
public IComClassDellAsset GetDellAsset(string serviceTag, string apiKey)
{
.....
}
}
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83E")]
[ComVisible(true)]
public interface IComClassDellAsset
{
string CountryLookupCode { get; set; }
string CustomerNumber { get; set; }
bool IsDuplicate { get; set; }
string ItemClassCode { get; set; }
string LocalChannel { get; set; }
string MachineDescription { get; set; }
string OrderNumber { get; set; }
string ParentServiceTag { get; set; }
string ServiceTag { get; set; }
string ShipDate { get; set; }
}
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA70"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IComClassDellAssetEvents
{
}
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F937"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComClassDellAssetEvents))]
[ComVisible(true)]
[ProgId("ProgId.DellAsset")]
public class DellAsset : IComClassDellAsset
{
public string CountryLookupCode { get; set; }
public string CustomerNumber { get; set; }
public bool IsDuplicate { get; set; }
public string ItemClassCode { get; set; }
public string LocalChannel { get; set; }
public string MachineDescription { get; set; }
public string OrderNumber { get; set; }
public string ParentServiceTag { get; set; }
public string ServiceTag { get; set; }
public string ShipDate { get; set; }
}
//Account.cs
using System.Runtime.InteropServices;
namespace BankServerCSharp
{
[ComVisible(true)] // This is mandatory.
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IAccount
{
double Balance { get; } // A property
void Deposit(double b); // A method
}
[ComVisible(true)] // This is mandatory.
[ClassInterface(ClassInterfaceType.None)]
public class Account:IAccount
{
private double mBalance = 0;
private Account() { } // private constructor, coclass noncreatable
public static Account MakeAccount() { return new Account(); }
//MakeAccount is not exposed to COM, but can be used by other classes
public double Balance { get { return mBalance; } }
public void Deposit(double b) { mBalance += b; }
}
}
//Bank.cs
using System.Runtime.InteropServices;
namespace BankServerCSharp
{
[ComVisible(true)] // This is mandatory.
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IBank
{
string BankName { get; set; } // A property
IAccount FirstAccount { get; } // Another one of type IDispatch
}
[ComVisible(true)] // This is mandatory.
[ClassInterface(ClassInterfaceType.None)]
public class Bank:IBank
{
private string Name = "";
private readonly Account First;
public Bank() { First = Account.MakeAccount(); }
public string BankName {
get { return Name; }
set { Name= value; }
}
public IAccount FirstAccount {
get { return First; }
}
}
}
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /tlb /codebase "$(TargetPath)"
%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe /tlb /codebase "$(TargetPath)"
Sub TestSOExampleNew()
On Error GoTo Oops
Dim BiBiBaBa As New BankServerCSharp.Bank 'New!
BiBiBaBa.BankName = "Big Bird Bad Bank"
Dim Account As BankServerCSharp.Account 'No New!
Set Account = BiBiBaBa.FirstAccount
Account.Deposit 2000
MsgBox BiBiBaBa.BankName & ". First client's balance: " & Account.Balance
Exit Sub
Oops:
MsgBox "Sorry, an unexpected error occurred!"
End Sub
#include "stdafx.h"
#import "D:\Aktuell\CSharpProjects\BankServerCSharp\BankServerCSharp\bin\Release\BankServerCSharp.tlb"
//this is the path of my C# project's bin\Release folder
inline void TESTHR(HRESULT x) { if FAILED(x) _com_issue_error(x); };
int main()
{
try
{
TESTHR(CoInitialize(0));
BankServerCSharp::IBankPtr BankPtr = nullptr;
TESTHR(BankPtr.CreateInstance("BankServerCSharp.Bank"));
BankPtr->BankName = L"Ernie First Global Bank";
BankServerCSharp::IAccountPtr AccountPtr = BankPtr->FirstAccount;
TESTHR(AccountPtr->Deposit(200.09));
wprintf(L"Name: %s, Balance: %.2f\n", (LPCWSTR)BankPtr->BankName, AccountPtr->Balance);
}
catch (const _com_error& e)
{
CStringW out;
out.Format(L"Exception occurred. HR = %lx, error = %s", e.Error(), e.ErrorMessage());
MessageBoxW(NULL, out, L"Error", MB_OK);
}
CoUninitialize();// Uninitialize COM
return 0;
}