c# - 如何从 C++/C# 调用此 DLL 函数

标签 c# c++ dll pinvoke

我花了很多时间试图解决这个问题,所以我想我可以在这里得到一些帮助。 基本上我有一个在 IDL 中这样声明的 DLL 函数:

[id(1), helpstring("method findFile")] HRESULT findFile(
    [in] BSTR fileName,
    [out] LONG* someValue
    );

我究竟如何从 C++/C# 声明和调用?

注意:有一个VB6应用程序成功调用了该函数。声明是:

 Private Declare Function findFile Lib "thedll.dll" ( _
    ByVal fileName As String, _
    ByRef someValueAs Long _
  )

电话:

Dim a As String
Dim b As Long
Dim r As long

a = "image.jpg"
b = -1
r = findFile(a, b)

附录:

我不能保证 VB6 代码看起来像那样,因为我有可执行文件,我只被告知那部分看起来像什么,所以也许你们是对的,但它不匹配。我确实编写了 C++ DLL,现在我需要自己整理一些成功调用 DLL 的代码,以便尝试一些东西而不依赖于那个 exe。

DLL 函数的 C++ 实现如下所示:

STDMETHODIMP CFinder::findFile(BSTR fileName, LONG* someValue)
{
    *someValue = 8;

    return S_OK;
}

最佳答案

未经测试的 C# 声明:

[DllImport("thedll.dll", SetLastError=true)]
static extern int findFile([MarshalAs(UnmanagedType.BStr)]string fileName, out int someValue);

关于c# - 如何从 C++/C# 调用此 DLL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8568493/

相关文章:

c# - 如何将自定义列添加到我的 Serilog MS SQL Server 接收器?

c# - 单击时部分选择 DataGridView 单元格的文本

c++ - 使用 shared_ptr boost 序列化而不在指向类中实现 serialize() 函数

c++ - 如何返回指向对象的指针以便在访问对象方法的函数中使用?

c++ - 简单递归题

validation - Windows DLL 是否由 Microsoft 签名?我可以检测它们是否被第三方篡改过吗?

C++ .dll 在 Visual Studio 中添加 - 另一个 "unresolved external symbol"

c# - 升级到 asp.net core 2.2 后为空 href

c# - 在 SQL Server 2005 中选择具有特定月份和年份的记录

C++ VS : 'new' and 'delete' override in the multi-DLL solution