I still have problems making the COM Interop work. I have a C# COM DLL which is registered in the system. I can do CoInitialize and also CoCreateInstance seems to work but afterwards the functions that I call seem to not work or return the wrong results or even crash.

The complete project is here: http://www.filedropper.com/zorrotoolbox

Its quite small. One just has to compile it in Visual Studio and place the Zorro files in the include and strategy folder respectively. It would be nice to get a fully working minimal example together so we could put it in the Zorro documentation to help others.

The documentations also describes two possible ways to call the functions. Either via C++ style, or via C-style.
When i try the C-style I unfortunately get a compiler error:

Code:
#include <default.c>
#include <litec.h>
#include <com.h>
#include <ZorroToolbox.h>

int WINAPI main()
{
	HRESULT hr = CoInitialize(NULL);
	if (hr != S_OK)
	{
		printf("CoInitialize Failed: %xnn", hr);
		quit();
	} 
	else
	{
		printf("CoInitialize succeededn");	
	}
	
	IDoubleList *list = DoubleList_New();
	if (!list)
		printf("Failed to allocate objectn");
	
	// Call via C-syle call -> Compiler error
        // @temp_00582(): wrong number of parameters
        // < 	hr = list->lpVtbl->ShowMessageBox(list);	
	hr = list->lpVtbl->ShowMessageBox(list);
	
	list->Release();
	
	CoUninitialize();
}



The code looks like this:

Code:
#include <default.c>
#include <litec.h>
#include <com.h>
#include <ZorroToolbox.h>

int WINAPI main()
{
	HRESULT hr = CoInitialize(NULL);
	if (hr != S_OK)
	{
		printf("CoInitialize Failed: %xnn", hr);
		quit();
	} 
	else
	{
		printf("CoInitialize succeededn");	
	}
	
	IDoubleList *list = DoubleList_New();
	if (!list)
		printf("Failed to allocate objectn");
	
	hr = list->ShowMessageBox();
	if (hr != S_OK)
		printf("ShowMessageBox failedn");
		
	hr = list->Add(1.0);
	if (hr != S_OK)
		printf("Add failedn");
	list->Add(5.0);
	list->Add(3.0);
	list->Add(2.0);
	list->Sort();
	
	long count;
	list->Count(&count);
	printf("Count: %in", count);
	
	double value;
	list->GetItem(0, &value);
	printf("Item: %fn", value);
	
	long i;
	for (i = 0; i < count; ++i)
	{
		double value;
		list->GetItem(i, &value);
		printf("%fn", value);
	}
	
	list->Release();
	CoUninitialize();
}



and this is the ZorroToolbox.h file:

Code:
typedef struct _IDoubleListVtbl
{
	HRESULT __stdcall QueryInterface(void* This, IID *riid, void** ppvObject);
	DWORD   __stdcall AddRef(void* This);
	DWORD   __stdcall Release(void* This);

	HRESULT __stdcall Add(/*[in]*/ void *This, /*[in]*/ double value);
	HRESULT __stdcall Insert(/*[in]*/ void *This, /*[in]*/ long index, /*[in]*/ double value);
	HRESULT __stdcall Remove(/*[in]*/ void *This, /*[in]*/ double value);
	HRESULT __stdcall RemoveAt(/*[in]*/ void *This, /*[in]*/ long index);
	HRESULT __stdcall RemoveRange(/*[in]*/ void *This, /*[in]*/ long index, /*[in]*/ long count);
	HRESULT __stdcall Reverse(/*[in]*/ void *This);
	HRESULT __stdcall ReverseRange(/*[in]*/ void *This, /*[in]*/ long index, /*[in]*/ long count);
	HRESULT __stdcall SetItem(/*[in]*/ void *This, /*[in]*/ long index, /*[in]*/ double value);
	HRESULT __stdcall GetItem(/*[in]*/ void *This, /*[in]*/ long index, /*[out,retval]*/ double * pRetVal);
	HRESULT __stdcall Clear(/*[in]*/ void *This);
	HRESULT __stdcall Count(/*[in]*/ void *This, /*[out,retval]*/ long * pRetVal);
	HRESULT __stdcall Sort(/*[in]*/ void *This);

	HRESULT __stdcall ShowMessageBox(/*[in]*/ void *This);
} IDoubleListVtbl;

typedef interface IDoubleList { IDoubleListVtbl *lpVtbl; } IDoubleList;

IDoubleList* DoubleList_New()
{
	IDoubleList *pList = NULL;

	GUID clsid;
	IID iid;

	IIDFromStr("{AC9831A9-2433-409E-967F-4B74127F3B12}", &clsid);
	IIDFromStr("{EF4A8708-0FBF-4959-B8B9-FD1F1846B77A}", &iid);

	HRESULT hr = CoCreateInstance(&clsid, NULL, CLSCTX_ALL, &iid, &pList);
	return pList;
}



And this the C# Code:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZorroToolbox
{
    [ComVisible(true), Guid("EF4A8708-0FBF-4959-B8B9-FD1F1846B77A")]
    public interface IDoubleList
    {
        void Add(double value);
        void Insert(int index, double value);
        void Remove(double value);
        void RemoveAt(int index);
        void RemoveRange(int index, int count);
        void Reverse();
        void ReverseRange(int index, int count);
        void SetItem(int index, double value);
        double GetItem(int index);
        void Clear();
        int Count();
        void Sort();

        void ShowMessageBox();
    }

    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true), Guid("AC9831A9-2433-409E-967F-4B74127F3B12")]
    public class DoubleList : IDoubleList
    {
        private List<double> data = new List<double>();

        public void Add(double value)
        {
            data.Add(value);
        }

        public void Clear()
        {
            data.Clear();
        }

        public int Count()
        {
            return data.Count;
        }

        public double GetItem(int index)
        {
            return data[index];
        }

        public void Insert(int index, double value)
        {
            data.Insert(index, value);
        }

        public void Remove(double value)
        {
            data.Remove(value);
        }

        public void RemoveAt(int index)
        {
            data.RemoveAt(index);
        }

        public void RemoveRange(int index, int count)
        {
            data.RemoveRange(index, count);
        }

        public void Reverse()
        {
            data.Reverse();
        }

        public void ReverseRange(int index, int count)
        {
            data.Reverse(index, count);
        }

        public void SetItem(int index, double value)
        {
            data[index] = value;
        }

        public void Sort()
        {
            data.Sort();
        }

        public void ShowMessageBox()
        {
            MessageBox.Show("MessageBox Text");
        }
    }
}



Zorro sais the script crashes. Even the ShowMessageBox line does not work. No message box is shown.

Last edited by trenki2; 01/25/17 21:21.