0

I would like to use a few functions of a .dll to manage a EVB of Digilent (Basys2).

The manufacturer provides me the next files:

dpcdefs.h
dpcutil.h
dpcutil.lib

Besides the dpcutil.dll that it's installed in c:/windows/system32

The function I am trying to use apperas in the header file dpcutil.h:

..... DPCAPI    BOOL    DpcInit(ERC * perc); ....

And in the file dpcdefs.h appears defined the type ERC. It's a int:

.... typedef int        ERC;    /* Error code type */ ....

The type BOOL is:

.... #define    BOOL unsigned int ....

I've got to use a few more functions but I don't manage to use this one property. I made a class to manage this function:

class Dpcutil
{
    public Dpcutil() { }

    const string _dllLocation = "C:\\Windows\\System32\\dpcutil.dll";

    [DllImport(_dllLocation)]
    public static extern uint DpcInit(ref int perc);
}

The program compiles correctly, but when I execute the function DpcInit The program fails:

Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'D:\SVN\sfa\trunk\sw\digilent_parallel_interface_module_test\digilent_parallel_interface_module_test\bin\Debug\digilent_parallel_interface_module_test.vshost.exe'.

Additional information: A call to PInvoke function 'digilent_parallel_interface_module_test!digilent_parallel_interface_module_test.Dpcutil::DpcInit' correspondence prevented stack. Perhaps the reason is that the managed PInvoke signature does not match the target unmanaged signature. Check that the convention and parameters of the PInvoke call signature match the target unmanaged signature.

I don't know if I 'm using correctly the DllImport.

Community
  • 1
  • 1
oscargomezf
  • 117
  • 1
  • 5
  • 1
    Try adding the calling convention: `[DllImport(_dllLocation, CallingConvention = CallingConvention.Cdecl)]`. I googled your files and DPCAPI is typedef'd as `extern "C"` – cbr Mar 18 '15 at 13:15
  • The issue is indeed the calling convention. I don't think the DLL belongs in the system directory. It's not yours to modify. All the same, don't hard code the full path in your p/invoke. What if the system directory lives somewhere else? – David Heffernan Mar 18 '15 at 13:26
  • Thank you so much GrawCube, now the function seems to work correctly... I am going to try other functions. – oscargomezf Mar 18 '15 at 14:53

0 Answers0