Alguien una vez dijo que no es cuestion de cambiarse de un lenguaje a otro sino de usar la herramienta adecuada al problema que se desea resolver. C# es excelente para muchas cosas en las que VFP no es tan bueno, por lo que el poder crear un DLL en C# y usarlo en VFP se convierte en algo muy poderoso.
Aqui les dejo todo un conjunto de tips para lograr justamente esto:
1. En tu clase C# debes incluir el namespace System.Runtime.InteropServices;
2. La clase que deseas accesar desde VFP debe estar declarada como public y tener estas directivas: ? 1 2 [ClassInterface(ClassInterfaceType.AutoDual)] [ProgId("className")]
donde "className" es el nombre OLE de la clase, es decir, el que usaras en el CREATEOBJECT en VFP.
3 Los metodos de la clase que quieras accesar desde VFP deben estar declarados como public y no ser estaticos.
4. Los metodos publicos de la clase no pueden tener sobrecargas, es decir, multiples versiones del mismo metodo con diferentes parametros.
5. Si un metodo devuelve un tipo estructurado, este tipo debe ser creado como class (no como struct) y cumplir con todas las condiciones indicadas en los puntos 1, 2, 3 y 4.
6. Si un metodo devuelve un array de cualquier tipo (ej, string[]), VFP lo recibe como un tipo ARRAY y no como un objeto (como lo es en .NET), por lo que no podremos acceder a las propiedaes del array, tal como Lenght, pero si a sus items: ? 1 2 3 4 5 6 7 8 9 10 11 12 aItems = myClass.MyMethod() FOR EACH oItem IN aItems ?oItem.Property ENDFOR
o
aItems = myClass.MyMethod() FOR i = 1 TO ALEN(aItems,1) oItem = aItems[i] ?oItem.Property ENDFOR
7. Antes de compilar la clase, debes ir a las propiedades del proyecto, Application, boton Assembly Information y marcar la casilla "Make assembly COM-visible"
8. Debes firmar la DLL. Para esto, sigue los pasos indicados aqui
9. Una vez compilada la dll, la misma debe ser registrada de la siguiente forma antes de poder ser usada en VFP:
Windows 32 bits: C:\WINDOWS\microsoft.net\framework\v2.0.50727\regasm mylib.dll /register /codebase /tlb
Windows 64 bits C:\WINDOWS\microsoft.net\framework64\v2.0.50727\regasm mylib.dll /register /codebase /tlb
Sign a .NET Assembly with a Strong Name Without Recompiling
Signing a .NET assembly with a strong name is easy in Visual Studio. However, what if this is a 3rd party assembly and you don't have the source?
For me, I have an application that has a requirement that all assemblies are signed with a strong name. One of the assemblies I am using is RestSharp. I like to contribute to RestSharp and I didn't want to modify the project file to sign the assembly as I didn't want that to go back to the repository when I had some changes to contribute.
Not a problem. What I have is a batch file that disassembles the DLL to IL and then reassembles the IL back into a DLL and includes my key file. This way I get to keep the original DLL for projects that I don't need the assembly to have a strong name and a separate one that is signed with the strong name for the projects where I need that.
Here's what I did: 1. In the Release build folder I created the following: o The key file (create using sn.exe -k MyPublicPrivateKeyFile.snk) o A subfolder to contain the signed assembly, mine is named "Signed" o A batch file (see below) 2. Create a batch file in the Release folder named "SignAssembly.bat" with the following contents: del .\Signed\RestSharp.* /F "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm.exe" .\RestSharp.dll /out:.\Signed\RestSharp.il "C:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm.exe" .\Signed\RestSharp.il /dll /key=.\RestSharp.snk /output=.\Signed\RestSharp.dll
pause The first line deletes any previously signed assembly. Second line uses ILDASM to disassemble the DLL to IL in the "Signed" folder as RestSharp.il. The third line reassembles the IL into an assembly using ILASM and tells it to use the IL file and the key file. That's it.
Now, whenever I do a new release build, I just run the batch file and I have a signed copy to use, and I did it without changing the project file to do it.