Germán
Germán

Reputation: 4565

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

I'm taking a lot of time trying to figure this out, so I thought I could get some help here. Basically I have a DLL function declared like this in IDL:

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

How exactly do I declare and invoke from either C++ / C#?

Note: there is a VB6 app that successfully invokes the function. Declaration is:

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

The call:

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

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

Addendum:

I cannot guarantee that the VB6 code looks like that because I have the executable, I was only told what that portion looks like, so maybe you guys are right and it doesn't match. I did author the C++ DLL, and now I need to get together some code myself that successfully calls the DLL in order to try out stuff and not depend on that exe.

C++ implementation of the DLL function looks like this:

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

    return S_OK;
}

Upvotes: 1

Views: 395

Answers (1)

D Stanley
D Stanley

Reputation: 152491

Untested C# declaration:

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

Upvotes: 1

Related Questions