Click here to Skip to main content
16,011,757 members

Comments by lewisv (Top 5 by date)

lewisv 5-Jun-18 12:15pm View    
I think I have it.
Thanks for the help.

So your first post was good, I think that was one of the problems.
The second issue was dealing with a the handle that is passed into each routine.

When I opened a connection I got an IntPtr back.
IntPtr m_ptr;
m_ptr = wrapper.Open(...)
I turned that into a struct
m_handle= Marshal.PtrToStructure<docprochandle>(m_ptrp);

I was passing that handle back into the dll.
wrapper.DPSetCallBacks(ref m_handle, ref m_events);

But the wrapper did not want a DocProcHandle, it wanted an IntPtr. When I replaced that, it went though the application just fine.
wrapper.DPSetCallBacks(m_ptr, ref m_events);

Maybe it worked fine with non-callbacks, but with the callbacks because it was placed on the stack again and messing the stack up like you mentioned.
lewisv 2-Jun-18 18:01pm View    
It has been 10 years since I did any real work in c/c++.
But it looks like cdecl to me.

#define CDECLCALL_CONV __cdecl

typedef void (CDECLCALL_CONV *ConnectedCB) (DPHandle hdl);
BPS_DP_API unsigned long CDECLCALL_CONV getDPDevices(DPHandle devices[], unsigned long* count);


What do you mean by cdecl being right for calling "into" managed code. Is there something else I need to do in my managed c# code?
lewisv 2-Jun-18 16:19pm View    
I am sorry if it was a little confusing. I have run it though the debugger.

private void connected(ref DocProcHandle hdl)
{
  // this code works fine
}  
  <--- it crashes here after it leaves my callback method and before anything else in the c# application runs


There is no error message, the program just "stops responding" then asks me if I want to debug. No exits the debugger, Yes it will open another visual studio, but then tell me it can't debug.
If I leave the callback out, the application continues with no issues. But of course I need to handle the callbacks.

I am guessing here, but it looks like by setting/calling the callback function I am messing up memory. So the application crashes outside of the c# application and inside the dll that is calling the delegate.
I know the dll works, I have a c application that is calling it and working. To make sure I have not missed anything, I am basically rebuilding the c app in c# and calling the same methods.

Thanks
lewisv 2-Jun-18 11:21am View    
That makes sense so here is what I changed it to.

Changed the declaration in the struct to
public struct DPcbs
{
  public uint size;
  public IntPtr connectedCB;
... more callbacks
}


The setting the struct i have (i made the items member variables so that the GC would not get rid of them)
      m_c = new ConnectedCB(connected);
      m_p = Marshal.GetFunctionPointerForDelegate(m_c);
      m_events.connectedCB = m_p;


It fires and runs, but the same issue. It blows up directly have the connected delegate finishes.
lewisv 8-May-18 12:34pm View    
You can see the header file. I think my dllimport looks like it would work for the given function.
As far as what my test program is passing in, they are the same as a sample c application that uses the same dll and method.