I need to pass a StringBuilder object to a custom unmanaged DLL. The code most of the time works great, however once in a while I get "Object reference not set to an instance of an object" which means the GC moved the address of StringBuilder object while managed code is writing to it.
I have tried GC.keepalive already. GCHandle.allc and Marshal.alloc doesn't seem to be possible with this type of object. I need the equivalent of (__pin *) in managed C++ to pin the object somehow.
Expand|Select|Wrap|Line Numbers
- UpdateStatus(DWORD type, DWORD action, LPTSTR buffer, DWORD bufferSize);
- DLL Declare
- private static extern int UpdateStatus(int type, int action, StringBuilder buffer, int bufferSize);
Expand|Select|Wrap|Line Numbers
- StringBuilder buffer = new StringBuilder(256);
- // error happens here.
- UpdateStatus(1, 1, buffer, buffer.Capacity);
Expand|Select|Wrap|Line Numbers
- StringBuilder buffer = new StringBuilder(256);
- // this obviously always works
- UpdateStatus(1, 1, null, 0);
Thanks.