<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>16.7ms in the life : Windows</title><link>http://blogs.msdn.com/peterrosser/archive/tags/Windows/default.aspx</link><description>Tags: Windows</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Using exported DLL functions</title><link>http://blogs.msdn.com/peterrosser/archive/2006/02/22/ExportedDllFunctions.aspx</link><pubDate>Thu, 23 Feb 2006 01:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:537396</guid><dc:creator>Peter Rosser</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/peterrosser/comments/537396.aspx</comments><wfw:commentRss>http://blogs.msdn.com/peterrosser/commentrss.aspx?PostID=537396</wfw:commentRss><wfw:comment>http://blogs.msdn.com/peterrosser/rsscomments.aspx?PostID=537396</wfw:comment><description>&lt;P&gt;Now and then it's necessary to use "private" or "internal-only" functions in DLLs that you did not write, don't have the source for, and/or cannot get public interfaces for.&amp;nbsp; I recently had to do this again for a utility application, so I thought it would be nice to document the process a little more clearly for others.&lt;/P&gt;
&lt;P&gt;A starting assumption is that the reader is familiar with GetProcAddress() in its "main" form, which is when you know the full signature of the function you need to call.&amp;nbsp; For example, if you have a copy of foo.dll that exports Bar(LPWSTR, DWORD*):&lt;/P&gt;&lt;FONT color=#008000 size=2&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;// library function is: Bar(LPWSTR lpParam, DWORD *pdwParam)&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;typedef&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;FONT size=2&gt; (&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;__stdcall&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; *BAR_PROC)(LPWSTR, DWORD*);&lt;BR&gt;&lt;BR&gt;HANDLE hFoo = LoadLibrary(&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"foo.dll"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;);&lt;BR&gt;BAR_PROC proc = (BAR_PROC)GetProcAddress(hFoo, &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"Bar"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;);&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#008000 size=2&gt;// call the library function&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;DWORD dwParam = 0x4;&lt;BR&gt;(proc)(L&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"string"&lt;/FONT&gt;&lt;FONT size=2&gt;, &amp;amp;dwParam);&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;But what if you need to access a function that is not included in the named exports of the library?&amp;nbsp; 90% of the time the functions will be exported with a name, but sometimes not, and for various reasons.&amp;nbsp; The most common reason is for "security through obscurity", because using the function is supposed to be something that you, the user, "should never have to do".&amp;nbsp; Unfortunately, there are scenarios when you need to do just that, for reasons the original publisher never considered, such as if you need to tweak the way something behaves in your mission-critical server process, and the company that made the original custom component has gone out of business.&lt;/P&gt;
&lt;P&gt;Enter the "ordinal" function access method.&amp;nbsp; If you look at the MSDN docs, they even allude to using a function's ordinal with GetProcAddress, but it's a bit of a mystery how to a) get the ordinal, and b) how to use it even if you get it.&amp;nbsp; Once you know the ordinal and prototype of a function, it's simple to use it--getting the function prototypes is the fun part.&lt;/P&gt;
&lt;P&gt;Say you got your hands on a .DEF file that lists the function ordinals, or have used link.exe and&amp;nbsp;through sufficiently wizardly use of a hex editor have examined the [NONAME] functions and determined what their prototypes are.&amp;nbsp; Let's pretend that the Bar function above was actually a [NONAME] function, and you got a listing like this from link.exe /dump /exports foo.dll:&lt;/P&gt;&lt;PRE&gt;Microsoft (R) COFF/PE Dumper Version 7.10.4035 
Copyright (C) Microsoft Corporation. All rights reserved. 


Dump of file c:\myprojects\foo.dll 

File Type: DLL

  Section contains the following exports for foo.dll

    00000000 characteristics 
    41107692 time date stamp Tue Aug 03 22:39:30 2005 
        0.00 version 
           1 ordinal base 
           6 number of functions 
           4 number of names 

    ordinal hint RVA      name 
          3    0 00006948 Connect 
          4    1 00007798 Close 
          5    2 00012000 GetFoo 
          6    3 00014D12 SetFoo 
          1      0000F3A0 [NONAME] 
          2      0000D20A [NONAME]

  Summary

        6000 .data
        2000 .reloc
        9000 .rsrc
       18000 .text
