Part two: in. NET in the use of VFP written COM components
Fox can be flexible, intuitive, fast processing data, making it very suitable for application at the level of business logic. Starting with Visual FoxPro 5, Microsoft has been strengthening the development capabilities of the visual FoxPro COM components. Briefly summarize the features of visual FoxPro in COM component writing:
New Run-time Library: VFPnT.DLL (n represents version number). In this runtime, a lot of old and interface control elements are removed, smaller, more stable, and faster.
A new Session object. Provides good isolation in multi-threaded state.
Built-in support for XML facilitates the delivery of recordsets (cursors) in multi-tier applications.
The Execscript () function provides the ability to execute multiple lines of statements dynamically.
Fully supports COM + standards.
Writing COM components with Fox is a pleasant thing to do. NET provides call support for COM components, so we'll be happy in. NET continues.
Writing a COM component in Visual FoxPro: (code#5)
DEFINE CLASS Customer as Foxbaseclass olepublic
* Inherit to the custom FoxBase class
* Use the Olepublic keyword when declaring, telling the compiler that a COM component is defined
PROCEDURE Init ()
This. DATABASEPATH=SUBSTR (SYS), 1,rat ("\", SYS (2)) + "Data\"
Dodefault ()
Endproc
* Getcountry () Return the customer said in the list of countries, the results are expressed in XML
FUNCTION getcountry () as String
Local cXml as String
SELECT Distinc (country) as country from customer into CURSOR Temp
Cursortoxml ("Temp", "cXml", 1,16,0, "1")
Use in Temp
Return CXML
Endfunc
* GetCustomer () returns a list of customers in the specified country, with the results expressed in XML
FUNCTION GetCustomer (Ccountry As String) as String
Local cXml as String
SELECT * FROM Customer WHERE country=m.ccountry into CURSOR Temp
Cursortoxml ("Temp", "cXml", 1,16,0, "1")
Use in Temp
Return CXML
Endfunc
* Total net sales of designated countries. This is achieved by calling the stored procedure in DBC
FUNCTION getsumordernet (Ccountry as String) as Double
Return Get_sum_order_net (M.ccountry)
Endfunc
* The interface to write to the log. This is achieved by calling the stored procedure in DBC
FUNCTION LogIt (cmsg as String) as Boolean
Logmsg (CMSG)
Return. T.
Endfunc
Enddefine
Next, we're going to compile the code and build a COM component. If you put the above code in a file called FOXCLASS.PRG, you can use the following statement to get the in-process COM component: (code#6)
* Put FOXCLASS.PRG into a project named Foxwebservice, and if the project does not exist, build the project
Build PROJECT Foxwebservice from Foxclass
* Compile the Foxwebservice project into a COM component (Foxwenservice.dll) and register it in the system.
Build Mtdll Foxwebservice from Foxwebservice recompile
In. NET, if you want to reference an unmanaged COM component, you must provide a. NET definition for unmanaged COM. You can use the TlbImp utility in the. NET SDK to convert an unmanaged COM type library into. NET metadata, which is actually a container class that can be accessed by any managed code, wrapping the original unmanaged code. (code#7)
At the command prompt, follow the following statement:
Converts the H:\NET\fox_com\foxwebservice.dll into. NET, stored in the H:\NET\ Foxwebservice.dll
Tlbimp/out:net_fox_com.dll Foxwebservice.dll
If you are not sure how TlbImp arranges net_fox_com objects, use the Ildasm.exe tools provided with the. NET SDK to view the structure of the net_fox_com.dll. In this way, we can use the COM component we just wrote in the C # program: (CODE#8)
Net_fox_com. Customerclass obis= New net_fox_com. Customerclass ();
Obis. Getcountry ();
Obis. Getsumordernet ("UK");
If the C # program uses the classes in net_fox_com, you can compile the C # code so that the code is executed at the command prompt: (code#9)
Csc/t:winexe Com_client_sample.cs/r:net_fox_com.dll
The special point is to add a reference to the Net_fox_com.dll. It is also important to note that the Net_fox_com.dll file should be placed in the. NET search path, otherwise the application will be executed with an error. My practice is to put the exe file in the same directory as Net_fox_com.dll, and if the COM component changes, you must regenerate the Net_fox_com.dll and recompile the C # code.
. NET's smooth transition to COM system reflects Microsoft's respect for developers. Try it and you'll find it all so simple ...