"AccessViolationException" when calling to "Marshal.PtrToStringAuto"
i used external methods resolve mx records of several domains,
but, i'm getting "system.accessviolationexception" not caught try catch block!
i'm working code last couple of years, possible update caused error.
strange 1 server out of 12 throws error.
here code:
private static list<string> _getmxrecords(string domain) { if (environment.osversion.platform != platformid.win32nt) return null; list<string> results = new list<string>(); intptr ptr1 = intptr.zero; intptr ptr2 = intptr.zero; int errorcode = dnsquery(ref domain, querytypes.dns_type_mx, queryoptions.dns_query_bypass_cache, 0, ref ptr1, 0); if (errorcode != 0) return null; mxrecord recmx; for (ptr2 = ptr1; !ptr2.equals(intptr.zero); ptr2 = recmx.pnext) { recmx = (mxrecord)marshal.ptrtostructure(ptr2, typeof(mxrecord)); string record = marshal.ptrtostringauto(recmx.pnameexchange); //////////////////// <-- exception occurs here ////////////////////////// if (record.length > 3) results.add(record); } dnsrecordlistfree(ptr1, 0); if (results.count == 0) return null; return results; }
[dllimport("dnsapi", entrypoint = "dnsquery_w", charset = charset.unicode, setlasterror = true, exactspelling = true)] private static extern int dnsquery([marshalas(unmanagedtype.vbbyrefstr)]ref string pszname, querytypes wtype, queryoptions options, int aipservers, ref intptr ppqueryresults, int preserved); [dllimport("dnsapi", charset = charset.auto, setlasterror = true)] private static extern void dnsrecordlistfree(intptr precordlist, int freetype); private enum queryoptions { dns_query_bypass_cache = 8 } private enum querytypes { dns_type_mx = 15 } [structlayout(layoutkind.sequential)] private struct mxrecord { public intptr pnext; public string pname; public short wtype; public short wdatalength; public int flags; public int dwttl; public int dwreserved; public intptr pnameexchange; public short wpreference; public short pad; }
the "eventviewer" shows error eventid: 1026
framework version: v4.0.30319 description: process terminated due unhandled exception. exception info: system.accessviolationexception stack: @ microsoft.win32.win32native.copymemoryuni(system.text.stringbuilder, intptr, intptr) @ system.runtime.interopservices.marshal.ptrtostringuni(intptr)
and "application error" this:
faulting application xxxxxx.exe, version x.0.0.0, faulting module ntdll.dll, version 5.2.3790.4789, fault address 0x0001bdb2.
i tried (after searching google side down):
1. uncheck "suppress jit optimization on module load"
2. change "platform target" "x86"
3. add "try catch" block @ main application , register "appdomain.currentdomain.unhandledexception" event (which doesn't raise!!)
4. check "p/invoke" signatures
5. frustrated :)
i'm using windows 2003 web edition (32 bit).
thank you!!
i solved it, want update gets here same problem..
you need add attribute called "handleprocesscorruptedstateexceptionsattribute" @ declaration of method calls external method in order redirect exception application, like:
[handleprocesscorruptedstateexceptionsattribute()] private static list<string> _getmxrecords(string domain) { ... }
good luck!
evyatar
.NET Framework > Common Language Runtime Internals and Architecture
Comments
Post a Comment