如何从c++ / c#调用这个DLL函数

How to call this DLL function from either C++ / C#

本文关键字:DLL 函数 调用 c++      更新时间:2023-10-16

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

[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);