简体   繁体   English

F#消耗非托管DLL函数

[英]F# consuming unmanaged dll functions

I have a project written in F# and I have to use an unmanaged DLL for part of it. 我有一个用F#编写的项目,我必须使用非托管DLL作为其中的一部分。 I'm very new to .Net programing, so this is another level of complexity added on top of everything else. 我对.Net编程非常陌生,所以这是另外一个复杂程度。

In the unmanaged DLL's documentation they provide samples of how to consume the functions from C#, like so: 在非托管DLL的文档中,他们提供了如何使用C#中的函数的示例,如下所示:

using System;
using System.Runtime.InteropServices;

[DllImport("foo.dll")] public static extern Int32 foo_init(
    [MarshalAs(UnmanagedType.AnsiBStr), In()] ref string FOOarg_Handle,
    [MarshalAs(UnmanagedType.AnsiBStr), In()] ref string FOOarg_User, 
    [MarshalAs(UnmanagedType.AnsiBStr), Out()] out string FOOarg_DataOut, 
    [MarshalAs(UnmanagedType.AnsiBStr), Out()] out string FOOarg_ErrOut);

This is pretty ugly in and of itself, but I have no clue how to do this from F#. 这本身就很丑陋,但我不知道如何从F#中做到这一点。 Wouldn't it be a good idea to just create a C# class, put all the function definitions that wrap the external DLL's functions in there, and call those from F#, instead of calling the unmanaged functions directly from F#? 仅仅创建一个C#类,将所有包含外部DLL函数的函数定义放在那里,并从F#中调用它们,而不是直接从F#调用非托管函数,这不是一个好主意吗?

How else would one go about this? 怎么会这样呢? Thanks in advance! 提前致谢!

There doesn't appear to be a description of the syntax for extern function declarations either on MSDN or in the spec. 在MSDN或规范中似乎没有关于extern函数声明的语法的描述。 But, as far as I can tell, it mirrors the C declarations verbatim, as you can see from these (rather old) examples . 但是,就我所知,它逐字地反映了C声明,正如您可以从这些(相当古老的)示例中看到的那样

I left feedback on the MSDN page for External Functions suggesting it include explanation and examples of the syntax. 我在外部函数的MSDN页面上留下了反馈,建议它包括语法的解释和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM