开发者

COM component in f#

I'm C++ developer, who has to write a COM component in F#. The component should run on system with .NET 2.0. IDE is Visual Studio 2010. I'm pretty new to .NET and F#. I would like a simple sam开发者_JS百科ple of this component.


UPDATE Thanks to Daniel for specified direction. This is my answer for the question.

namespace COM_FS
open System
open System.Runtime.InteropServices

[<Guid("6E6864CC-5FB7-44B8-B6E3-A5291C9B4AC8")>]
type IExampleFS = interface
  abstract SetString : string -> unit
  abstract GetString : unit -> string
end

[<Guid("01A11F64-59E1-496F-989F-7E0CC5CE8570");
ClassInterface(ClassInterfaceType.None);
ComVisible(true)>]
type ExampleFS_Class = class
  val mutable str : string
  new () = { str = null }
  interface IExampleFS with
    member this.SetString(x) = this.str <- x
    member this.GetString() = this.str
  end
end

And I had to add AssemblyInfo.fs to the same AssemblyInfo.cs

namespace COM_FS

open System.Reflection
open System.Runtime.CompilerServices
open System.Runtime.InteropServices

module AssemblyInfo =
  [<Literal>]
  let public Version = "1.0.0.0"
  [<Literal>]
  let public FileVersion = "1.0.0.0"

[<assembly: AssemblyTitle("COM_FS")>]
[<assembly: AssemblyDescription("")>]
[<assembly: AssemblyConfiguration("")>]
[<assembly: AssemblyCompany("Mycrosoft")>]
[<assembly: AssemblyProduct("COM_FS")>]
[<assembly: AssemblyCopyright("Copyright © Mycrosoft 2011")>]
[<assembly: AssemblyTrademark("")>]
[<assembly: AssemblyCulture("")>]


[<assembly: ComVisible(true)>]
[<assembly: Guid("39ADA032-BF0F-45C9-BE4A-66AD14352F63")>]

[<assembly: AssemblyVersion(AssemblyInfo.Version)>]
[<assembly: AssemblyFileVersion(AssemblyInfo.FileVersion)>]
()

And client C++ code looks like

COM_FS::IExampleFSPtr p(__uuidof(COM_FS::ExampleFS_Class));

_bstr_t str(L"world");
p->SetString(str);
wchar_t* wstr = p->GetString();
std::wcout << wstr << std::endl;


You can translate the interface from a C# sample one-to-one, with only minor differences in syntax. It's mostly (totally?) a matter of applying attributes. Beyond that, it's not much different from coding anything else.

UPDATE

Assuming this article is correct, here's a sampler of the syntax:

open System.Runtime.InteropServices

[<Guid("694C1820-04B6-4988-928F-FD858B95C880")>]
type DBCOM_Interface =
    [<DispId(1)>]
    abstract Init : userid:string * password:string -> unit
    //...

[<Guid("47C976E0-C208-4740-AC42-41212D3C34F0"); InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>]
type DBCOM_Events = interface end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