What a Syntax of declaring API function in C++?-Collection of common programming errors

Dave Doknjas wrote:

>

>(note that both of these are very Microsoft specific)

Of course they are, since the subject is a Windows API.

>native C++: private:

>__declspec(dllimport) static long long URLDownloadToFile(long long pCaller, std::string szURL, std::string szFileName, long long dwReserved, long long lpfnCB);

That’s wrong in virtually every respect. There are no Microsoft APIs that

accept a std::string. Those need to be either “const char *” or “const

wchar_t *” depending on the character set. The return value and dwReserved

should both be just plain “long”, and pCaller and lpfnCB should both be

“void *”.

>C++/CLI: private:

>[System::Runtime::InteropServices::DllImport(“urlmon”, EntryPoint=”URLDownloadToFileA”, ExactSpelling=true, CharSet=System::Runtime::InteropServices::CharSet::Ansi, SetLastError=true)]

>static Int64 URLDownloadToFile(Int64 pCaller, String ^szURL, String ^szFileName, Int64 dwReserved, Int64 lpfnCB);

>

>You could include the appropriate header file, but that’s likely to bring in a lot of clutter you don’t need.

That’s really bad advice for a Windows API. Windows APIs are almost never

used in isolation. If he needs URLDownloadToFile, then he almost certainly

needs the “clutter”.

Tim Roberts, [email protected]

Providenza & Boekelheide, Inc.