linking shlwapi.h for to StrCmpLogicalW function
Any idea how to port this code from C# to C++?
[DllImport("shlwapi.dll",开发者_如何学JAVA CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string psz1, string psz2);
If I tried this line in my header file in C++, the compiler generated a bunch of error.
#include <Shlwapi.h>
The errors are linker errors because you haven't included the corresponding .lib file, Shlwapi.lib.
You can include the .lib file in the project settings, or alternatively by adding this
#pragma comment(lib, "Shlwapi.lib")
to, for example, StdAfx.h.
I finally got it figured out. Here is the function. It is critical that you put the shlwapi.h and vcclr.h header files before any of your own header files if you have any. That was the issue I have been struggled with. Don't fully understand why it is. If anyone have an good explanation, welcome to make comment. Also, if anyone know how to combine the last three line of code to a single return statement, welcome to add comment.
#include "shlwapi.h" //needed this for StrCmpLogicalW
#include <vcclr.h> //needed this for PtrtoStringChars
//your own header files
ref class FileInfoNameComparer: public IComparer
{
private:
virtual int Compare( Object^ x, Object^ y ) sealed = IComparer::Compare
{
FileInfo^ objX = gcnew FileInfo(x->ToString());
FileInfo^ objY = gcnew FileInfo(y->ToString());
pin_ptr<const wchar_t> wch1 = PtrToStringChars(objX->Name);
pin_ptr<const wchar_t> wch2 = PtrToStringChars(objY->Name);
return StrCmpLogicalW(wch1, wch2);
}
};
精彩评论