<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Art Of Creation - Dynamics AX Blog &#187; WinAPI</title>
	<atom:link href="http://www.artofcreation.be/tag/winapi/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artofcreation.be</link>
	<description>The everyday life of a Dynamics AX developer</description>
	<lastBuildDate>Fri, 23 Jul 2010 16:29:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>WinAPI, RPC 1702 and FindFirstFileW</title>
		<link>http://www.artofcreation.be/2009/04/08/winapi-rpc-1702-and-findfirstfilew/</link>
		<comments>http://www.artofcreation.be/2009/04/08/winapi-rpc-1702-and-findfirstfilew/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 21:39:50 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[RPC]]></category>
		<category><![CDATA[System.IO]]></category>
		<category><![CDATA[WinAPI]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=46</guid>
		<description><![CDATA[A very common scenario when doing interfaces with other applications, is to have a directory where one application places files, and the other one picks those up and processes them. Let&#8217;s say some application want to interface with Dynamics AX, one way, from that application to AX. That application will drop files of a specific [...]]]></description>
			<content:encoded><![CDATA[<p>A very common scenario when doing interfaces with other applications, is to have a directory where one application places files, and the other one picks those up and processes them.<br />
Let&#8217;s say some application want to interface with Dynamics AX, one way, from that application to AX. That application will drop files of a specific format, say *.txt, in a directory. You&#8217;ll want to have a batch job in AX that checks that directory every 5 minutes for those files. When your batch job finds those files, it will open them and do something with that data, then move them to an other directory, so you don&#8217;t process them twice.</p>
<p>Now the first thing you&#8217;ll probably think of (well, I did&#8230;), is to use the class <a href="http://msdn.microsoft.com/en-us/library/aa678136(AX.10).aspx">WinAPI</a>. This class has some useful static methods for checking if folders or files exist, finding files with a filename of a certain format. Just the kind of thing we&#8217;re looking for. Your code might look like this:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> loopfilesWinApi<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> fileName;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> handle;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> format <span style="color: #00007f;">=</span> <span style="color: #ff0000;">&quot;*.txt&quot;</span>;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>WinApi<span style="color: #00007f;">::</span><span style="color: #000000;">folderExists</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;C:<span style="color: #000000;">\\</span>Temp<span style="color: #000000;">\\</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</span>handle<span style="color: #00007f;">,</span>fileName<span style="color: #000000;">&#93;</span> <span style="color: #00007f;">=</span> WinApi<span style="color: #00007f;">::</span><span style="color: #000000;">findFirstFile</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;C:<span style="color: #000000;">\\</span>Temp<span style="color: #000000;">\\</span>&quot;</span> <span style="color: #00007f;">+</span> format<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">//currentfileName = fileName;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>filename<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filename <span style="color: #00007f;">=</span> WinApi<span style="color: #00007f;">::</span><span style="color: #000000;">findNextFile</span><span style="color: #000000;">&#40;</span>handle<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; WinApi<span style="color: #00007f;">::</span><span style="color: #000000;">closeHandle</span><span style="color: #000000;">&#40;</span>handle<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>That&#8217;s perfect. It checks a directory, in this cas C:\temp, for files with the extension txt&#8230; <strong>until</strong> you try to run this in batch. Your batch will have the status error, and there will be an entry in the event log saying:</p>
<blockquote><p>RPC error: RPC exception 1702 occurred in session xxx</p></blockquote>
<p>That error message is very misleading, but <a href="http://blogs.msdn.com/floditt/archive/2009/01/29/rpc-error-1702-when-calling-a-winapi-method-from-a-batch-job.aspx">Florian Dittgen has a nice blog entry about this</a>, that explains why this goes wrong: WinAPI methods a static and run on client. In batch, you can not use these client methods.<br />
But hey! There is a class called WinAPIServer, surely this runs on server, right? Correct, but a lot of the methods available in WinAPI are missing in WinAPIServer. You can however extend the WinAPI server class by copying methods from the WinAPI class and modifying them a bit. Let&#8217;s try that.</p>
<p>This is what the WinAPI::FindFirstFile() method looks like:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">client</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">container</span> findFirstFile<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">str</span> filename<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Binary data <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> Binary<span style="color: #000000;">&#40;</span><span style="color: #000000;">592</span><span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// size of WIN32_FIND_DATA when sizeof(TCHAR)==2</span><br />
&nbsp; &nbsp; DLL _winApiDLL <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> DLL<span style="color: #000000;">&#40;</span>#KernelDLL<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; DLLFunction _findFirstFile <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> DLLFunction<span style="color: #000000;">&#40;</span>_winApiDLL<span style="color: #00007f;">,</span> <span style="color: #ff0000;">'FindFirstFileW'</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; _findFirstFile.<span style="color: #000000;">returns</span><span style="color: #000000;">&#40;</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">DWord</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; _findFirstFile.<span style="color: #000000;">arg</span><span style="color: #000000;">&#40;</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">WString</span><span style="color: #00007f;">,</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">Pointer</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">&#91;</span>_findFirstFile.<span style="color: #000000;">call</span><span style="color: #000000;">&#40;</span>filename<span style="color: #00007f;">,</span> data<span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span>data.<span style="color: #000000;">wString</span><span style="color: #000000;">&#40;</span>#offset44<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>As you can see, this method uses the function FindFirstFileW from the Kernel32 dll located in the folder C:\WINDOWS\system32. Let&#8217;s copy this method to WinAPIServer and modify it to look like this:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">server</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">container</span> findFirstFile<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">str</span> filename<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; #define.<span style="color: #000000;">KernelDLL</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'KERNEL32'</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; #define.<span style="color: #000000;">offset44</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">44</span><span style="color: #000000;">&#41;</span><br />
<br />
&nbsp; &nbsp; InteropPermission interopPerm;<br />
&nbsp; &nbsp; Binary data;<br />
&nbsp; &nbsp; DLL _winApiDLL;<br />
&nbsp; &nbsp; DLLFunction _findFirstFile;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; interopPerm <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> InteropPermission<span style="color: #000000;">&#40;</span>InteropKind<span style="color: #00007f;">::</span><span style="color: #000000;">DllInterop</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; interopPerm.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; data <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> Binary<span style="color: #000000;">&#40;</span><span style="color: #000000;">592</span><span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// size of WIN32_FIND_DATA when sizeof(TCHAR)==2</span><br />
&nbsp; &nbsp; _winApiDLL <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> DLL<span style="color: #000000;">&#40;</span>#KernelDLL<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; _findFirstFile <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> DLLFunction<span style="color: #000000;">&#40;</span>_winApiDLL<span style="color: #00007f;">,</span> <span style="color: #ff0000;">'FindFirstFileW'</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; _findFirstFile.<span style="color: #000000;">returns</span><span style="color: #000000;">&#40;</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">DWord</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; _findFirstFile.<span style="color: #000000;">arg</span><span style="color: #000000;">&#40;</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">WString</span><span style="color: #00007f;">,</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">Pointer</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">&#91;</span>_findFirstFile.<span style="color: #000000;">call</span><span style="color: #000000;">&#40;</span>filename<span style="color: #00007f;">,</span> data<span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span>data.<span style="color: #000000;">wString</span><span style="color: #000000;">&#40;</span>#offset44<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Three things have changed here:</p>
<ol>
<li>&#8220;Client&#8221; has been change to &#8220;Server&#8221; so it can run on server;</li>
<li>The code has been changed to use the InteropPermission class; this is required because the code runs on server;</li>
<li>Initialization of the variables is moved to after assertion of the InteropPermission.</li>
</ol>
<p>When you test this in batch (running on server), this will work, and you can do this for all WinAPI methods in the examples above&#8230; <strong>until</strong> you try this on a x64 architecture. You&#8217;ll receive a message saying that an error occurred when calling the FindFirstFileW function in the Kernel32 DLL (the exact message slipped my mind).<br />
The problem is described on the <a href="http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.axapta.programming&amp;tid=c5b2f7c6-0dc3-49f3-a49e-dcd836625563&amp;cat=&amp;lang=&amp;cr=&amp;sloc=&amp;p=1">Axapta Programming Newsgroup</a>. You can code your way around this problem, but I think the message is clear: <strong>don&#8217;t use WinAPI</strong>.</p>
<p>Instead, use the .NET Framework <strong>System.IO</strong> namespace (watch out for lowercase/uppercase here), in our case in specific, System.IO.File and System.IO.Directory.<br />
I get this nice code example from <a href="http://greg.agiletortoise.com/2007/04/02/dynamics-ax-making-net-calls-from-inside-ax/">Greg Pierce’s blog</a> (modified it a bit):</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> loopfilesSystemIO<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// loop all files that fit the required format</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> fileName;<br />
&nbsp; &nbsp; InteropPermission interopPerm;<br />
<br />
&nbsp; &nbsp; System.<span style="color: #000000;">Array</span> files;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> i<span style="color: #00007f;">,</span> j;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">container</span> fList;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; interopPerm <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> InteropPermission<span style="color: #000000;">&#40;</span>InteropKind<span style="color: #00007f;">::</span><span style="color: #000000;">ClrInterop</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; interopPerm.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; files <span style="color: #00007f;">=</span> System.<span style="color: #000000;">IO</span>.<span style="color: #000000;">Directory</span><span style="color: #00007f;">::</span><span style="color: #000000;">GetFiles</span><span style="color: #000000;">&#40;</span>@<span style="color: #ff0000;">&quot;C:<span style="color: #000000;">\t</span>emp&quot;</span><span style="color: #00007f;">,</span> <span style="color: #ff0000;">&quot;*.txt&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #000000;">&#40;</span> i<span style="color: #00007f;">=</span><span style="color: #000000;">0</span>; i<span style="color: #00007f;">&lt;</span>ClrInterop<span style="color: #00007f;">::</span><span style="color: #000000;">getAnyTypeForObject</span><span style="color: #000000;">&#40;</span>files.<span style="color: #000000;">get_Length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; i<span style="color: #00007f;">++</span> <span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fList <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conins</span><span style="color: #000000;">&#40;</span>fList<span style="color: #00007f;">,</span> <span style="color: #0000ff;">conlen</span><span style="color: #000000;">&#40;</span>fList<span style="color: #000000;">&#41;</span><span style="color: #00007f;">+</span><span style="color: #000000;">1</span><span style="color: #00007f;">,</span> ClrInterop<span style="color: #00007f;">::</span><span style="color: #000000;">getAnyTypeForObject</span><span style="color: #000000;">&#40;</span>files.<span style="color: #000000;">GetValue</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #000000;">&#40;</span>j<span style="color: #00007f;">=</span><span style="color: #000000;">1</span>;j<span style="color: #00007f;">&amp;</span>lt;<span style="color: #00007f;">=</span> <span style="color: #0000ff;">conlen</span><span style="color: #000000;">&#40;</span>fList<span style="color: #000000;">&#41;</span>; j<span style="color: #00007f;">++</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fileName <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conpeek</span><span style="color: #000000;">&#40;</span>fList<span style="color: #00007f;">,</span> j<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>This will run on client and on server without any problems. To have clean code, you could create your own class, just like WinAPI, but using the System.IO namespace. That way, you can reuse this code and save time.</p>
<p>Conclusion: abandon WinAPI, and use System.IO instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2009/04/08/winapi-rpc-1702-and-findfirstfilew/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
