0

我需要在 C++ MFC 中执行与下面的 C# .net 代码相同的操作,你们能帮帮我吗?

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("shell32.dll", EntryPoint = "#261",
            CharSet = CharSet.Unicode, PreserveSig = false)]
        public static extern void GetUserTilePath(
          string username, 
          UInt32 whatever, // 0x80000000
          StringBuilder picpath, int maxLength);

        public static string GetUserTilePath(string username)
        {   // username: use null for current user
            var sb = new StringBuilder(1000);
            GetUserTilePath(username, 0x80000000, sb, sb.Capacity);
            return sb.ToString();
        }

        [STAThread]
        static void Main(string[] args)
        {
            string path =GetUserTilePath(null);
            Console.WriteLine("path = %s", path);
        }
    }
}

我无法从 #261(或 261)入口点和/或其对 C++ 的确切签名访问 GetProcAddress。

编辑:这是诀窍

HMODULE shell32Dll= ::LoadLibrary(L"shell32.dll");


HRESULT (__stdcall *getUserImage)(LPCWSTR userName,
                        DWORD zero , LPWSTR outPath, UINT size);

(FARPROC&)getUserImage = ::GetProcAddress(shell32Dll, MAKEINTRESOURCEA(261));
if(getUserImage)
{
        WCHAR outPath[MAX_PATH];
        getUserImage(NULL,0x80000000,outPath,MAX_PATH);
}

MAKEINTRESOURCEA(261) 而不是 "#261" 或 "261" 让工作完成!

谢谢!卡梅伦!

4

1 回答 1

0

GetProcAddr 文档

lpProcName [in]
函数或变量名,或函数的序数值。如果这个参数是一个序数值,它必须在低位字中;高位字必须为零。

事实证明你应该这样称呼它:

(FARPROC&)getUserImage = ::GetProcAddress(shell32Dll, MAKEINTRESOURCE(261));
于 2012-04-26T18:47:22.997 回答