perwapi Discussions Rss Feedhttp://perwapi.codeplex.com/Thread/List.aspxperwapi Discussions Rss DescriptionNew Post: .NET Framework 4.0 supporthttp://perwapi.codeplex.com/discussions/270081<div style="line-height: normal;"><p>Hi shah_amish_b</p>
<p>I have not made the changes for the V4.0 metadata, so it is likely that this is broken.&nbsp; I hope to have a look soon, to see if there are large or small changes.</p>
<p>John</p></div>k_john_goughFri, 26 Aug 2011 05:32:30 GMTNew Post: .NET Framework 4.0 support 20110826053230ANew Post: .NET Framework 4.0 supporthttp://perwapi.codeplex.com/discussions/270081<div style="line-height: normal;">
<p>Hi,</p>
<p>I'm just reading and writing dll without modifying any thing. The dll is built with .net framework 4.0 but after writing If I try to open it with ILDASM, it failed to open and the error is &quot;Failed to open meta data&quot;.</p>
<p>Does PERWAPI support binaries built with framework 4.0?</p>
<p>Regards.</p>
<p>--Amish</p>
<p>&nbsp;</p>
</div>shah_amish_bTue, 23 Aug 2011 12:58:56 GMTNew Post: .NET Framework 4.0 support 20110823125856PNew Post: Alter binary by adding try / finally in methodhttp://perwapi.codeplex.com/discussions/263647<div style="line-height: normal;"><p>Hi</p>
<p>I guess you can find out what the ILASM should look like by looking at the ILDASM output from a short C# program.</p>
<p>In order to see what methods to call to make it happen you might have a look at my old book "Compiling for the .NET Common Language Runtime" Prentice-Hall PTR (2002).&nbsp; Borrow a copy and have a look.&nbsp; Otherwise you can have a look at the source code of the Component Pascal Compiler, available on CodePlex as GPCP.&nbsp; If you need a bigger pointer send me a message and I shall dig out the details and reply.</p>
<p>Cheers, John</p></div>k_john_goughMon, 11 Jul 2011 11:00:54 GMTNew Post: Alter binary by adding try / finally in method 20110711110054ANew Post: Alter binary by adding try / finally in methodhttp://perwapi.codeplex.com/discussions/263647<div style="line-height: normal;">
<p>Hi,</p>
<p>I want to add try/finally block in one of the method using this library. Do we have any samples for this?</p>
<p>I'm trying add try/finally in following code,</p>
<p>public void FunctionVoid()<br>
{<br>
&nbsp;&nbsp; Console.WriteLine(&quot;FunctionVoid called!!&quot;);<br>
}</p>
<p>so I wrote following code using PERWAPI,&nbsp;</p>
<p>CILInstructions instructions = methodDef.GetCodeBuffer();<br>
<br>
instructions.ResetInstCounter();<br>
instructions.StartInsert();<br>
instructions.StartBlock(); //try block<br>
instructions.EndInsert();<br>
<br>
while (true)<br>
{<br>
&nbsp;&nbsp; CILInstruction instCurrent = instructions.GetNextInstruction();<br>
<br>
&nbsp;&nbsp; if (instCurrent == null)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; instructions.GetPrevInstruction();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br>
&nbsp;&nbsp; }<br>
<br>
}<br>
<br>
instructions.StartInsert();<br>
TryBlock tryblk1 = instructions.EndTryBlock(); //try end<br>
instructions.StartBlock(); //finally<br>
instructions.EndFinallyBlock(tryblk1);<br>
instructions.EndInsert();<br>
<br>
</p>
<p>but, somehow it doesn't add finally block. If you look at patched binary in ildasm,</p>
<p>method public hidebysig instance void&nbsp; FunctionVoid() cil managed<br>
{<br>
&nbsp; // Code size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 13 (0xd)<br>
&nbsp; .maxstack&nbsp; 1<br>
&nbsp; .try<br>
&nbsp; {<br>
&nbsp;&nbsp;&nbsp; IL_0000:&nbsp; nop<br>
&nbsp;&nbsp;&nbsp; IL_0001:&nbsp; ldstr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;FunctionVoid called!!&quot;<br>
&nbsp;&nbsp;&nbsp; IL_0006:&nbsp; call&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void [mscorlib]System.Console::WriteLine(string)<br>
&nbsp;&nbsp;&nbsp; IL_000b:&nbsp; nop<br>
&nbsp; }&nbsp; // end handler<br>
&nbsp; IL_000c:&nbsp; ret<br>
} // end of method Validate::FunctionVoid</p>
<p>Is there any thing wrong in my code? Can any body suggest me the correct method to add finally block using PERWAPI?</p>
<p>Regards,</p>
</div>shah_amish_bSat, 02 Jul 2011 06:10:59 GMTNew Post: Alter binary by adding try / finally in method 20110702061059ANew Post: How to add new GENERIC type ?http://perwapi.codeplex.com/Thread/View.aspx?ThreadId=233819<div style="line-height: normal;">Thanks for the reply.<br /><br />I did another test actually ADDING the linq reference to the class and<br />than getting the type from that referenced assembly. Now I get even<br />more chaos because suddenly the class has two duplicated constructors<br />!<br /><br />This obviously happens due to a bug with attributes, I show this at<br />the end of the code.<br /><br />Here is the code snippets of what I did:<br /><br />To my party class I added a member so the LINQ assembly and the<br />EntityRef now get exported:<br /><br />System.Data.Linq.EntityRef<Party> _Party2 = new<br />System.Data.Linq.EntityRef<Party>();<br /><br />In my project I loop through the referenced assemblies, when I get the<br />LINQ assembly I do:<br /><br />ClassRef entityRefClass = _Assembly.GetClasses().Where(x =><br />x.Name().StartsWith("EntityRef`1")).Single();<br /><br />Than I get the Party class, and I want to add a new field owner which<br />should be of type EntityRef<Party> (same like the Party2 I added now<br />manually).<br /><br />So I call: (aField is of type Party)<br /><br />PEAPI.ClassSpec _EntityType = entityRefClass.Instantiate(new<br />PEAPI.Type[] { aField.GetFieldType() });<br /><br />Inspecting this in debug time it has a GenericParameter of type party,<br />so I should have now a EnitytRef<Party> type.<br /><br />I than add a field to my class:<br /><br />aClass.AddField(PEAPI.FieldAttr.Public, "OL2S__owner", _EntityType);<br /><br />Now I open the modified assembly in reflector and all seems correct:<br /><br />public EntityRef<Party> OL2S__owner;<br /><br />Declaring Type: OL2S.TestClassesIntercepted.Party<br />Assembly: TestClassesIntercepted, Version=1.0.0.0<br /><br />Which is EXACTLY the same as the field (Party2) added in C# code<br /><br />private EntityRef<Party> _Party2;<br /><br />Declaring Type: OL2S.TestClassesIntercepted.Party<br />Assembly: TestClassesIntercepted, Version=1.0.0.0<br /><br />Now I reference that assembly and try to use the Party class. I cannot<br />compile as VS.net throws me error on line:<br /><br />Party _Party = new Party();<br /><br />Error 3 The call is ambiguous between the following methods or<br />properties: 'OL2S.TestClassesIntercepted.Party.Party()' and<br />'OL2S.TestClassesIntercepted.Party.Party()'<br /><br />Opening reflector I indeed see two ctors for Party, so perwapi somehow<br />added another ctor. As I mentioned above, this is a clear bug wtih<br />attributes. I did add the following attributes to my class (so the<br />fields who are just there to get the external assembly referenced to<br />not get modified by my code)<br /><br /> [Transient()]<br /> System.Data.Linq.EntityRef<Party> _Party2 = new<br />System.Data.Linq.EntityRef<Party>();<br /><br /> [Transient()]<br /> private System.Guid Foo;<br /><br />Transient is a simple class:<br /><br /> public class TransientAttribute:System.Attribute<br /> {<br /> }<br /><br />So if I DO NOT MODIFY anything, just open the assembly and write it<br />back , there is now a duplicated ctor ! I tracked this down to<br />Attributes using ILDASM, this shows me instead of a ctor:<br /><br />method:$MD$18: void()<br /><br />and the body for this is:<br /><br />.method public hidebysig specialname rtspecialname<br /> instance void $MD$18() cil managed<br />{<br /> // Code size 7 (0x7)<br /> .maxstack 8<br /> IL_0000: ldarg.0<br /> IL_0001: call instance void [mscorlib]System.Attribute::.ctor()<br /> IL_0006: ret<br />} // end of method Party::$MD$18<br /><br />Hope there is a solution for this and an example for how to add the<br />EntityRef class by MANUALLY adding the external assembly to the<br />calling one.<br /><br />Thanks for any help<br /><br />Joe</div>joer33304Thu, 11 Nov 2010 23:28:12 GMTNew Post: How to add new GENERIC type ? 20101111112812PNew Post: How to add new GENERIC type ?http://perwapi.codeplex.com/Thread/View.aspx?ThreadId=233819<div style="line-height: normal;">Hi Joe Not sure what is happening here. I will check it out and get back to you. I have recently done a couple of small updates to the source of PERWAPI to fix an issue in another area. The best use example of PERWAPI is as the PE-file writer for the GP
Component Pascal compiler. However this does not exercise the generics at all. John</div>k_john_goughThu, 11 Nov 2010 00:56:05 GMTNew Post: How to add new GENERIC type ? 20101111125605ANew Post: How to add new GENERIC type ?http://perwapi.codeplex.com/Thread/View.aspx?ThreadId=233819<div style="line-height: normal;">
<p>Hi,</p>
<p>thanks for the great product, I got my first task ALMOST solved. I am stuck at adding a new field to a class with a GENERIC type of an external reference.</p>
<p>Can anybody post some code on how to do this correctly ? Here is my scenario:</p>
<p>I need to add fields of type System.Data.Linq.EntityRef&lt;T&gt;. The assembly I am altering has NO reference to system.data.linq. I did add that reference, but adding such obviously does nothing because of one calls GetClasses there are ZERO.&nbsp; I loaded
the System.Data.Linq assembly directly as PE file and I see all classes (of course).</p>
<p>So its unclear why one needs a reference when there is nothing in the reference ? Anyway, I added the type to the reference and it than shows up.</p>
<p>But now I need to define the type of the generic class, so I did</p>
<p>&#65279;&#65279;&#65279;PEAPI.ClassSpec _EntityType = entityRefClass.Instantiate(new PEAPI.Type[] { aField.GetFieldType() });</p>
<p>and than added this to my class as:</p>
<p>&#65279;aClass.AddField(PEAPI.FieldAttr.Public, _FieldName, _EntityType);</p>
<p>&nbsp;</p>
<p>The strange thing is that in ildasm and reflector all looks correct, the member shows up as something like EnitytRef&lt;Customer&gt;</p>
<p>But if I use the dll in another project it even does not compile with an error &quot;not support in Language&quot; if I reference the field.</p>
<p>If I do not reference the field, but get the fields via reflection, the field shows up but as FieldRefType and not as the other correct fields with their name.</p>
<p>&nbsp;</p>
<p>I know in MSIL, references are addressed via 0x0a tokens, so I am sure somewhere I have to add the referenced type to the executing assembly so this token is created. However what about the generic implementation ? As Instantiate returns &#65279;&#65279;&#65279;PEAPI.ClassSpec,
this cannot be added via peFile.AddClass.</p>
<p>&nbsp;</p>
<p>So an example would be highly appreciated....</p>
<p>&nbsp;</p>
<p>Joe</p>
<p>&#65279;</p>
</div>joer33304Mon, 08 Nov 2010 03:40:02 GMTNew Post: How to add new GENERIC type ? 20101108034002ANew Post: PERWAPI uses CR-LF centric message formats.http://perwapi.codeplex.com/Thread/View.aspx?ThreadId=85492<div style="line-height: normal;"><p>Currently PERWAPI emits messages that are very Microsoft-platform centered.&nbsp; We will try to clean this up for the next refresh, along with any fixes and/or extensions.&nbsp;</p>
<p>The same principle applies to the treatment of path-names.&nbsp;&nbsp;The whole project needs to be made more Mono-friendly.</p></div>k_john_goughTue, 16 Feb 2010 11:35:38 GMTNew Post: PERWAPI uses CR-LF centric message formats. 20100216113538ANew Post: Documentation issue with BoundArrays?http://perwapi.codeplex.com/Thread/View.aspx?ThreadId=85491<div style="line-height: normal;"><p>The discussion on page 23 of the documentation regarding the creation of BoundArray descriptors is almost certainly wrong in some details.&nbsp; It is the case that some CIL emitters do specify arrays where the number of lower bound values specified&nbsp;is different to the number of sizes specified, however the precise semantics of how PERWAPI should translate such cases into PE-files that conform to the ECMA standard needs to be clarified.</p>
<p>The text&nbsp; will be corrected as soon as possible.&nbsp; Experiments using PERWAPI to express the complete semantics of C# V3 should sort the matter out sometime soon.</p></div>k_john_goughTue, 16 Feb 2010 11:27:33 GMTNew Post: Documentation issue with BoundArrays? 20100216112733A