PDA

View Full Version : .NET COM in Lianja - Workaround



josipradnik
2015-02-06, 08:53
There have been one thread ("Calling a method on a .NET COM Object") ending:

COM/ActiveX is working fine (We just tested against Excel). This is a .net specific COM issue.
http://www.lianja.com/community/showthread.php?1250-ActiveX-Controls-Does-not-Recognize-License

After digging C# find nothing suspicious there. VFP is calling the same .NET COM quit fine. Declaration in C# like this is enough for VFP to work with it. In Lianja it does not work.

[ComVisible(true)]

In Lianja same .NET object is created but every property is .F. and methods can not be called from Lianja. I can accept that creating such an object from Excel works. Maybe activex is another story.
But I have stuff made inside .NET DLLs which I do not want to re-program in Lianja (desktop app). And am not sure if it can be done yet (fiscalization and wild XML structures). That ,NET stuff is called within my VFP application without any problem. There is an article in VFP Help ("Visual FoxPro and Advanced COM") which helped me to make the workaround wrapper. Until Lianja team find solution for calling .NET methods.

Step #1
In Visual Foxpro you need to create a PRG called VFPCOM.PRG and save it.


define class VFPCOM as session olepublic
procedure docmd(ccmd as string) as variant ;
helpstring "Execute a VFP cmd"
&ccmd && just execute parm as if it were a fox command
function evalexpr(cexpr as string) ;
helpstring "Evaluate a VFP expression"
return &cexpr && evaluate parm as if it were a fox expr
function error(nerror, cmethod, nline)
comreturnerror(cmethod+' err#='+str(nerror,5)+;
' line='+str(nline,6)+' '+message(),_vfp.servername)
function dotnet(cdotnet as string, cproporfunc as string) ;
helpstring "Evaluate a .NET function or propery"
rr=createobject(cdotnet)
kk="rr."+cproporfunc
return &kk
enddefine

Step #2
Create another PRG in VFP (BUILDVFPCOM.PRG) and save it


*SET STEP ON
IF FILE("vfpcom.dll")
DECLARE integer DllUnregisterServer IN vfpcom.dll
DllUnregisterServer()
CLEAR DLLS
ENDIF
BUILD PROJECT vfpcomserver FROM vfpcom.prg
BUILD DLL vfpcomserver from vfpcomserver recomp
*now test this COM server:


ox = CreateObject("vfpcomserver.vfpcom") && create the server object
?"Time from server:"+ox.evalexpr("time()") && get the record count

Step #3
Run this second prg ( ! ). It will build DLL and test it.
Now you can play with it in VFP Command Window with 3 methods exposed.
977

We are here because of "dotnet". Others are for VFP stuff.


ox = CreateObject("vfpcomserver.vfpcom") && create the server object
?ox.dotnet('PLUS.Lider.LiderConfig','url2')
?ox.dotnet('PLUS.Lider.Client.LiderClient','DOTNET _buildverzija()')
?ox.dotnet('PLUS.Lider.Client.LiderClient','GetGUI D()')

First parameter is .NET class
Second is .NET class property or method

Step #4
After you get the calling .NET works in VFP, it is time to move to Lianja. In Lianja Console:


ed myvfpcomscript
and write something similar to my .NET (DLLs with [ComVisible(true)] and properly built)


ox = CreateObject("vfpcomserver.vfpcom") && create the server object
?ox.evalexpr("TIME()")
?ox.dotnet('PLUS.Lider.LiderConfig','url2')
?ox.dotnet('PLUS.Lider.Client.LiderClient','DOTNET _buildverzija()')
?ox.dotnet('PLUS.Lider.Client.LiderClient','GetGUI D()')

Then in Console

do myvfpcomscript

Or you can execute line by line.

Josip

barrymavin
2015-02-06, 11:30
If the .net DLL is properly built for COM interop then SET DEBUG ON and look in debug_client.txt to see what methods are being exposed.

Also make sure the .net dll is registered and that it is 32 bit as Lianja runs as a 32 bit application.

You should also try accessing it using its GUID {....} and experiment to see which access method works best after registering the dll with regasm.

barrymavin
2015-02-06, 18:10
My initial thoughts on this are that the .net dll is not exposing the methods as they are missing the correct decorators.

At first I thought maybe it was because it is a non UI object but then VFP OLEDB works fine so I ruled that out.

You will need to investigate the .net dll and how it is being built and registered on the machine.

This thread in stack overflow may help you create a .net dll that is properly enabled for COM Interop.
http://stackoverflow.com/questions/7092553/turn-a-simple-c-sharp-dll-into-a-com-interop-component

EDIT: ComVisible(true) needs to be on the class AND the methods.
http://blogs.msdn.com/b/asiatech/archive/2011/12/05/how-to-develop-and-deploy-activex-control-in-c.aspx