{"id":175,"date":"2016-02-26T09:09:29","date_gmt":"2016-02-26T14:09:29","guid":{"rendered":"http:\/\/salzlechner.com\/dev\/?p=175"},"modified":"2016-03-11T07:05:00","modified_gmt":"2016-03-11T12:05:00","slug":"interfacing-dataflex-net-and-com-interop","status":"publish","type":"post","link":"http:\/\/salzlechner.com\/dev\/2016\/02\/26\/interfacing-dataflex-net-and-com-interop\/","title":{"rendered":"Interfacing DataFlex &#8211; .NET and COM Interop"},"content":{"rendered":"<p>DataFlex is not based on the .NET Framework. That means we cannot directly interface with .NET components. Luckily Microsoft added a technology called COM Interop which allows both a COM based system to call the .NET component and the .NET component to consume COM components.<\/p>\n<p>In our case we want to be able to use a COM Interface which is supported by DataFlex to consume a .NET component.<\/p>\n<h2>But WHY?<\/h2>\n<p>Why would we want to do this in the first place?<\/p>\n<p>A lot of things are often easier to do in .NET or a library already exists that can be used but very often now these libraries are not available any longer as ActiveX controls.<\/p>\n<p>So in order to show how to consume a .NET component we need to first create a .NET component. We will use Microsoft Visual Studio to create one<\/p>\n<p>First we need to launch Visual Studio. Because we want Visual Studio to be able to make changes to the computers registry it would be a good idea to start it as Administrator<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/startvstudio.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-180\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/startvstudio-300x187.png\" alt=\"startvstudio\" width=\"300\" height=\"187\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/startvstudio-300x187.png 300w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/startvstudio.png 352w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Once Visual Studio is started we need to create a new project<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/newproject.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-182 size-full\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/newproject.png\" alt=\"newproject\" width=\"949\" height=\"657\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/newproject.png 949w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/newproject-300x208.png 300w\" sizes=\"(max-width: 949px) 100vw, 949px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>My preferred language is C# but you can do the same in Visual Basic of course. Select a Class Library project and type in a name and location<\/p>\n<p>Visual Studio will create a project with an empty class called Class1 for you<\/p>\n<p>First you want to rename your class and probably also the code file (Class1.cs) or simply delete the class1 file and add a new one with a proper name<\/p>\n<p>Also at the top above\u00a0the class you will need to add a using statement for the\u00a0System.Runtime.InteropServices which will be needed to enable COM Interop<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/newclass.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-187 size-full\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/newclass.png\" alt=\"newclass\" width=\"327\" height=\"237\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/newclass.png 327w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/newclass-300x217.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>In order to create and use this component we need two main parts.<\/p>\n<p>A class implementing the functionality of our component and a so-called Interface that is\u00a0a contract\u00a0describing all the methods and properties that will be available in\u00a0an object created from this class<\/p>\n<p>We already have an empty class now lets add an empty Interface definition<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/interfacedef.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-188\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/interfacedef.png\" alt=\"interfacedef\" width=\"297\" height=\"94\" \/><\/a><\/p>\n<p>as you can see right above the class definition (inside the namespace declaration) we define a public interface<\/p>\n<p>The next step is to tell Visual Studio about this interface. We can do this by using metadata. In that metadata we have to do the following<\/p>\n<ul>\n<li>tell Visual Studio that the following interface should be visible as a COM component<\/li>\n<li>define a GUID for the interface<\/li>\n<li>define the\u00a0Interface type for our interface<\/li>\n<\/ul>\n<p>Whats a GUID?<\/p>\n<p>A GUID is a globally unique identifier. It a 128-bit value generated from random numbers and most often displayed as 32 hexadecimals digits. COM uses GUIDs as unique identifiers for components<\/p>\n<p>Where do we get these GUIDs. The beauty of GUIDs is that we can programatically generate them at any time. Visual Studio has a little utility built in to handle this task for us.<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/toolsguid.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-189\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/toolsguid-300x267.png\" alt=\"toolsguid\" width=\"300\" height=\"267\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/toolsguid-300x267.png 300w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/toolsguid.png 415w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>From the tools Menu select Create GUID<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/createguid.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-190\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/createguid-288x300.png\" alt=\"createguid\" width=\"288\" height=\"300\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/createguid-288x300.png 288w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/createguid.png 391w\" sizes=\"(max-width: 288px) 100vw, 288px\" \/><\/a><\/p>\n<p>The Create GUID dialog will be shown. In this case we will need format #5. Select Format #5 and then click New GUID and then click copy to copy the new GUID to the clipboard<\/p>\n<p>Now lets add the metadata<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/metadataintf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-191 size-full\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/metadataintf.png\" alt=\"metadataintf\" width=\"443\" height=\"158\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/metadataintf.png 443w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/metadataintf-300x107.png 300w\" sizes=\"(max-width: 443px) 100vw, 443px\" \/><\/a><\/p>\n<p>We add a line that tells Visual Studio to make this interface visible as a COM component<\/p>\n<p>then the next line we add the GUID generated earlier. This is our interfaces unique identifier and will be used to register the interface.<\/p>\n<p>The InterfaceType is defined as an IDispatch interface.<\/p>\n<p>Now lets work on the implementation of our class<\/p>\n<p>Our class needs to inherit from the interface. In addition we also need to add metadata to the class.<\/p>\n<p>The class also needs to be set to ComVisible and we need another GUID for our class.<\/p>\n<p>In addition we define the class interface type and assign a ProgId to our class<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/metadataclass.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-192\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/metadataclass.png\" alt=\"metadataclass\" width=\"458\" height=\"138\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/metadataclass.png 458w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/metadataclass-300x90.png 300w\" sizes=\"(max-width: 458px) 100vw, 458px\" \/><\/a><\/p>\n<p>To make it easier on us we can tell Visual Studio to automatically register our component for COM Interop when the project is built<\/p>\n<p>You can simply open the project build\u00a0properties and enable to Register for COM Interop option<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/regcominterop.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-193\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/regcominterop.png\" alt=\"regcominterop\" width=\"633\" height=\"168\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/regcominterop.png 633w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/regcominterop-300x80.png 300w\" sizes=\"(max-width: 633px) 100vw, 633px\" \/><\/a><\/p>\n<p>You do not have to do the above step. You can also manually register the component using the REGASM utility. In order to register your component using REGASM simply start a Visual Studio command prompt as Adminstrator and execute the following<\/p>\n<p>REGASM dllfilename.dll \u00a0\/tlb \/codebase<\/p>\n<p>The codebase switch is not required nor should it be used when deploying but it makes life easier on the development machine<\/p>\n<p>At this point our full source should look as follows<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/fullsource1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-194\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/fullsource1.png\" alt=\"fullsource1\" width=\"491\" height=\"450\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/fullsource1.png 491w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/fullsource1-300x275.png 300w\" sizes=\"(max-width: 491px) 100vw, 491px\" \/><\/a><\/p>\n<p>Now we build the project in Visual Studio which will build our component and register it for COM Interop<\/p>\n<p>Now in our DataFlex project we can import the component<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/dfimport.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-195\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/dfimport.png\" alt=\"dfimport\" width=\"725\" height=\"364\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/dfimport.png 725w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/dfimport-300x151.png 300w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><\/a><\/p>\n<p>On the import Automation library dialog browse to the folder containing your dll and tlb file and select the tlb file created by\u00a0Visual Studio<\/p>\n<p>Once selected the Studio will generate a DataFlex class package based on the interface published in our Visual Studio project<\/p>\n<p>Create a new view to test the component<\/p>\n<p>At the top of the view use our class and add a Button to the view<\/p>\n<p>We can add our COM object to the view and then in the OnClick procedure of the button we can call the\u00a0CreateComObject method to instantiate the COM object<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/testview1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-196\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/testview1.png\" alt=\"testview1\" width=\"509\" height=\"455\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/testview1.png 509w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/testview1-300x268.png 300w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/><\/a><\/p>\n<p>Of course at this point our class is somewhat boring so we need to add actual functionality to our class. Back to Visual Studio<\/p>\n<p>Our first major\u00a0feature will be the Hello function. We want to function to accept a name as a parameter and then let it return a new string saying\u00a0something like &#8216;Hello name, how are you?&#8217;<\/p>\n<p>In order to do this we need to do 2 things<\/p>\n<ul>\n<li>Add the function declaration to our interface<\/li>\n<li>Add the function implementation to our class<\/li>\n<\/ul>\n<p>First in the interface we add the declaration<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/functionintf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-197\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/functionintf.png\" alt=\"functionintf\" width=\"427\" height=\"140\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/functionintf.png 427w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/functionintf-300x98.png 300w\" sizes=\"(max-width: 427px) 100vw, 427px\" \/><\/a><\/p>\n<p>as you can see above we declare a function called Hello that returns a string and accepts a string as a parameter<\/p>\n<p>above the function declaration you can see a metadata line. We are assinging a DispId\u00a0of 1. The DispId is the numeric identifier for this method in the COM component.<\/p>\n<p>This line is not required because Visual Studio can automate assignment of DispIds but we prefer controlling our own DispIds.<\/p>\n<p>Next we need to implement the actual method<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/functionclass.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-198\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/functionclass.png\" alt=\"functionclass\" width=\"478\" height=\"186\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/functionclass.png 478w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/functionclass-300x117.png 300w\" sizes=\"(max-width: 478px) 100vw, 478px\" \/><\/a><\/p>\n<p>Here we can see a simple method accepting a string parameter called name. It simply returns a string that says &#8216;Hello name, how are you&#8217;.<\/p>\n<p>Now we can rebuild our Visual Studio project<\/p>\n<p>Then back in DataFlex because our interface has changed we will need to regenerate the class for our COM component<\/p>\n<p>After regenerating the class add a form called oName and add another button to your test view<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/hellobtn.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-199\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/hellobtn.png\" alt=\"hellobtn\" width=\"616\" height=\"212\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/hellobtn.png 616w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/hellobtn-300x103.png 300w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/a><\/p>\n<p>In the OnClick method we call the ComHello method in our com component passing it the value of our oName form. Then we use the Info_Box method to show the return value<\/p>\n<p>Make sure to build your Visual Studio project as a 32bit project.<\/p>\n<p>When deploying your components either use REGASM to register or you can use registration free COM<\/p>\n<p>some libraries we have built using this technique include<\/p>\n<ul>\n<li>SQL Query library<br \/>\nsimilar to the built in SQL commands but faster and allows us the use of\u00a0parameterized queries<\/li>\n<li>ODBC Query library<br \/>\nsame as the SQL Query library but for ODBC<\/li>\n<li>MetaPhone library<\/li>\n<li>File IO<\/li>\n<li>PDF Library<\/li>\n<li>JSON Parser<\/li>\n<\/ul>\n<p>and more<\/p>\n<p>&nbsp;<\/p>\n\n\t\t<div class='author-shortcodes'>\n\t\t\t<div class='author-inner'>\n\t\t\t\t<div class='author-image'>\n\t\t\t<img src='http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/mike5crop-566174_60x60.jpg' alt='' \/>\n\t\t\t<div class='author-overlay'><\/div>\n\t\t<\/div> \n\t\t<div class='author-info'>\n\t\t\tMichael Salzlechner is the CEO of StarZen Technologies, Inc.<\/p>\n<p>He was part of the Windows Team at Data Access Worldwide that created the DataFlex for Windows Product before joining StarZen Technologies.<\/p>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A tutorial to show how to build a .NET component and then use it as a COM component within a DataFlex application<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","ngg_post_thumbnail":0,"footnotes":""},"categories":[7,6,27,4],"tags":[],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-c","category-dataflex","category-dataflex-webapp","category-windows"],"_links":{"self":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":12,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":277,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/175\/revisions\/277"}],"wp:attachment":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}