{"id":481,"date":"2017-09-20T21:02:43","date_gmt":"2017-09-20T20:02:43","guid":{"rendered":"http:\/\/510245153.swh.strato-hosting.eu\/?p=481"},"modified":"2017-09-20T21:02:43","modified_gmt":"2017-09-20T20:02:43","slug":"dynamically-load-a-windows-api-function","status":"publish","type":"post","link":"https:\/\/thomas-jansen.eu\/?p=481","title":{"rendered":"Dynamically load a Windows API function"},"content":{"rendered":"<p>Sometimes an API function is nice to have but will break compatibility with previous versions of Windows. It is possible to load functions dynamically.<\/p>\n<p>For example, we will use the function\u00a0SHDefExtractIconW. Using it will break Windows 2000 compatibility. Whether that is a problem I&#8217;ll leave that up to you.<\/p>\n<p>The windows prototype for this function is:<\/p>\n<p>HRESULT SHDefExtractIcon(<br \/>\n_In_ LPCTSTR pszIconFile,<br \/>\nint iIndex,<br \/>\n_In_ UINT uFlags,<br \/>\n_Out_opt_ HICON *phiconLarge,<br \/>\n_Out_opt_ HICON *phiconSmall,<br \/>\nUINT nIconSize<br \/>\n);<\/p>\n<p>Most API functions use the calling definition __stdcall. Omitting this will lead to stack errors or malfunctioning functions. A variable of the function prototype must be made. It will have the following form:<\/p>\n<p>HRESULT (__stdcall *SHDefExtractIcon)(<br \/>\n_In_ LPCTSTR pszIconFile,<br \/>\nint iIndex,<br \/>\n_In_ UINT uFlags,<br \/>\n_Out_opt_ HICON *phiconLarge,<br \/>\n_Out_opt_ HICON *phiconSmall,<br \/>\nUINT nIconSize<br \/>\n);<\/p>\n<p>Note that the name SHDefExtractIcon is free to choose since this is just a variable declaration.<\/p>\n<p>You can also remove the variable names, leaving them will make it somewhat self-documenting.<\/p>\n<p>Now load the DLL using:<\/p>\n<p>HMODULE module = LoadLibrary(L&#8221;shell32.dll&#8221;);<\/p>\n<p>and the function using:<\/p>\n<p>SHDefExtractIcon = ( HRESULT(__stdcall *)(LPCTSTR, int, UINT, HICON *, HICON *, UINT))<br \/>\nGetProcAddress(module,&#8221;SHDefExtractIconW&#8221;);<\/p>\n<p>Here the variable names are removed for clarity. note that API functions accepting string arguments are often available in two variant e.g. MessageBoxA and MessageBoxW, you can use both. The compiler will create an define MessageBox to either one depending on the compiler setting, Multibyte (A) or Unicode(W).<\/p>\n<p>GetProcAddress will return NULL if the function can not be found.<\/p>\n<p>Now the functionn can be called:<\/p>\n<p>(*SHDefExtractIcon)(L&#8221;c:\\\\Windows\\\\System32\\\\shell32.dll&#8221;, 0,0, &amp;largeIcon, &amp;smallIcon, MAKELPARAM(256, 0));<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes an API function is nice to have but will break compatibility with previous versions of Windows. It is possible to load functions dynamically. For example, we will use the function\u00a0SHDefExtractIconW. Using it will break Windows 2000 compatibility. Whether that is a problem I&#8217;ll leave that up to you. The windows prototype for this function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-481","post","type-post","status-publish","format-standard","hentry","category-win3264-api-c"],"_links":{"self":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=481"}],"version-history":[{"count":1,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/481\/revisions"}],"predecessor-version":[{"id":482,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/481\/revisions\/482"}],"wp:attachment":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}