c# - Code Access Security is preventing PInvoking Setup API calls -
i'm rewording question since understand bit more now. originally, had vague. i've discovered i'm being routed called "code access security." old-hat reading this, i'm sure, not me.
the application large in nutshell have 2 assemblies. 1 utilities assembly various "tools" used throughout program. other calling upon these tools in order function.
in utilities assembly, there many functions pinvoked 1 giving me grief is: setupdigetdeviceinterfacedetail() (see here). function prototype looks this:
[dllimport("setupapi.dll", setlasterror = true, charset = charset.auto)] [return : marshalas(unmanagedtype.bool)] public static extern bool setupdigetdeviceinterfacedetail( safehandlezeroorminusoneisinvalid deviceinfoset, ref sp_device_interface_data deviceinterfacedata, intptr deviceinterfacedetaildata, uint deviceinterfacedetaildatasize, intptr requiredsize, intptr deviceinfodata);
in assembly uses function, i'm using 2 step process outlined in remarks in order gain understanding of how space need store devicepath in sp_device_interface_detail_data structure (see here). example:
string getdevicepath(safehandleseroorminusoneisinvalid hlist, sp_device_interface_data infoset) { intptr preqsize = marshal.allochglobal(4); marshal.writeint32(preqsize, 0); uint reqsize; // size needed pinvoke.setupdigetdeviceinterfacedetail(hlist, ref infoset, intptr.zero, 0, preqsize, intptr.zero); reqsize = (uint)marshal.readint32(preqsize, 0); intptr pdevinfodetail = marshal.allochglobal((int)reqsize + 4); // +4 cbsize // call again, time getting actual data wanted pinvoke.setupdigetdeviceinterfacedetail(hlist, ref infoset, pdevinfodetail, reqsize, intptr.zero, intptr.zero); string path; // work .net magic read unmanaged memory path string , assign // above variable. deallocate both unmanaged memory blocks. return path; }
the frustrating thing is, these assemblies used 2 different programs. 1 gui using visual studio isolated shell. other command line program. when gui running, above code called , executes expected. in command line tool however, fail (as described in msdn reference setup api function) data happened. @ point, i'm able recover portion of data returned. comes runtime: "stem.security.partialtrustvisibilitylevel, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089".
i know has code access security i'm not @ sure how fix. using suggestions i've found far i've tried attribute assembly (i placed before namespace block of code): [assembly: allowpartiallytrustedcallers]
but caused other compilation problems.
please, helpful , appreciated.
andy
unfortunately, problem isn't yet fixed. however, problem appears have nothing code access security first thought. being thrown red herring. stepped way through code using memory window in visual studio , noticed these strings in memory before calling setup api function fill them. occasionally, different block of memory different contents too, ended contents pasted.
the problem appears have 64 vs. 32 bit environments (at least, that's theory @ point).
however, question isn't actual problem i'm "answering" close it.
Comments
Post a Comment