我正在將 COM 的客戶端應用程式從 VB 重寫為 C :
VB
Private Sub Command1_Click()
Dim proxy As Person
Set proxy = New Person
proxy.FirstName = "Maxim"
proxy.LastName = "Donax"
proxy.Persist ("C:\myFile.xml")
End Sub
C
#import "COM.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
#include <iostream>
int main()
{
Person per; // error 0070
per.FirstName = "Maxim"; // error 3365
per.LastName = "Donax";// error 3365
per.Persist(" C:\myFile.xml ");// error 3365
}
我收到錯誤 E0070:不允許不完整的型別,這本身會產生錯誤 3365:下一個字串中不允許使用類“人”的不完整型別
我明白我做錯了什么,但我找不到正確的解決方案。請幫忙。
COM.tlb:
using System.Xml.Serialization;
using System.IO;
using System.Runtime.InteropServices;
namespace COM
{
[ClassInterface(ClassInterfaceType.None)]
public class Person : System.EnterpriseServices.ServicedComponent, IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsMale { get; set; }
public void Persist(string FilePath)
{
StreamWriter oFile = new StreamWriter(FilePath);
XmlSerializer oXmlSerializer = new XmlSerializer(typeof(Person));
oXmlSerializer.Serialize(oFile, this);
oFile.Flush();
oFile.Close();
}
static public Person Retrieve(string FilePath)
{
StreamReader oFile = new StreamReader(FilePath);
XmlSerializer oXmlSerilizer = new XmlSerializer(typeof(Person));
Person oPerson = oXmlSerilizer.Deserialize(oFile) as Person;
return oPerson;
}
}
}
IPerson.cs
using System;
namespace COM
{
public interface IPerson
{
string FirstName { get; set; }
bool IsMale { get; set; }
string LastName { get; set; }
void Persist(string FilePath);
}
}
uj5u.com熱心網友回復:
你必須學習一點 C 才能做到這一點。
也只需查看Visual Studio 在輸出檔案夾中生成的.tlh(可能還有.tli)檔案。
所以,這里是如何實體化和呼叫 COM 物件(例如使用來自 comdef.h 的 Windows SDK 工具/包裝器_bstr_t):
#include <windows.h>
#import "COM.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
int main()
{
CoInitialize(NULL);
{
IPersonPtr per(__uuidof(Person)); // equivalent of CoCreateInstance(CLSID_Person,..., IID_IPerson, &person)
per->put_FirstName(_bstr_t(L"Maxim"));
per->put_LastName(_bstr_t(L"Donax"));
per->Persist(_bstr_t(L"c:\\myFile.xml"));
}
CoUninitialize();
return 0;
}
或者更簡單一點(使用方便的自動生成的包裝器):
#include <windows.h>
#import "COM.tlb"
using namespace COM; // your namespace, COM is probably not a good namespace name
int main()
{
CoInitialize(NULL);
{
IPersonPtr per(__uuidof(Person));
per->PutFirstName(L"Maxim");
per->PutLastName(L"Donax");
per->Persist(L"c:\\myFile.xml");
}
CoUninitialize();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351528.html
上一篇:從資料表中洗掉多于一行
