Link to home
Start Free TrialLog in
Avatar of jdcoburn
jdcoburn

asked on

pointer to a char*[]

hi -- I'm interfacing  to a dll using C# (not written by me) that accepts a pointer argument. i want to transfer an array of bytes (or chars) to a usb device through the dll provided. i can pass the array of pointers and it shows up on the usb monitor correctly. however, i can't set values in the array at runtime. i get a message that says the object is not created ({"Object reference not set to an instance of an object."}).. i've included the relevant code. below. the first part is the extern prototype; the second part is the part of the method that calls it. i get the message regarding 'new' when i single step through the code and try and load an array element with a value.
[DllImport("USBH_api.dll",CharSet=CharSet.Ansi)]
        public static extern UInt32 USBH_BulkTx(IntPtr hndl, char pipe_id, char*[] pBuf2, UInt32 buf_length, out UInt16 err);
 
 
method
{
char*[] pTxbuffer = new char*[30];
            *pTxbuffer[0] = 't';  //this line causes the null reference dialog. if i remove it, the code works but no value is sent.
            
            if (hndl == (IntPtr)0) return;
 
            xfer_len = USB_API.USBH_BulkTx(hndl, bulk_out, pTxbuffer, (uint)pTxbuffer.Length, out err);
 
}

Open in new window

Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

I haven't used pointers in YEARS.

Looks to me like you've correctly created an array that can hold pointers to single chars. Then, rather than assigning the first element a pointer to a char, you're trying to assign a char directly to whatever its pointer points to. Of course, it doesn't point to anything yet.
You probably want to do something like this. Rather than filling an array of pointers to char, you probably want to fill an array of char.

char[] pTxbuffer = new char[30];
pTxbuffer[0] = 't';

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jdcoburn
jdcoburn

ASKER

I was able to solve this problem. thanks for the various suggestions.
Jim