{"id":4314,"date":"2014-03-30T09:50:28","date_gmt":"2014-03-30T09:50:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-a-syntax-of-declaring-api-function-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:50:28","modified_gmt":"2014-03-30T09:50:28","slug":"what-a-syntax-of-declaring-api-function-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-a-syntax-of-declaring-api-function-in-c-collection-of-common-programming-errors\/","title":{"rendered":"What a Syntax of declaring API function in C++?-Collection of common programming errors"},"content":{"rendered":"<p>Dave Doknjas wrote:<\/p>\n<p>&gt;<\/p>\n<p>&gt;(note that both of these are very Microsoft specific)<\/p>\n<p>Of course they are, since the subject is a Windows API.<\/p>\n<p>&gt;native C++: private:<\/p>\n<p>&gt;__declspec(dllimport) static long long URLDownloadToFile(long long pCaller, std::string szURL, std::string szFileName, long long dwReserved, long long lpfnCB);<\/p>\n<p>That&#8217;s wrong in virtually every respect. There are no Microsoft APIs that<\/p>\n<p>accept a std::string. Those need to be either &#8220;const char *&#8221; or &#8220;const<\/p>\n<p>wchar_t *&#8221; depending on the character set. The return value and dwReserved<\/p>\n<p>should both be just plain &#8220;long&#8221;, and pCaller and lpfnCB should both be<\/p>\n<p>&#8220;void *&#8221;.<\/p>\n<p>&gt;C++\/CLI: private:<\/p>\n<p>&gt;[System::Runtime::InteropServices::DllImport(&#8220;urlmon&#8221;, EntryPoint=&#8221;URLDownloadToFileA&#8221;, ExactSpelling=true, CharSet=System::Runtime::InteropServices::CharSet::Ansi, SetLastError=true)]<\/p>\n<p>&gt;static Int64 URLDownloadToFile(Int64 pCaller, String ^szURL, String ^szFileName, Int64 dwReserved, Int64 lpfnCB);<\/p>\n<p>&gt;<\/p>\n<p>&gt;You could include the appropriate header file, but that&#8217;s likely to bring in a lot of clutter you don&#8217;t need.<\/p>\n<p>That&#8217;s really bad advice for a Windows API. Windows APIs are almost never<\/p>\n<p>used in isolation. If he needs URLDownloadToFile, then he almost certainly<\/p>\n<p>needs the &#8220;clutter&#8221;.<\/p>\n<p>&#8212;<\/p>\n<p>Tim Roberts, timr@probo.com<\/p>\n<p>Providenza &amp; Boekelheide, Inc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dave Doknjas wrote: &gt; &gt;(note that both of these are very Microsoft specific) Of course they are, since the subject is a Windows API. &gt;native C++: private: &gt;__declspec(dllimport) static long long URLDownloadToFile(long long pCaller, std::string szURL, std::string szFileName, long long dwReserved, long long lpfnCB); That&#8217;s wrong in virtually every respect. There are no Microsoft APIs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4314","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4314"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4314\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}