&lt;/PRE&gt;
&lt;P&gt;The depends.exe tool provides similar data in a GUI fashion.&amp;nbsp;You can examine the binary at offsets F3A0 and D20A to determine what the function signature is (well, what the base pointer sig is), and match them up to the expected signature of Bar(LPWSTR, DWORD*) manually to get the proper ordinal.&amp;nbsp; Or more easily, if you know that one of the methods is the droid you are looking for, you can just code up a quick test app and poke at the method as if it were the one you wanted.&amp;nbsp; If it works without throwing an exception... ;-)&lt;/P&gt;
&lt;P&gt;Getting back to the main point, to load the exported-by-ordinal function, there's a handy macro pre-defined in the Visual Studio (v6 or later, IIRC) build environment&amp;nbsp;that can package up that ordinal into the "expected" LPSTR for GetProcAddress.&amp;nbsp; In my example, assuming Bar was actually at ordinal 1, you would do this:&lt;/P&gt;&lt;FONT color=#008000 size=2&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;// library function is: Bar(LPWSTR lpParam, DWORD *pdwParam)&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;typedef&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;FONT size=2&gt; (&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;__stdcall&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; *BAR_PROC)(LPWSTR, DWORD*);&lt;BR&gt;&lt;BR&gt;HANDLE hFoo = LoadLibrary(&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"foo.dll"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;);&lt;BR&gt;BAR_PROC proc = (BAR_PROC)GetProcAddress(hFoo, MAKEINTRESOURCEA(1));&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#008000 size=2&gt;// call the library function&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;DWORD dwParam = 0x4;&lt;BR&gt;(proc)(L&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"string"&lt;/FONT&gt;&lt;FONT size=2&gt;, &amp;amp;dwParam);&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;The only difference is that you use the MAKEINTRESOURCEA macro to convert that ordinal into something that GetProcAddress can understand, rather than the string name of the function.&lt;/P&gt;
&lt;P&gt;If you really want to get tricky, you don't actually need GetProcAddress at all to do this magic.&amp;nbsp; If you can guarantee that the DLL won't change underneath you, or you don't mind breaking if it does, or you have smart scanning code that can "find" the offsets again, you can plug the RVA directly into the FARPROC variable.&amp;nbsp; It's all just numbers under the hood, after all.&amp;nbsp; Using the example output of link.exe from above, you can do this:&lt;/P&gt;&lt;FONT color=#008000 size=2&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;// library function is: Bar(LPWSTR lpParam, DWORD *pdwParam)&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;typedef&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;FONT size=2&gt; (&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;__stdcall&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; *BAR_PROC)(LPWSTR, DWORD*);&lt;BR&gt;&lt;FONT color=#0000ff&gt;#define&lt;/FONT&gt;&lt;FONT color=#000000&gt; PROC_OFFSET 0x0000F3A0&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;HANDLE hFoo = LoadLibrary(&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"foo.dll"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;);&lt;BR&gt;BAR_PROC proc = (BAR_PROC)(hFoo + PROC_OFFSET)&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;;&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#008000 size=2&gt;// call the library function&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;DWORD dwParam = 0x4;&lt;BR&gt;(proc)(L&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"string"&lt;/FONT&gt;&lt;FONT size=2&gt;, &amp;amp;dwParam);&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;Why does this work?&amp;nbsp; LoadLibrary(dll) loads the specified library into the process' address space, and returns a HANDLE value.&amp;nbsp; This HANDLE is the starting address of the library, and since the RVA values for a DLL are offsets from the starting address, you can perform simple addition to the base to get a function's entry point.&amp;nbsp; Pretty cool, huh?&amp;nbsp; All GetProcAddress does is read the DLL's export table into a struct and performs the math for you, returning the result.&amp;nbsp; I'm a big fan of not reinventing the wheel, so I use GetProcAddress when there's an exports entry for what I need.&lt;/P&gt;
&lt;P&gt;What do you do if the function you need is not exported at all?&amp;nbsp; Nine times out of ten it's much easier to just get it exported, but if that's not possible, well, that's a topic for another day.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=537396" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/peterrosser/archive/tags/Coding/default.aspx">Coding</category><category domain="http://blogs.msdn.com/peterrosser/archive/tags/Windows/default.aspx">Windows</category></item><item><title>Windows Insomnia: Why Won't My Computer Go To Sleep?</title><link>http://blogs.msdn.com/peterrosser/archive/2006/01/04/WindowsInsomnia.aspx</link><pubDate>Thu, 05 Jan 2006 08:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:509518</guid><dc:creator>Peter Rosser</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/peterrosser/comments/509518.aspx</comments><wfw:commentRss>http://blogs.msdn.com/peterrosser/commentrss.aspx?PostID=509518</wfw:commentRss><wfw:comment>http://blogs.msdn.com/peterrosser/rsscomments.aspx?PostID=509518</wfw:comment><description>&lt;P&gt;One of the issues I am investigating for a fix relates to the auto-suspend feature in Windows.&amp;nbsp; The way it's supposed to work is you set a time in the Power control panel (Control Panel --&amp;gt; Performance and Maintenance --&amp;gt; Power Options) that controls how long the computer waits until it goes into "standby" or "suspend".&amp;nbsp; Whether that is S2 or S3 depends on your BIOS settings.&amp;nbsp; What's going on with the bug I'm looking at is sometimes Windows will not suspend when it should, or indeed, ever, unless you close Media Center or manually select Standby.&lt;/P&gt;
&lt;P&gt;This is pretty annoying, especially since my Media Center consumes about 200W while fully active (250-275W if engaged in HDTV playback), and only about 15W in S3 suspend.&amp;nbsp; With electricity around $0.084 / kWh, that works out to $0.40 each day left unchecked.&amp;nbsp; If I let it go all month, that's another $12, just because of this stupid bug (well, and because I'm lazy and forget to manually suspend... but I should not &lt;EM&gt;have&lt;/EM&gt; to).&amp;nbsp; Lame, lame, lame.&amp;nbsp; Extrapolating to all of the customers affected by this issue, I can see it adding up to a big problem.&amp;nbsp; Say only 250,000 users hit this (a low figure, I bet), that's $3,000,000 per month in wasted energy costs, based on the rates I pay.&amp;nbsp; Yikes!&lt;/P&gt;
&lt;P&gt;While I can fix this problem, I cannot fix all of the things that prevent computers from going into standby automatically, because they should really not be fixed, or because Windows software is not causing the problem in the first place.&amp;nbsp; Several things can cause a computer to fail to go into standby, and figuring out exactly what is causing the problem can be very difficult.&lt;/P&gt;
&lt;P&gt;So, what are the most common causes of Windows insomnia?&amp;nbsp; In rough order of likelihood, here is my &lt;A href="http://en.wikipedia.org/wiki/SWAG"&gt;SWAG&lt;/A&gt;:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;There is user activity.&amp;nbsp; This usually means keyboard or mouse activity, but can be "simulated" by applications via SendKeys(), etc. 
&lt;LI&gt;Windows is not configured to go into Standby automatically.&amp;nbsp; Since it's not the default for most computers, this&amp;nbsp;makes the list.&amp;nbsp; Easy to fix. 
&lt;LI&gt;An application tells Windows not to go into Standby automatically.&amp;nbsp; Different from user activity simulation because this is what applications are &lt;EM&gt;supposed&lt;/EM&gt; to do to prevent Standby. [ &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/power/base/setthreadexecutionstate.asp"&gt;SetThreadExecutionState(&lt;EM&gt;state&lt;/EM&gt;)&lt;/A&gt;&amp;nbsp;] 
&lt;LI&gt;An application generates enough system activity (e.g. disk access, network access, CPU usage) that Windows does not consider the machine to be "idle" enough. 
&lt;LI&gt;A hardware device driver prevents it.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Case 1 is very unlikely, since really the only applications that do this sort of thing are hacks/trainers/evil programs (and testing programs, if you want a legitimate example).&amp;nbsp; Unless your computer is infected by malware, this is probably not the cause.&amp;nbsp; Run a quality scanner to tell.&amp;nbsp; For case 2, just set Control Panel setting.&lt;/P&gt;
&lt;P&gt;For cases 3 and 4, troubleshooting gets trickier.&amp;nbsp; For the average end-user who does not know how to use a Kernel Debugger like &lt;A href="http://www.microsoft.com/whdc/devtools/debugging/default.mspx"&gt;WinDbg&lt;/A&gt; or &lt;A href="http://www.compuware.com/products/driverstudio/softice.htm"&gt;SoftICE&lt;/A&gt;, and does not have &lt;A href="http://www.microsoft.com/whdc/devtools/debugging/debugstart.mspx"&gt;easy access to kernel symbols&lt;/A&gt;, figuring out who is keeping the computer up all night takes a little wanton process termination.&amp;nbsp;Before you open up taskmgr and start gunning processes, though, there are a couple of things you can check:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Are you sharing any folders on the network?&amp;nbsp; If a remote computer is connected to your machine and refreshes the connection (i.e. sends a command), the Server service will call KeInhibitSystemShutdown(), which does exactly what it sounds like it does: resets the system's idle timer.&amp;nbsp; How do you tell if this is happening? An easy way is to pull up a command window (Start --&amp;gt; Run --&amp;gt; cmd) and type:&lt;BR&gt;&lt;BR&gt;&lt;FONT face="Courier New"&gt;nbtstat -s&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;If you see any "IN" connections, they can keep you awake.&amp;nbsp; Most well-behaved Windows applications will not needlessly refresh connections over the network, but others don't play that nicely.&amp;nbsp; A ubiquitous example would be WMP or iTunes--both will scan network folders for changes if you add the path to your "watched folders" list.&amp;nbsp; The best way to solve this problem is to fix the client side (the other computer), but if that's not possible, you can just deny access to that computer/user.&amp;nbsp; In the worst case, you can stop the Server service (&lt;FONT face="Courier New"&gt;net stop server&lt;/FONT&gt;), but that prevents sharing altogether. 
&lt;LI&gt;Check your Application and System event logs for errors or warnings.&amp;nbsp; Often, exceptional situations will be reported there and can provide a clue to why Windows is not going into Standby. 
&lt;LI&gt;Hardware (or more appropriately, drivers for hardware) can prevent Windows from entering standby, too.&amp;nbsp; If you used to be able to go into standby, but cannot now, have you added or replaced hardware, or updated drivers?&amp;nbsp; This can be tricky, too, since most people set their Standby timers for longer than is convenient to sit around and wait on, so failure to go into Standby can be missed for days or weeks. 
&lt;LI&gt;To eliminate any remote networking cause, isolate your computer from the network (unplug the network cable!).&amp;nbsp; Low-tech, but effective.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If you have eliminated network shares, suspicious event log entries, and hardware from causing the problem, the next thing to do is start killing processes and see which one (or if you're unlucky, which ones) are doing it.&amp;nbsp; You need to first exclude those processes that are part of Windows itself, since terminating them is not very wise.&amp;nbsp; An easy way to get a fairly complete list of the Windows processes is to boot into Safe Mode, press Start --&amp;gt; Run, and type:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;tasklist &amp;gt;"%SystemDrive%\SysProcesses.txt"&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;A new file will be created on your system drive (usually C:) that contains the list of processes you shouldn't touch.&amp;nbsp; Reboot back into normal Windows mode, log in, and:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open the SysProcesses.txt file 
&lt;LI&gt;Start taskmgr (right click on the taskbar and select &lt;STRONG&gt;Task Manager&lt;/STRONG&gt;) and click the Processes tab. 
&lt;LI&gt;Set your Standby timer to 1 minute (in the Power Options control panel) 
&lt;LI&gt;Let&amp;nbsp;1 minute elapse.&amp;nbsp; If the computer does not suspend, proceed to the next step. 
&lt;LI&gt;Select a process not listed in the SysProcesses.txt file in taskmgr and kill it. 
&lt;LI&gt;Go to step 4.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;If you run out of processes to kill and the computer still won't go into Standby, it's time to start shutting down services in the Services console (Start --&amp;gt; Run --&amp;gt; &lt;FONT face="Courier New"&gt;services.msc&lt;/FONT&gt;).&amp;nbsp; If you have stopped all of the services you possibly can, it's either a piece of hardware or one of the core Windows processes, and things are a bit tougher.&amp;nbsp; To troubleshoot those, you need the help of a kernel-mode debugger.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=509518" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/peterrosser/archive/tags/Coding/default.aspx">Coding</category><category domain="http://blogs.msdn.com/peterrosser/archive/tags/Musing/default.aspx">Musing</category><category domain="http://blogs.msdn.com/peterrosser/archive/tags/Windows/default.aspx">Windows</category></item></channel></rss>