CnPack Forum » CnWizards IDE Wizards » GetLongPathNameA function PROBLEM


2006-11-27 22:54 mjasinski
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

2006-11-29 10:07 shenloqi
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.

2006-11-29 18:50 mjasinski
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: [email]mja@onet.pl[/email]

Mariusz Jasinski

2006-11-29 20:34 shenloqi
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.

2006-11-30 16:18 mjasinski
Ok, thanks.
I will must change OS :-)

2006-11-30 22:27 shenloqi
You're welcome :)

页: [1]


Powered by Discuz! Archiver 5.0.0  © 2001-2006 Comsenz Inc.