Cute
Cute

Reputation: 14021

How to Use GetCORSystemDirectory()?

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);
        }
    }   

i did like this to get the .NET installation path but i am getting Linker Errors.

error LNK2019: unresolved external symbol _GetCORSystemDirectory@12 referenced in function _main dot.obj

Upvotes: 0

Views: 514

Answers (2)

Indy9000
Indy9000

Reputation: 8871

define the GetCORSystemDirectory signature:

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

initialise the function pointer:

FNPTR_GET_COR_SYS_DIR   GetCORSystemDirectory = NULL;

get a function pointer from mscoree.dll and use:

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

As requested:

#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;
}

Upvotes: 1

sharptooth
sharptooth

Reputation: 170509

You need to pass "GetCORSystemDirectory" as the second parameter into GetProcAddress() - it will return a pointer to that function implementation. Then you'll cast the pointer appropriately and call the function through it.

Upvotes: 0

Related Questions