<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-948868851971907265</id><updated>2011-08-25T07:05:27.389+10:00</updated><category term='Python'/><category term='Developer Studio'/><category term='Apple Pie'/><category term='Error -36'/><category term='marshalling'/><category term='Microsoft'/><category term='MFC'/><category term='Seagate'/><category term='CoreGraphics'/><category term='PDF'/><category term='webbrowser'/><category term='Samba'/><category term='Recipe'/><category term='COM'/><category term='Windows'/><category term='NAS'/><category term='OSX'/><category term='embed'/><category term='Developer Studio Express'/><category term='GoFlex Home'/><category term='thread'/><category term='.NET'/><title type='text'>Stuff I needed to know</title><subtitle type='html'>A collection of stuff that I probably found out the hard way.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-5044294813224527312</id><published>2011-05-19T18:00:00.001+10:00</published><updated>2011-05-19T18:04:28.280+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Recipe'/><category scheme='http://www.blogger.com/atom/ns#' term='Apple Pie'/><title type='text'>Grandma's Apple Pie (as dictated by Mum)</title><content type='html'>&lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;&lt;span style="text-decoration: underline"&gt;Short Crust Pastry&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"&gt;&lt;br /&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Blend 125g butter, 125g plain flour and 125g self raising flour in a kitchen whiz.&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Add 2 tablespoons of caster sugar and blend.&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Add 1 egg yolk and combine.&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Add 2-3 tablespoons of ice water (until the pastry &lt;i&gt;almost&lt;/i&gt; combines)&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Press pastry into a ball, then separate into 2/3 and 1/3 disks flattened out.&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Wrap in plastic and chill for 60 minutes.&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"&gt;&lt;br /&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;&lt;span style="text-decoration: underline"&gt;Apple Filling&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"&gt;&lt;br /&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Peel, core and slice 5-6 large Granny Smith Apples. Add 1 lemon, cut into quarters. Add 1/4 cup water (up to 1/2 cup).&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Bring to boil and cook till softened.  Add 1/2 cup sugar and stir until dissolved. Leave till cold.&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Put pastry in tin and add cold apple, being sure to remove lemon.  Cover with pastry. Brush the top with egg white, then sprinkle with caster sugar.&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"&gt;&lt;br /&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Cook at 220C for 10-15 minutes.&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"&gt;Turn oven down to 180C and cook for 25-20 minutes  (ie, make the total oven time about 35 minutes).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-5044294813224527312?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/5044294813224527312/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2011/05/grandmas-apple-pie-as-dictated-by-mum.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/5044294813224527312'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/5044294813224527312'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2011/05/grandmas-apple-pie-as-dictated-by-mum.html' title='Grandma&apos;s Apple Pie (as dictated by Mum)'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-4993251197289304074</id><published>2011-02-27T19:15:00.007+11:00</published><updated>2011-06-22T22:52:15.079+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Samba'/><category scheme='http://www.blogger.com/atom/ns#' term='OSX'/><category scheme='http://www.blogger.com/atom/ns#' term='GoFlex Home'/><category scheme='http://www.blogger.com/atom/ns#' term='Seagate'/><category scheme='http://www.blogger.com/atom/ns#' term='NAS'/><category scheme='http://www.blogger.com/atom/ns#' term='Error -36'/><title type='text'>Samba NAS and error code -36</title><content type='html'>&lt;div&gt;I just bought a Seagate GoFlex Home NAS and was wanting to backup a bunch of files to it.  I ran their installer, installed all their recommended tools, configured the server, and got a Folder on my desktop representing the "GoFlex Home Public" folder on the server.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The first thing I tried to backup was their installation software, and I got this:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;The Finder can’t complete the operation because some data in “some folder” can’t be read or written.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;(Error code -36)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It's taken me several hours and lots of unsuccessful internet trawling to finally work out what the workaround is, so I figure its worth a post here where Google can find it for the next guy.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The problem appears to be related to the use of extended attributes, which Snow Leopard loves to attach to files these days.  I believe that there is some sort of problem writing files with the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;com.apple.quarantine&lt;/span&gt; attribute to Samba mounted disks.  Whilst you can try to use &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;xattr -d &lt;/span&gt;to remove those attributes from your files, its tedious and very error prone.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Similarly, I found that sometimes files got stuck on the drive - you couldn't remove them, if you tried (in the terminal) you'd get permission errors.  Nothing, &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;sudo&lt;/span&gt; included, would allow you to fix the permissions. Yet looking in a terminal window with &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;/bin/ls&lt;/span&gt;, they looked fine.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Short answer: Don't use the GoFlex Home Agent to mount network folders.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This always uses the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;smb:&lt;/span&gt; protocol to access the server.  Instead, use the "Connect to server..." command on the "Go" menu in the finder, and explicitly specify that you want to use the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;afp:&lt;/span&gt; protocol instead.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Once I switched to using &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;afp:&lt;/span&gt;, all of the spurious errors went away.  I was able to delete all the temporarily stuck files, and I could copy whatever I liked onto the drive, regardless of the extra attributes.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Strangely enough, when I then tried out the Memeo Backup that Seagate include in the package, it warned me that my backup volume is &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;afp:&lt;/span&gt; and I should use &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;smb: &lt;/span&gt;instead.  Yeah right. However, it looks like one reason they recommend &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;smb:&lt;/span&gt; is that the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;afp: &lt;/span&gt;volume didn't seem to auto-mount when I went back in - I had to mount it for them.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Update: &lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Alternately, you can just wait for Apple to release OSX 10.6.7 which "&lt;span class="Apple-style-span" style="color: rgb(85, 85, 85); font-style: italic; line-height: 24px; "&gt;resolves an issue when transferring files to certain SMB servers&lt;/span&gt;".  Clearly, this blog post blowing the whole issue wide open had them scared and they realised they had to fix it pronto, or face my scathing posts.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Update 2:&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Hmmm, despite it "mostly" working, I keep getting errors from Time Machine saying it was unable to complete the backup - it looks to me like it fails if it has to mount the drive itself.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Moral: do not buy this drive if you want to use it for Time Machine backups.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-4993251197289304074?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/4993251197289304074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2011/02/samba-nas-and-error-code-36.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/4993251197289304074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/4993251197289304074'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2011/02/samba-nas-and-error-code-36.html' title='Samba NAS and error code -36'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-5380468747335117268</id><published>2010-11-27T13:37:00.006+11:00</published><updated>2010-11-27T14:17:16.142+11:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Developer Studio'/><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft'/><title type='text'>Why are my zeroes behaving like ones?</title><content type='html'>&lt;div&gt;Guess the output of this program:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; C:\&gt;&lt;b&gt;type x.c&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; #include &lt;stdio.h&gt;&lt;/stdio.h&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; #define ZERO 0&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; #define P(x) printf("x=%d",x)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; main()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; {&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;     P(ZERO);&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; }&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; C:\&gt;&lt;b&gt;.\x.exe&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; x=1&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Duh, what?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Ok, I cheated, I didn't show you the stupid compiler option I managed to pass through the use of a slightly buggy makefile.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;div&gt;C:\&gt;&lt;b&gt;cl &lt;/b&gt;&lt;span class="Apple-style-span"  style="color:#FF0000;"&gt;&lt;b&gt;/D0&lt;/b&gt;&lt;/span&gt;&lt;b&gt; x.c&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86&lt;/div&gt;&lt;div&gt;Copyright (C) Microsoft Corporation.  All rights reserved.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;x.c&lt;/div&gt;&lt;div&gt;Microsoft (R) Incremental Linker Version 9.00.30729.01&lt;/div&gt;&lt;div&gt;Copyright (C) Microsoft Corporation.  All rights reserved.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;/out:x.exe&lt;/div&gt;&lt;div&gt;x.obj&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;For those who don't memorise command-lines, that &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;b&gt;/D0&lt;/b&gt;&lt;/span&gt; essentially is instructing the compiler to "define a value for the symbol &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;" and the default value for definitions is "&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;b&gt;1&lt;/b&gt;&lt;/span&gt;".&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Yes, the Microsoft VC90 compiler actually allows you to redefine the value of INTEGER tokens in your source code.  Same thing happens at VC100.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;C:\&gt;&lt;b&gt;cl /D0 x.c&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;/span&gt;&lt;/div&gt;&lt;span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Copyright (C) Microsoft Corporation.  All rights reserved.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;x.c&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Microsoft (R) Incremental Linker Version 10.00.30319.01&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Copyright (C) Microsoft Corporation.  All rights reserved.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;/out:x.exe&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;x.obj&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;C:\&gt;&lt;b&gt;.\x.exe&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;x=1&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It could be argued that it only shows up if you have one preprocessor macro using the value of another preprocessor macro, and you have to use an unlikely command-line option, so its not that tragic.  But seriously guys, Apple catches you doing that sort of stupid thing at command-line parsing time...&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;jeff$ &lt;b&gt;cc -D0 x.c&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;command-line&gt;&amp;lt;command-line&gt;: error: macro names must be identifiers&lt;/command-line&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;... and those of that that grew up with ANSI-C use macros like this all the time.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;#define NAME(l)&lt;/span&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;l[0]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;#define ADDR(l) l[1]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;#define TYPE(l) l[2]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Having those 0, 1 and 2 become subject to the whim of the command-line is unthinkable.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This all came about because of a Python-generated command line.  I thought I was creating&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;CL &lt;b&gt;/DOPTION0&lt;/b&gt; x.c&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;but a bug meant I created this instead:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;CL &lt;b&gt;/DO /DP /DT /DI /DO /DN &lt;/b&gt;&lt;span class="Apple-style-span"  style="color:#FF0000;"&gt;&lt;b&gt;/D0&lt;/b&gt;&lt;/span&gt; x.c&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So I'm a lot more careful now.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Someone did point out to me that&lt;i&gt; "All computer programs are just 0s and 1s.  Looks like this one was just 1s"&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Computer geek humor.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-5380468747335117268?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/5380468747335117268/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2010/11/why-are-my-zeroes-behaving-like-ones.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/5380468747335117268'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/5380468747335117268'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2010/11/why-are-my-zeroes-behaving-like-ones.html' title='Why are my zeroes behaving like ones?'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-37936456848639858</id><published>2009-07-02T15:45:00.010+10:00</published><updated>2009-07-09T16:30:15.841+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='marshalling'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft'/><title type='text'>.NET corrupts heap when marshalling LPSTR</title><content type='html'>(Hi Lizzie)&lt;br /&gt;&lt;br /&gt;I've just spent the best part of a week trying to track down why my bridge from C# to C++ was working on Windows XP but crashing sporadically on Windows 7, and the answer is that .NET marshalling is trickier than you think for strings.&lt;br /&gt;&lt;br /&gt;Essentially, I had this:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;[DllImport("mydll.dll",CharSet=Ansi,CallingConvention=Cdecl)]&lt;br /&gt;[MarshalAs(UnmanagedType.LPStr)]&lt;br /&gt;private static extern String LookupCorrespondingString(Int32 key);&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;and in the DLL, I had&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;__declspec(dllexport) const char *LookupCorrespondingString(int key);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Whenever I called this, it would get all the way into my DLL, I could tell it was going to return a value, but during the return operation, it would crash.  When I ran it in the debugger, I got output messages about how memory was being free'd into the wrong heap.&lt;br /&gt;&lt;br /&gt;Eventually I found this article:&lt;br /&gt;&lt;a href="https://blogs.msdn.com/dsvc/archive/2009/06/22/troubleshooting-pinvoke-related-issues.aspx"&gt;https://blogs.msdn.com/dsvc/archive/2009/06/22/troubleshooting-pinvoke-related-issues.aspx&lt;/a&gt;&lt;br /&gt;which contained the useful quote (emphasis mine):&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;When a string buffer allocated by native code is marshaled to managed code, CLR Interop marshaller will allocate a managed string object and copy the contents of native buffer to the managed string object. Now &lt;b&gt;in order to prevent a potential memory leak, CLR Interop Marshaller will try to free allocated native memory. It does so by calling CoTaskMemFree. The decision to call CoTaskMemFree is by-design. This can at times lead to crash&lt;/b&gt;, if memory was allocated by the called-native function using any API other than CoTaskMemAlloc family of API’s as custom allocators may allocate on different heaps.&lt;/i&gt;&lt;/blockquote&gt;And there was the answer.  .NET was freeing the block of memory I had passed to it to help me "prevent a potential leak".  The problem being the lack of ability to communicate the 'const-ness' of the underlying DLL entry point's return value in the MarshalAs() attribute.&lt;br /&gt;&lt;br /&gt;The solution was to declare the entry point differently:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;[DllImport("mydll.dll",CharSet=Ansi,CallingConvention=Cdecl)]&lt;br /&gt;private static extern &lt;span class="Apple-style-span"  style="color:#FF0000;"&gt;IntPtr &lt;/span&gt;LookupCorrespondingString(Int32 key);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;and then when I call it, do the marshalling explicitly.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;String s = &lt;span class="Apple-style-span"  style="color:#FF0000;"&gt;Marshal.PtrToStringAnsi(&lt;/span&gt; LookupCorrespondingString(k) &lt;span class="Apple-style-span"  style="color:#FF0000;"&gt;)&lt;/span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style=" ;font-family:Georgia;"&gt;After I bitched and moaned about how this stuff isn't documented anywhere, Manish Jawa kindly pointed out that it is, in fact, documented in the very first sentence on this page: &lt;a href="http://msdn.microsoft.com/en-us/library/f1cf4kkz.aspx" style="text-decoration: none;"&gt;http://msdn.microsoft.com/en-us/library/f1cf4kkz.aspx&lt;/a&gt; - you can't ask for more than that.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style=" ;font-family:Georgia;"&gt;Actually, you can and they give it to you here: &lt;a href="http://msdn.microsoft.com/en-us/library/x3txb6xc.aspx"&gt;http://msdn.microsoft.com/en-us/library/x3txb6xc.aspx&lt;/a&gt; - the problem I was experiencing and its solution spelled out.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So, the moral of the story is: if you pass native strings to .NET via the marshalling interface, make sure you use IntPtr and PtrToStringAnsi() unless you want them to be free'd for you.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-37936456848639858?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/37936456848639858/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/07/net-corrupts-heap-when-marshalling.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/37936456848639858'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/37936456848639858'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/07/net-corrupts-heap-when-marshalling.html' title='.NET corrupts heap when marshalling LPSTR'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-7000471344172482832</id><published>2009-05-13T10:57:00.007+10:00</published><updated>2009-05-13T13:33:06.828+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='COM'/><category scheme='http://www.blogger.com/atom/ns#' term='webbrowser'/><category scheme='http://www.blogger.com/atom/ns#' term='thread'/><category scheme='http://www.blogger.com/atom/ns#' term='embed'/><title type='text'>Embedding .NET in an otherwise native application</title><content type='html'>For reasons best known to the company I work for we embed the .NET CLR inside our otherwise native C++ MFC-based application, and for the last two weeks I've been trying to nail down why pretty much everything seems to work, except the windows where we embed the WebBrowser control via COM.  No matter what I tried, IOleObject::SetClientSite() would fail and the web browser would display as an empty white rectangle on the desktop instead of in our window.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/15c4ed10-21ae-45ac-85d9-416645e504ec"&gt;This guy&lt;/a&gt; seemed to have a very similiar symptom, but no answer; however, it was a fair guess he had the same problem and it was something to do with threading.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;One of the guys at AutoDESK Developer Support (thanks again) pointed out that the CLR initialises the main thread to use MTA (multi-thread-apartment) mode, whereas the WebBrowser control really only works with STA (single-thread-apartment) mode.  So, the solution was to ensure that I called &lt;a href="http://msdn.microsoft.com/en-us/library/ms678543.aspx"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;CoInitialize(NULL)&lt;/span&gt;&lt;/a&gt; in our main thread &lt;span class="Apple-style-span" style="font-style: italic;"&gt;before &lt;/span&gt;the CLR had a chance to mess things up.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-7000471344172482832?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/7000471344172482832/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/05/embedding-net-in-otherwise-native.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/7000471344172482832'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/7000471344172482832'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/05/embedding-net-in-otherwise-native.html' title='Embedding .NET in an otherwise native application'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-4583266606281146260</id><published>2009-03-31T15:01:00.003+11:00</published><updated>2009-03-31T15:05:48.702+11:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Developer Studio'/><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft'/><title type='text'>".PDB files cannot be linked due to incompatible versions"</title><content type='html'>&lt;span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;The message "&lt;span class="Apple-style-span" style="border-collapse: collapse; font-family: Arial; font-size: 13px; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;.PDB files cannot be linked due to incompatible versions&lt;/span&gt;" can be displayed during builds, even if the .PDB file has just been created by the current compilation (ie, its just not possible that it was built by a different version of the compiler)&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;&lt;br /&gt;The problem is that the &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;mspdbsrv.exe&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt; process (which Microsoft uses to create PDB files) does not always terminate after linking.  If you have a mixed environment where you compile and link some modules with VC7.0 and then some with VC8.0, sometimes the 8.0 &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;mspdbsrv.exe&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt; process hangs around and interferes with subsequent 7.0 compiles.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;Solution: use the Task Manager to terminate it.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-4583266606281146260?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/4583266606281146260/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/03/pdb-files-cannot-be-linked-due-to.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/4583266606281146260'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/4583266606281146260'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/03/pdb-files-cannot-be-linked-due-to.html' title='&quot;.PDB files cannot be linked due to incompatible versions&quot;'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-9112896587861076237</id><published>2009-03-31T14:34:00.010+11:00</published><updated>2009-05-13T11:29:35.770+10:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Windows'/><category scheme='http://www.blogger.com/atom/ns#' term='Developer Studio Express'/><category scheme='http://www.blogger.com/atom/ns#' term='MFC'/><title type='text'>Building MFC applications with Visual Studio .NET 2008 Express</title><content type='html'>&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;For reasons best known to themselves, Microsoft really really don't want you to build traditional MFC-based applications with their new Developer Studio tools - if you want the free stuff, you gotta trade in those testicles and do things with .NET or .nothing.  None of the MFC libraries come in the latest Express (read: free) versions of Developer Studio, nor do such useful tools as RC and MIDL.&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;However...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;You can get RC and MIDL from the Platform SDK.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=E6E1C3DF-A74F-4207-8586-711EBE331CDC&amp;amp;displaylang=en"&gt;http://www.microsoft.com/downloads/details.aspx?FamilyId=E6E1C3DF-A74F-4207-8586-711EBE331CDC&amp;amp;displaylang=en&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;And you can get an old version of the MFC and ATL libraries from the Driver Development Kit.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;a href="http://www.microsoft.com/whdc/DevTools/ddk/default.mspx"&gt;http://www.microsoft.com/whdc/DevTools/ddk/default.mspx&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;though in true Microsoft style, those headers contain compile errors.&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;Line 1034 and 1036 of …\include\mfc42\afxwin1.inl will probably contain the following:&lt;br /&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;_AFXWIN_INLINE CMenu::operator==(const CMenu&amp;amp; menu) const&lt;br /&gt;{ return ((HMENU) menu) == m_hMenu; }&lt;br /&gt;_AFXWIN_INLINE CMenu::operator!=(const CMenu&amp;amp; menu) const&lt;br /&gt;{ return ((HMENU) menu) != m_hMenu; }&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;which is incorrect – they generate error 4430.  It should  be:&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;_AFXWIN_INLINE &lt;span class="Apple-style-span" style="color: rgb(255, 0, 0);"&gt;BOOL &lt;/span&gt;CMenu::operator==(const CMenu&amp;amp; menu) const&lt;br /&gt;{ return ((HMENU) menu) == m_hMenu; }&lt;br /&gt;_AFXWIN_INLINE &lt;span class="Apple-style-span" style="color: rgb(255, 0, 0);"&gt;BOOL &lt;/span&gt;CMenu::operator!=(const CMenu&amp;amp; menu) const&lt;br /&gt;{ return ((HMENU) menu) != m_hMenu; }&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;(And people blame Windows crashes on "bad drivers" - how hard can it be to write a driver when the DDK has such blatant errors - they literally cannot have tried to compile with these headers before shipping them)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;Sadly, whats discussed here only works up to VC8.0 - at 9.0 the ATL changes are too drastic, as I found out the hard way.  If you want to use MFC or ATL, you can't use Visual Studio 2008 Express.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-9112896587861076237?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/9112896587861076237/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/03/building-mfc-applications-with-visual.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/9112896587861076237'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/9112896587861076237'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/03/building-mfc-applications-with-visual.html' title='Building MFC applications with Visual Studio .NET 2008 Express'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-5067411409613331299</id><published>2009-01-09T18:36:00.011+11:00</published><updated>2009-10-22T17:51:20.472+11:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='PDF'/><category scheme='http://www.blogger.com/atom/ns#' term='OSX'/><category scheme='http://www.blogger.com/atom/ns#' term='CoreGraphics'/><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>PDF to JPEG conversion</title><content type='html'>&lt;p&gt;Various PDFs collected from around the net would be better off as individual image files.  You'd think there'd be a standard tool to convert them but I couldn't find any at a price point I was interested in. Fortunate OSX Python has access to CoreGraphics which can do the heavy lifting. &lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;br /&gt;#!/usr/bin/python&lt;br /&gt;import sys,re,os,os.path&lt;br /&gt;from CoreGraphics import *&lt;br /&gt;&lt;br /&gt;def doit(pdfname):&lt;br /&gt;  if not re.search(".pdf$",pdfname): return&lt;br /&gt;  print pdfname&lt;br /&gt;  dirname = re.sub(".pdf$","",pdfname)&lt;br /&gt;  try:&lt;br /&gt;     os.mkdir(dirname)&lt;br /&gt;  except:&lt;br /&gt;     print "Can't create directory '%s'"%(dirname)&lt;br /&gt;  return&lt;br /&gt;  pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(pdfname))&lt;br /&gt;  cs = CGColorSpaceCreateDeviceRGB()&lt;br /&gt;  bg = CGFloatArray(5)       # create's an array of 5 0's which is good enough for me&lt;br /&gt;  for i in range(1, pdf.getNumberOfPages() + 1):&lt;br /&gt;     page = pdf.getPage(i)&lt;br /&gt;     r = page.getBoxRect(kCGPDFMediaBox)&lt;br /&gt;     h = r.getHeight()&lt;br /&gt;     w = r.getWidth()&lt;br /&gt;     del page&lt;br /&gt;&lt;br /&gt;     #c = CGBitmapContextCreateWithColor(int(w), int(h), cs, (0,0,0,0))&lt;br /&gt;     c = CGBitmapContextCreateWithColor(int(w), int(h), cs, bg))&lt;br /&gt;     c.saveGState()&lt;br /&gt;     c.setInterpolationQuality(kCGInterpolationHigh)&lt;br /&gt;     c.drawPDFDocument(r,pdf,i)&lt;br /&gt;     c.restoreGState()&lt;br /&gt;     c.writeToFile(os.path.join(dirname, "page%04d.jpg"%i),kCGImageFormatJPEG)&lt;br /&gt;     del c&lt;br /&gt;  del cs&lt;br /&gt;  del pdf&lt;br /&gt;&lt;br /&gt;if __name__=='__main__':&lt;br /&gt;  for a in sys.argv[1:]: doit(a)&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'times new roman';"&gt;The original version of this script was broken by Snow Leopard (which upgraded Python to 2.6.1).  The call to &lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;CGBitmapContextCreateWithColor()&lt;/span&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'times new roman';"&gt; failed with an error message about the 4th argument which it seems to think &lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;&lt;b&gt;shouldn't&lt;/b&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'times new roman';"&gt; be a 'const float[5]'.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman', serif;"&gt;The solution is to pass in a &lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;CGFloatArray()&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman', serif;"&gt; object instead.  I haven't been able to modify one of those, but the default thats produced when you use '&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;bg = CGFloatArray(5)&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman', serif;"&gt;' appears to be good enough.  Those objects still look leaky as hell but what are ya gonna do?&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;Squirrel:~ jeff$ python&lt;br /&gt;Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51)&lt;br /&gt;[GCC 4.2.1 (Apple Inc. build 5646)] on darwin&lt;br /&gt;Type "help", "copyright", "credits" or "license" for more information.&lt;br /&gt;&gt;&gt;&gt; from CoreGraphics import CGFloatArray&lt;br /&gt;&gt;&gt;&gt; a = CGFloatArray(5)&lt;br /&gt;&gt;&gt;&gt; print repr(a)&lt;br /&gt;&amp;lt;CoreGraphics.CGFloatArray; proxy of &amp;lt;Swig Object of type 'CGFloatArray *' at 0x2287a0&gt; &gt;&lt;br /&gt;&gt;&gt;&gt; print repr(a[0])&lt;br /&gt;swig/python detected a memory leak of type 'CGFloat *', no destructor found.&lt;br /&gt;&amp;lt;Swig Object of type 'CGFloat *' at 0x224d10&gt;&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-5067411409613331299?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/5067411409613331299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/01/various-pdfs-collected-from-around-net.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/5067411409613331299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/5067411409613331299'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/01/various-pdfs-collected-from-around-net.html' title='PDF to JPEG conversion'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-948868851971907265.post-7276512270919095983</id><published>2009-01-09T16:51:00.002+11:00</published><updated>2009-05-13T11:27:21.430+10:00</updated><title type='text'>Brave New World</title><content type='html'>Everyone else seems to be getting on to this bandwagon, so why shouldn't I?&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I doubt I'll have anything interesting to say to the world - I'm far more likely to use this to record notes to myself that may conceivably be useful to others.  And unlike most bloggers, I'm going to go back and edit anything that needs fixing rather than posting new articles.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/948868851971907265-7276512270919095983?l=stuffineededtoknow.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://stuffineededtoknow.blogspot.com/feeds/7276512270919095983/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/01/brave-new-world.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/7276512270919095983'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/948868851971907265/posts/default/7276512270919095983'/><link rel='alternate' type='text/html' href='http://stuffineededtoknow.blogspot.com/2009/01/brave-new-world.html' title='Brave New World'/><author><name>Jeff Laing</name><uri>http://www.blogger.com/profile/17500974943434382108</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
