Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to export and use C# functions in QM?
#1
Knapsack problem

In "Knapsack problem", you explain how to export and use C/C++ functions in QM?

Please, how to do the same with C# functions?
#2
You can export your .NET classes as COM classes. Exporting static functions also possible (google for "how to export C# functions") but not so easy.

COM Interop Part 2: C# Server Tutorial

Tutorial. Using Visual Studio 2003.

If you still not have the dll project, in Visual Studio create new C# Class Library project. Lets say it is named ClassLibrary2.

In project properties, make 'Register for COM interop' True.

The cs file:
Code:
Copy      Help
using System;
using System.Runtime.InteropServices;


namespace ClassLibrary2
{
    [Guid("DBE018C4-1C61-41f3-B6A4-4E2F873D3D05")]
    public interface IManagedInterface
    {
        int Func1(int x);
    }

    [Guid("C1659361-1625-4746-931C-36014B876679")]
    public class InterfaceImplementation : IManagedInterface
    {
        public int Func1(int x)
        {
            return x;
        }
    }
}

Replace the GUIDs, because they must be unique. QM also can be used to generate GUIDs:
Macro Macro1207
Code:
Copy      Help
_s.Guid; out _s

Build the project. It creates the dll, and registers it so that it can be used as COM dll on your computer. Registration may fail if Visual Studio is not running as administrator, for example on Vista/7 if UAC is on. To run as administrator, right click the shortcut and click 'Run as administrator'.

In QM, you will have to call the functions through IDispatch.

Macro Macro1206
Code:
Copy      Help
IDispatch d._create("ClassLibrary2.InterfaceImplementation")
out d.Func1(10)

On other computers you have to register the dll using regasm.exe or this QM function:
Function RegisterNetComComponent
Code:
Copy      Help
;/
function $dllFile [flags] ;;flags: 1 unregister, 2 create type library (.tlb file)

;Registers or unregisters .NET dll as COM component.
;Error on failure.
;QM must be running as administrator.

;EXAMPLE
;RegisterNetComComponent "$desktop$\ClassLibrary2.dll"


str cl so
if(!GetNetFolder(cl)) end ES_FAILED

cl.formata("\regasm.exe ''%s'' /nologo /codebase" _s.expandpath(dllFile))
if(flags&1) cl+" /u"
else if(flags&2) cl+" /tlb"

int ec=RunConsole2(cl so)
err end _error
if(ec) end so

Function GetNetFolder
Code:
Copy      Help
;/
function! str&s

;Gets full path of .NET framework folder, like "c:\windows\microsoft.net\framework\v1.0.3705".
;Returns 1 on success, 0 on failure (eg if the computer does not have .NET).

;EXAMPLE
;str s
;if(!GetNetFolder(s)) ret
;out s


dll- mscoree #GetCORSystemDirectory @*pbuffer cchBuffer *dwlength

BSTR b.alloc(300)
int hr=GetCORSystemDirectory(b 300 &_i)
err ret
if(hr) ret
s.ansi(b)
s.rtrim("\")
ret 1
#3
Thanks Gintaras for your help. I have purchased a QM license and now I can use a lot of dll functions and libraries. I will recommend QM to my friends. Thanks again.


Forum Jump:


Users browsing this thread: 1 Guest(s)