Board logo

Subject: GetLongPathNameA function PROBLEM [Print This Page]

Author: mjasinski    Time: 2006-11-27 22:54     Subject: GetLongPathNameA function PROBLEM

File CnCommon.pas includes this lines (cnpack part):

function GetLongPathNameA(lpszShortPath: PAnsiChar; lpszLongPath: PAnsiChar;
  cchBuffer: DWORD): DWORD; stdcall; external 'kernel32.dll'
  name 'GetLongPathNameA';

The GetLongPathName API call is only available on Windows 98/ME and
Windows 2000/XP. It is not available on Windows 95 & NT.

But file JclFileUtils.cpp includes better solution (cnwizards part):

function RtdlGetLongPathName(const Path: string): string;
begin
  Result := Path;
  if not Assigned(_GetLongPathName) then
    _GetLongPathName := GetModuleSymbol(Kernel32Handle, 'GetLongPathNameA');
  if not Assigned(_GetLongPathName) then
    Result := ShellGetLongPathName(Path)
  else
  begin
    SetLength(Result, MAX_PATH);
    SetLength(Result, _GetLongPathName(PChar(Path), PChar(Result), MAX_PATH));
  end;
end;

I improved CnCommon.pas.
I have compiled sources and my CnWizards_CB6.dll works on WinNT correctly.

Could You apply this trick in CnCommon.pas file?

Mariusz Jasinski
Poland
Author: shenloqi    Time: 2006-11-29 10:07

Hi, the new code as below:

//----Code begin----
var
  _Kernel32Handle: HMODULE = HMODULE(0);
  _GetLongPathName: function (lpszShortPath: PAnsiChar; lpszLongPath: PAnsiChar;
    cchBuffer: DWORD): DWORD; stdcall;

function Kernel32Handle: HMODULE;
begin
  if _Kernel32Handle = HMODULE(0) then
    _Kernel32Handle := LoadLibrary(kernel32);
  Result := _Kernel32Handle;
end;

function ShellGetLongPathName(const Path: string): string;
var
  PIDL: PItemIDList;
  Desktop: IShellFolder;
  AnsiName: string;
  WideName: array [0..MAX_PATH] of WideChar;
  Eaten, Attr: ULONG;
begin
  Result := Path;
  if Path <> '' then
  begin
    if Succeeded(SHGetDesktopFolder(Desktop)) then
    begin
      MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PChar(Path), -1, WideName, MAX_PATH);
      if Succeeded(Desktop.ParseDisplayName(0, nil, WideName, Eaten, PIDL, Attr)) then
      try
        SetLength(AnsiName, MAX_PATH);
        if SHGetPathFromIDList(PIDL, PChar(AnsiName)) then
          StrResetLength(AnsiName);
        Result := AnsiName;
      finally
        CoTaskMemFree(PIDL);
      end;
    end;
  end;
end;

function ShortNameToLongName(const FileName: string): string;
begin
  Result := FileName;
  if not Assigned(_GetLongPathName) then
    _GetLongPathName := GetProcAddress(Kernel32Handle, 'GetLongPathNameA');
  if Assigned(_GetLongPathName) then
  begin
    SetLength(Result, MAX_PATH);
    SetLength(Result, _GetLongPathName(PChar(FileName), PChar(Result), MAX_PATH));
  end
  else
  begin
    Result := ShellGetLongPathName(FileName);
  end;
end;
//---- Code End ----

I haven't test it, could you test it?
If the code is OK, I will commit it.
Author: mjasinski    Time: 2006-11-29 18:50

I have tested it. No error.

In my compilation some icons near the form are very fuzzy (system Windows NT).
Could You compile and send me CnWizards_CB6.dll ?
email: mja@onet.pl

Mariusz Jasinski

Image Attachment: fuzzy_icons_2.gif (2006-11-29 18:50, 8.83 K) / Download count 579
http://bbs.cnpack.org/attachment.php?aid=229



Image Attachment: fuzzy_icons.gif (2006-11-29 18:50, 10.58 K) / Download count 579
http://bbs.cnpack.org/attachment.php?aid=230


Author: shenloqi    Time: 2006-11-29 20:34

I updated CnCommon.pas in CVS.

These icons looks ugly may because your operating system is WinNT, which not support 256 bit color icons.

I can not compile CnWizards_CB6.dll because I have no BCB, I am sorry for that.
Author: mjasinski    Time: 2006-11-30 16:18

Ok, thanks.
I will must change OS :-)
Author: shenloqi    Time: 2006-11-30 22:27

You're welcome




Welcome to CnPack Forum (http://bbs.cnpack.org/) Powered by Discuz! 5.0.0