1

I am a beginner in C# and I have been asked to create a C# code which uses a C++ DLL.

Functions from C++ DLL need a void* parameter so I send a IntPtr parameter from the C# code.

But at the end of this function my pointer seems to be NULL. Am I missing something?

Here my C# code:

int somme = 0;
IntPtr deviceHandle = new IntPtr();
uint[] SpiConf = new uint[7];
somme = Opening(deviceHandle, SpiConf);
if (deviceHandle == IntPtr.Zero)
{
    Console.WriteLine("deviceHandle is NULL"); // 
}

And here is my function from the C++ DLL:

int Opening(void* deviceHandle, unsigned char SpiConf[])
{
    wchar_t devPath;
    unsigned long devPathsize = 0;
    unsigned short VID = 0x4d8;
    unsigned short PID = 0xde;
    int res;

    deviceHandle = Mcp2210_OpenByIndex(VID, PID, 0, &devPath, &devPathsize);
    res = Mcp2210_GetLastError();
    if (res != E_SUCCESS)
    {
        //Console.WriteLine("Failed to open connection");
        return -1;
    }
    if (deviceHandle == NULL)
    {
        return -2;
    }
    return 0; // This function returns 0
}

Any help would be very appreciate.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Shouldn't you also post the P/Invoke signature of the `Opening` method? – Uwe Keim Mar 13 '19 at 09:28
  • What does your `[DllImport]` method look like? – kalimag Mar 13 '19 at 09:28
  • 1
    @MTA Please do not format non-code like "C#" and "C++ DLL" as code. – Uwe Keim Mar 13 '19 at 09:32
  • @kalimag @UweKeim Hi, here is my [DllImport] method : `[DllImport("Spi_Functions.dll", CallingConvention = CallingConvention.Cdecl)]` `public static extern int Opening(IntPtr deviceHandle, uint[] SpiConf);` – R Landroval Mar 13 '19 at 09:56
  • 1
    Looking at the C++ code, I believe on exit the value of your C# deviceHandle will remain unchanged. _"deviceHandle = Mcp2210_OpenByIndex(...)"_ will only change the local (stack) copy of what is being pointed to - it will not change the original parameter or what the original parameter pointed to. You would need to pass a reference to a pointer or pointer to a pointer to modify the actual pointer parameter passed. – PaulF Mar 13 '19 at 09:57
  • @PaulF Hi, Thank you for your answer. What do you mean with _pass a reference to a pointer or pointer to a pointer_ ? I tried to send `ref deviceHandle` from my C# code but I've got the same problem. The pointer is still NULL. – R Landroval Mar 13 '19 at 10:36
  • It all depends on what _"Mcp2210_OpenByIndex"_ returns, if it returns the actual handle then your C++ code needs changing to _"*deviceHandle = Mcp2210_OpenByIndex(...)"_ to save the handle at the location pointed at - your C# code would need modifying to pass a reference to an int not an IntPtr - or setting the IntPtr to point at an int, rather than null. See here for info about _"reference to pointer"_ & _"pointer to pointer"_ : https://stackoverflow.com/questions/823426/passing-references-to-pointers-in-c – PaulF Mar 13 '19 at 11:25
  • I'd summarize this to: "it's the C++ part that's faulty. Not the C# part", right? – Dan Byström Mar 14 '19 at 09:36

0 Answers0