我花了很多时间试图解决这个问题,所以我想我可以在这里得到一些帮助。 基本上我有一个在 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/