I'm P/Invoking out to Graphviz as shown here. When I wrote that blog entry, the code worked just fine. Now, I'm putting together an HttpModule
that renders Graphviz graphs using that code, but I get an AccessViolationException
at agmemread
.
// Native signature
Agraph_t agmemread(char *);
// P/Invoke Signature
[DllImport(LIB_GRAPH)]
private static extern IntPtr agmemread(string data);
// Usage
IntPtr g = agmemread(data);
Like I said, this worked perfectly before. But now, I can't get my code to work in anything. Even my old Graphviz apps based on the same code don't work anymore.
What could I have possibly changed that would cause this? I haven't even downloaded a new version of Graphviz or anything, so the DLLs are all the same.
EDIT: I tried changing string
to StringBuilder
, but that produced the same result. Then, I added a MarshalAs
attribute:
static extern IntPtr agmemread([MarshalAs(UnmanagedType.LPWStr)] string data);
With that, I no longer get an AccessViolationException
, but Graphviz fails to read the string correctly and returns a null pointer.