Einzelnen Beitrag anzeigen

Benutzerbild von Phoenix
Phoenix
(Moderator)
Online

Registriert seit: 25. Jun 2002
Ort: Hausach
7.609 Beiträge
 
#9

Re: [C/C++] cannot convert `const wchar_t*' to `const char*'

  Alt 2. Okt 2006, 09:31
Das sind genauch die Ecken warum ich C/C++ nicht wirklich mag. Dieses ganze rumgepointere auf die verschiedensten Typen kann einen noch verrückt machen.

Aber hier mal etwas Code zu GetProcAddress, den ich mir irgendwann mal für ein FH-Projekt ausm Netz zusammengesucht hab als ich den Program Files - Folder gesucht hab:

Code:
#include <windows.h>
#include <tchar.h>
#include <shlobj.h>

#define NUM_ELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))

LPCTSTR lpszShell32  = TEXT("shell32.dll");
LPCTSTR lpszSHFolder = TEXT("shfolder.dll");

#ifdef UNICODE
  LPCSTR lpszGFP = "SHGetFolderPathW";
#else
  LPCSTR lpszGFP = "SHGetFolderPathA";
#endif

typedef HRESULT (WINAPI * SHGETFOLDERPATH)(HWND, int, HANDLE, DWORD, LPTSTR);

UINT_PTR GetProgramFilesFolder(LPTSTR lpFolder, UINT cchMax)
{
  SHGETFOLDERPATH pfnGFP;
  HINSTANCE      hDll;
  UINT_PTR       cch = 0;
  TCHAR          szFolder[MAX_PATH * 2];

    if(NULL != (hDll = LoadLibrary(lpszShell32)))
    {
        if(NULL != (pfnGFP = (SHGETFOLDERPATH)GetProcAddress(hDll, lpszGFP)))
        {
            if(SUCCEEDED(pfnGFP(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, szFolder)))
            {
                _tcsncpy(lpFolder, szFolder, min(cchMax, NUM_ELEMENTS(szFolder)));
                lpFolder[cchMax - 1] = TEXT('\0');
                cch = _tcslen(lpFolder);
            }
        }
        FreeLibrary(hDll);
    }

    if(!cch)
    {
        if(NULL != (hDll = LoadLibrary(lpszSHFolder)))
        {
            if(NULL != (pfnGFP = (SHGETFOLDERPATH)GetProcAddress(hDll, lpszGFP)))
            {
                if(SUCCEEDED(pfnGFP(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, szFolder)))
                {
                    _tcsncpy(lpFolder, szFolder, min(cchMax, NUM_ELEMENTS(szFolder)));
                    lpFolder[cchMax - 1] = TEXT('\0');
                    cch = _tcslen(lpFolder);
                }
            }
            FreeLibrary(hDll);
        }
    }

  return(cch);
}
Wie Du siehst wird in GetProcAddress einfach nur ein LPCSTR reingeworfen, und der wird je nach Bedarf (Unicode / nicht unicode) gefüllt.
Sebastian Gingter
Phoenix - 不死鳥, Microsoft MVP, Rettungshundeführer
Über mich: Sebastian Gingter @ Thinktecture Mein Blog: https://gingter.org
  Mit Zitat antworten Zitat