0

I have following C++ function signature in my Warper.dll file

extern "C" DLLEXPORTED unsigned char* UpdateWarp
(int warperId, int x, int y, int *xRet, int *yRet, int *width, int *height, int* scanWidth);

C++ function

    unsigned char* UpdateWarp(int warperId, int x, int y, 
                int *xRet, int *yRet, int *width, int *height, int* scanWidth)
{
    if(xRet && yRet && width && height && scanWidth)
    {
        Warper* warper = getMapItem<Warper*>(m_warpers, warperId);
        if (warper)
        {
            WarpedImage* warpedImg = warper->UpdateWarp(Point(x, y));
            *xRet = warpedImg->Position.X;
            *yRet = warpedImg->Position.Y;
            *width = warpedImg->Image.Width;
            *height = warpedImg->Image.Height;
            *scanWidth = warpedImg->Image.ScanWidth;

            m_warpedImages[warperId] = warpedImg;

            return warpedImg->Image.Data;
        }
    }
    return NULL;
}

in C# I have declared as

 [DllImport("Warper.dll", EntryPoint = "UpdateWarp")]
    private static extern IntPtr UpdateWarp(int warperId, int x, int y,
        ref int xRet, ref int yRet, ref int width, ref int height, ref int scanWidth);

and calling as

IntPtr ptrRawData = UpdateWarp(m_iWarperId, pt.X, pt.Y, ref x, ref y, ref w, ref h, ref iScanWidth);

when executing the code, it gives error AccessViolationException was unhandled both in Debug and Release mode. Error is in passing the values to function I think not returning the value. What can be the reason.

naveed
  • 45
  • 6
  • Try `[DllImport("Warper.dll", EntryPoint = "UpdateWarp", CallingConvention = CallingConvention.Cdecl)]` – xanatos Mar 01 '16 at 10:30
  • Are you sure your method doesn't expect an array of integers? In C `int *xRet` is both what in C# is `ref int xRet` AND `int[] xRet` (and technicall yit is even `out int xRet`, but this is already covered by the `ref int xRet`) – xanatos Mar 01 '16 at 10:36
  • 1
    @naveed Just the fact that you're returning a pointer is somewhat suspicious. If you're returning a pointer to a local variable, the behavior is undefined. You need to post the C++ function. – PaulMcKenzie Mar 01 '16 at 10:36
  • @xanatos when i remove `ref` from the declaration and calling, then it works but doesn't give satisfied result. – naveed Mar 01 '16 at 10:42
  • @PaulMcKenzie I am editing it with C++ function. please check – naveed Mar 01 '16 at 10:42
  • @naveed You should be debugging your DLL and C# code at the same time. Do you know how to do that? If not, then you need to get up to speed on how to do this. If you're using Visual Studio, this should be relatively easy. Problems like this can be easily diagnosed by just debugging and discovering where the problem exists, either in the call, in the return, while the C++ function is executing, etc. – PaulMcKenzie Mar 01 '16 at 10:48
  • thanks. I have solved problem by putting that dll project folder in re-created c# project folder. I think there was problem with my solution. – naveed Mar 01 '16 at 11:19

0 Answers0