如何使用GetCORSystemDirectory()?

时间:2009-07-23 09:56:53

标签: c++ visual-c++ clr

HANDLE Proc;
HMODULE hDLL;
hDLL = LoadLibrary(TEXT("mscoree.dll"));
if(hDLL == NULL)
    cout << "No Dll with Specified Name" << endl;
else
    {

    cout << "DLL Handle" << hDLL << endl<<endl;
    cout << "Getting the process address..." << endl;
    Proc = GetProcAddress(hDLL,"GetRequestedRuntimeVersion");

    if(Proc == NULL)
        {
         FreeLibrary(hDLL);
         cout << "Process load FAILED" << endl;
        }

    else
        {
         cout << "Process address found at: " << Proc << endl << endl;  
         LPWSTR st;DWORD* dwlength; ;DWORD cchBuffer=MAX_PATH;
         HRESULT hr=GetCORSystemDirectory(st,cchBuffer,dwlength);
         if(hr!=NULL)
         {
            printf("%s",hr);
        }
        FreeLibrary(hDLL);
        }
    }   

我这样做是为了获得.NET安装路径,但我得到了链接器错误。

错误LNK2019:函数_main dot.obj中引用的未解析的外部符号_GetCORSystemDirectory @ 12

2 个答案:

答案 0 :(得分:1)

定义GetCORSystemDirectory签名:

typedef  HRESULT  ( __stdcall *FNPTR_GET_COR_SYS_DIR) ( LPWSTR pbuffer, DWORD cchBuffer, DWORD* dwlength);

初始化函数指针:

FNPTR_GET_COR_SYS_DIR   GetCORSystemDirectory = NULL;

从mscoree.dll获取一个函数指针并使用:

GetCORSystemDirectory = (FNPTR_GET_COR_SYS_DIR) GetProcAddress (hDLL, "GetCORSystemDirectory"); 
if( GetCORSystemDirectory!=NULL)
{
    ...
    //use GetCORSystemDirectory
    ...
}

根据要求:

#ifndef _WIN32_WINNT            
#define _WIN32_WINNT 0x0600     
#endif
#include <stdio.h>
#include <tchar.h>
#include <windows.h>

typedef  HRESULT  (__stdcall *FNPTR_GET_COR_SYS_DIR) ( LPWSTR pbuffer, DWORD cchBuffer, DWORD* dwlength);
FNPTR_GET_COR_SYS_DIR   GetCORSystemDirectory = NULL;

int _tmain(int argc, _TCHAR* argv[])
{
    HINSTANCE hDLL = LoadLibrary(TEXT("mscoree.dll"));

    GetCORSystemDirectory = (FNPTR_GET_COR_SYS_DIR) GetProcAddress (hDLL, "GetCORSystemDirectory"); 
    if( GetCORSystemDirectory!=NULL)
    {
        TCHAR buffer[MAX_PATH];
        DWORD length;
        HRESULT hr = GetCORSystemDirectory(buffer,MAX_PATH,&length);

        // buffer should contain the folder name
        // use it..

    }

    return 0;
}

答案 1 :(得分:0)

您需要将“GetCORSystemDirectory”作为第二个参数传递给GetProcAddress() - 它将返回指向该函数实现的指针。然后你将适当地转换指针并通过它调用函数。