Home > Enterprise >  Rewrite application from VB to C and get error 0070
Rewrite application from VB to C and get error 0070

Time:11-07

I am rewriting the client application for COM from VB to 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
}

I get an error E0070:Incomplete type not allowed, which itself create errors 3365: Incomplete type not allowed of class "Person" in next strings

I understand what I am doing wrong, but I cannot find the right solution. Help please.

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);
    }
}

CodePudding user response:

You'll have to learn a bit of C to be able to do this.

Also just have a look at the .tlh (and possibly .tli) file that was generated in the output folder by Visual Studio.

So, here is how to instantiate and call the COM object (for example using the Windows SDK tools/wrappers from comdef.h like _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;
}

Or a bit more simple (use convenient wrappers automagically generated):

#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;
}
  • Related