1

I am trying to import dll that was coded in C++ to C#.

Code in C++:

.h file:

#define DllExport __declspec( dllexport )
extern "C" {
    class DllExport SomeClass
    {
    public:
        SomeClass();
        virtual ~SomeClass();
    private:
        BOOL    SomeFunction(DWORD cCode, DWORD dwIn, LPDWORD lpdwOut);
        }
}

.cpp file:

BOOL SomeClass::SomeFunction(DWORD cCode, DWORD dwIn, LPDWORD lpdwOut)
{
    if (lpdwOut == NULL) return FALSE;  //@006A

    BOOL    Result = FALSE;

        some code...

    return Result ;
}

Code in C#:

[DllImport(ConstVar.PMDDLLPATH, EntryPoint = "?SomeFunction@SomeClass@@AEAAHKKPEAK@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SomeFunction([MarshalAs(UnmanagedType.U4)] uint cCode,
                                       [MarshalAs(UnmanagedType.U4)] uint dwIn,
                                       [MarshalAs(UnmanagedType.U4)] ref uint lpdwOut);
uint res = 0;
bool result = SomeFunction(222, 2, ref res);

When debugging, incorrect value reflected in C++.

But when I add another parameter in C#, correct value will be reflected.

[DllImport(ConstVar.PMDDLLPATH, EntryPoint = "?SomeFunction@SomeClass@@AEAAHKKPEAK@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SomeFunction(uint param
                                       [MarshalAs(UnmanagedType.U4)] uint cCode,
                                       [MarshalAs(UnmanagedType.U4)] uint dwIn,
                                       [MarshalAs(UnmanagedType.U4)] ref uint lpdwOut);
uint res = 0;
bool result = SomeFunction(0, 222, 2, ref res);

I searched on the web to get some answer, could anyone please help to explain?

Hulyo
  • 11
  • 1
  • 1
    `BOOL SomeFunction(DWORD cCode, DWORD dwIn, LPDWORD lpdwOut);` -- This is a non-static member function -- thus there is a hidden `this` argument. Where does you setup account for this missing argument? – PaulMcKenzie Nov 04 '22 at 08:25

0 Answers0